Skip to content

Commit

Permalink
[enhancement] add couple missing zapcore types (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhail5555 authored May 21, 2024
1 parent ff4dcb1 commit 758f26c
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 13 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Run Tests

on:
push:
branches:
- main

tags:
- v*

pull_request:

workflow_dispatch:

jobs:
test-projects:
strategy:
matrix:
project: ["otelslog", "otelzap", "otelzerolog"]
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{ matrix.project }}
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Golang with cache
id: golang-with-cache
uses: magnetikonline/action-golang-cache@v5
with:
go-version-file: ${{ matrix.project }}/go.mod

- name: Set up gotestfmt
run: go install github.com/gotesttools/gotestfmt/v2/cmd/[email protected]

- name: Run tests
run: |
set -euo pipefail
go test -json -v ./... 2>&1 | gotestfmt -hide empty-packages
12 changes: 7 additions & 5 deletions otelslog/otel_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package otelslog
import (
"bytes"
"context"
"log/slog"
"os"
"testing"

"github.com/agoda-com/opentelemetry-logs-go/exporters/stdout/stdoutlogs"
"github.com/stretchr/testify/assert"
"log/slog"

sdk "github.com/agoda-com/opentelemetry-logs-go/sdk/logs"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
"os"
"testing"
)

// configure common attributes for all logs
Expand Down Expand Up @@ -42,7 +43,7 @@ func TestNewOtelHandler(t *testing.T) {
)

handler := NewOtelHandler(loggerProvider, &HandlerOptions{
level: slog.LevelInfo,
Level: slog.LevelInfo,
}).
WithAttrs([]slog.Attr{slog.String("first", "value1")}).
WithGroup("group1").
Expand All @@ -58,5 +59,6 @@ func TestNewOtelHandler(t *testing.T) {

actual := buf.String()

assert.Contains(t, actual, "INFO hello slog [scopeInfo: github.com/agoda-com/otelslog:0.0.1] {host.name=CLX4NV72V6, service.name=otelslog-example, service.version=1.0.0, first=value1, group1.second=value2, group1.group2.myKey=myValue}")
assert.Contains(t, actual, "INFO hello slog [scopeInfo: github.com/agoda-com/otelslog:0.0.1] {host.name=")
assert.Contains(t, actual, "service.name=otelslog-example, service.version=1.0.0, first=value1, group1.second=value2, group1.group2.myKey=myValue}")
}
28 changes: 27 additions & 1 deletion otelzap/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ limitations under the License.
package otelzap

import (
"encoding/base64"
"fmt"
"math"
"reflect"
"time"

otel "github.com/agoda-com/opentelemetry-logs-go/logs"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
"go.uber.org/zap/zapcore"
"math"
)

// otelLevel zap level to otlp level converter
Expand Down Expand Up @@ -82,6 +87,27 @@ func otelAttribute(f zapcore.Field) []attribute.KeyValue {
return []attribute.KeyValue{}
case zapcore.SkipType:
return []attribute.KeyValue{}
case zapcore.BinaryType:
return []attribute.KeyValue{attribute.String(f.Key, base64.StdEncoding.EncodeToString(f.Interface.([]byte)))}
case zapcore.ByteStringType:
return []attribute.KeyValue{attribute.String(f.Key, string(f.Interface.([]byte)))}
case zapcore.DurationType:
return []attribute.KeyValue{attribute.Float64(f.Key, float64(f.Integer)/float64(time.Second))}
case zapcore.TimeType:
if f.Interface != nil {
return []attribute.KeyValue{attribute.Int64(f.Key, time.Unix(0, f.Integer).In(f.Interface.(*time.Location)).Unix())}
}
return []attribute.KeyValue{attribute.Int64(f.Key, time.Unix(0, f.Integer).Unix())} // Fall back to UTC if location is nil.
case zapcore.TimeFullType:
return []attribute.KeyValue{attribute.Int64(f.Key, f.Interface.(time.Time).Unix())}
case zapcore.StringerType:
if stinger, ok := f.Interface.(fmt.Stringer); ok {
return []attribute.KeyValue{attribute.Stringer(f.Key, stinger)}
}
if v := reflect.ValueOf(f.Interface); v.Kind() == reflect.Ptr && v.IsNil() {
return []attribute.KeyValue{attribute.String(f.Key, "<nil>")}
}
return []attribute.KeyValue{}
}
// unhandled types will be treated as string
return []attribute.KeyValue{attribute.String(f.Key, f.String)}
Expand Down
46 changes: 46 additions & 0 deletions otelzap/conv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package otelzap

import (
"bytes"
"errors"
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

var (
testFieldKey = "test-123"
testNow = time.Now()
)

func TestOTeLAttributeMapping(t *testing.T) {
tests := []struct {
Input zapcore.Field
Expected []attribute.KeyValue
}{
{Input: zap.Bool(testFieldKey, true), Expected: []attribute.KeyValue{attribute.Bool(testFieldKey, true)}},
{Input: zap.Float64(testFieldKey, 123.123), Expected: []attribute.KeyValue{attribute.Float64(testFieldKey, 123.123)}},
{Input: zap.Int(testFieldKey, 123), Expected: []attribute.KeyValue{attribute.Int64(testFieldKey, 123)}},
{Input: zap.String(testFieldKey, "hello"), Expected: []attribute.KeyValue{attribute.String(testFieldKey, "hello")}},
{Input: zap.ByteString(testFieldKey, []byte("hello")), Expected: []attribute.KeyValue{attribute.String(testFieldKey, "hello")}},
{Input: zap.Binary(testFieldKey, []byte{1, 0, 0, 1}), Expected: []attribute.KeyValue{attribute.String(testFieldKey, "AQAAAQ==")}},
{Input: zap.Duration(testFieldKey, time.Minute), Expected: []attribute.KeyValue{attribute.Float64(testFieldKey, time.Minute.Seconds())}},
{Input: zap.Time(testFieldKey, testNow), Expected: []attribute.KeyValue{attribute.Int64(testFieldKey, testNow.Unix())}},
{Input: zap.Stringer(testFieldKey, bytes.NewBuffer([]byte("hello"))), Expected: []attribute.KeyValue{attribute.String(testFieldKey, "hello")}},
{Input: zap.Error(errors.New("world")), Expected: []attribute.KeyValue{semconv.ExceptionMessage("world")}},
{Input: zap.Skip(), Expected: []attribute.KeyValue{}},
}

for _, test := range tests {
t.Run(fmt.Sprintf("%+v", test.Input), func(t *testing.T) {
output := otelAttribute(test.Input)
assert.ElementsMatch(t, test.Expected, output)
})
}
}
4 changes: 4 additions & 0 deletions otelzap/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ go 1.20

require (
github.com/agoda-com/opentelemetry-logs-go v0.3.0
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/otel v1.18.0
go.opentelemetry.io/otel/sdk v1.18.0
go.opentelemetry.io/otel/trace v1.18.0
go.uber.org/zap v1.26.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel/metric v1.18.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/sys v0.12.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
29 changes: 22 additions & 7 deletions otelzap/go.sum
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
github.com/agoda-com/opentelemetry-logs-go v0.2.0/go.mod h1:bjUiOkH82zqgjLmxk0v9axmV5b9Ty+1qlixsrAMTLwQ=
github.com/agoda-com/opentelemetry-logs-go v0.3.0 h1:d2lMVUfCDeLzVgTxMeSU8IWaMXjwD4sVKigEZBGwcsw=
github.com/agoda-com/opentelemetry-logs-go v0.3.0/go.mod h1:k3QR1O5AOl+dFC7pkrK9wWmoD72jjDONPFHi9dAgLJc=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
go.opentelemetry.io/otel v1.17.0/go.mod h1:I2vmBGtFaODIVMBSTPVDlJSzBDNf93k60E6Ft0nyjo0=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go.opentelemetry.io/otel v1.18.0 h1:TgVozPGZ01nHyDZxK5WGPFB9QexeTMXEH7+tIClWfzs=
go.opentelemetry.io/otel v1.18.0/go.mod h1:9lWqYO0Db579XzVuCKFNPDl4s73Voa+zEck3wHaAYQI=
go.opentelemetry.io/otel/metric v1.17.0/go.mod h1:h4skoxdZI17AxwITdmdZjjYJQH5nzijUUjm+wtPph5o=
go.opentelemetry.io/otel/metric v1.18.0 h1:JwVzw94UYmbx3ej++CwLUQZxEODDj/pOuTCvzhtRrSQ=
go.opentelemetry.io/otel/metric v1.18.0/go.mod h1:nNSpsVDjWGfb7chbRLUNW+PBNdcSTHD4Uu5pfFMOI0k=
go.opentelemetry.io/otel/sdk v1.17.0/go.mod h1:U87sE0f5vQB7hwUoW98pW5Rz4ZDuCFBZFNUBlSgmDFQ=
go.opentelemetry.io/otel/sdk v1.18.0 h1:e3bAB0wB3MljH38sHzpV/qWrOTCFrdZF2ct9F8rBkcY=
go.opentelemetry.io/otel/sdk v1.18.0/go.mod h1:1RCygWV7plY2KmdskZEDDBs4tJeHG92MdHZIluiYs/M=
go.opentelemetry.io/otel/trace v1.17.0/go.mod h1:I/4vKTgFclIsXRVucpH25X0mpFSczM7aHeaz0ZBLWjY=
go.opentelemetry.io/otel/trace v1.18.0 h1:NY+czwbHbmndxojTEKiSMHkG2ClNH2PwmcHrdo0JY10=
go.opentelemetry.io/otel/trace v1.18.0/go.mod h1:T2+SGJGuYZY3bjj5rgh/hN7KIrlpWC5nS8Mjvzckz+0=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.25.0/go.mod h1:JIAUzQIH94IC4fOJQm7gMmBJP5k7wQfdcnYdPoEXJYk=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 758f26c

Please sign in to comment.