Skip to content

Commit

Permalink
Merge pull request #24 from uptrace/fix/otelutil
Browse files Browse the repository at this point in the history
chore: extract attrAny to otelutil
  • Loading branch information
vmihailenco authored Nov 25, 2021
2 parents fbfee06 + 9b1f549 commit 69b84d7
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 215 deletions.
22 changes: 13 additions & 9 deletions example/grpc/api/hello-service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions otelgraphql/example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ go 1.15

replace github.com/uptrace/opentelemetry-go-extra/otelgraphql => ../

replace github.com/uptrace/opentelemetry-go-extra/otelutil => ../../otelutil

replace github.com/uptrace/opentelemetry-go-extra/otelplay => ../../otelplay

require (
Expand Down
3 changes: 3 additions & 0 deletions otelgraphql/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ module github.com/uptrace/opentelemetry-go-extra/otelgraphql

go 1.15

replace github.com/uptrace/opentelemetry-go-extra/otelutil => ../otelutil

require (
github.com/graph-gophers/graphql-go v1.2.0
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/stretchr/testify v1.7.0
github.com/uptrace/opentelemetry-go-extra/otelutil v0.0.0-00010101000000-000000000000
go.opentelemetry.io/contrib v1.2.0
go.opentelemetry.io/otel v1.2.0
go.opentelemetry.io/otel/sdk v1.1.0
Expand Down
65 changes: 4 additions & 61 deletions otelgraphql/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package otelgraphql

import (
"context"
"encoding/json"
"fmt"
"reflect"

"github.com/graph-gophers/graphql-go/errors"
"github.com/graph-gophers/graphql-go/introspection"
Expand All @@ -15,6 +13,8 @@ import (
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
oteltrace "go.opentelemetry.io/otel/trace"

"github.com/uptrace/opentelemetry-go-extra/otelutil"
)

type Tracer struct {
Expand Down Expand Up @@ -56,7 +56,7 @@ func (t Tracer) TraceQuery(
span.SetAttributes(attribute.String("graphql.query", queryString))

for name, value := range variables {
span.SetAttributes(attrAny("graphql.variables."+name, value))
span.SetAttributes(otelutil.Attribute("graphql.variables."+name, value))
}

return ctx, func(errs []*errors.QueryError) {
Expand Down Expand Up @@ -88,7 +88,7 @@ func (t Tracer) TraceField(
span.SetAttributes(attribute.String("graphql.type", typeName))
span.SetAttributes(attribute.String("graphql.field", fieldName))
for name, value := range args {
span.SetAttributes(attrAny("graphql.args."+name, value))
span.SetAttributes(otelutil.Attribute("graphql.args."+name, value))
}

return ctx, func(err *errors.QueryError) {
Expand All @@ -115,60 +115,3 @@ func (t Tracer) TraceValidation(ctx context.Context) trace.TraceValidationFinish
span.End()
}
}

// stolen from otellogrus
func attrAny(key string, value interface{}) attribute.KeyValue {
switch value := value.(type) {
case nil:
return attribute.String(key, "<nil>")
case string:
return attribute.String(key, value)
case int:
return attribute.Int(key, value)
case int64:
return attribute.Int64(key, value)
case uint64:
return attribute.Int64(key, int64(value))
case float64:
return attribute.Float64(key, value)
case bool:
return attribute.Bool(key, value)
case fmt.Stringer:
return attribute.String(key, value.String())
}

rv := reflect.ValueOf(value)

switch rv.Kind() {
case reflect.Array:
rv = rv.Slice(0, rv.Len())
fallthrough
case reflect.Slice:
switch reflect.TypeOf(value).Elem().Kind() {
case reflect.Bool:
return attribute.BoolSlice(key, rv.Interface().([]bool))
case reflect.Int:
return attribute.IntSlice(key, rv.Interface().([]int))
case reflect.Int64:
return attribute.Int64Slice(key, rv.Interface().([]int64))
case reflect.Float64:
return attribute.Float64Slice(key, rv.Interface().([]float64))
case reflect.String:
return attribute.StringSlice(key, rv.Interface().([]string))
default:
return attribute.KeyValue{Key: attribute.Key(key)}
}
case reflect.Bool:
return attribute.Bool(key, rv.Bool())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return attribute.Int64(key, rv.Int())
case reflect.Float64:
return attribute.Float64(key, rv.Float())
case reflect.String:
return attribute.String(key, rv.String())
}
if b, err := json.Marshal(value); b != nil && err == nil {
return attribute.String(key, string(b))
}
return attribute.String(key, fmt.Sprint(value))
}
2 changes: 2 additions & 0 deletions otellogrus/example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ go 1.16

replace github.com/uptrace/opentelemetry-go-extra/otellogrus => ./..

replace github.com/uptrace/opentelemetry-go-extra/otelutil => ../../otelutil

replace github.com/uptrace/opentelemetry-go-extra/otelplay => ../../otelplay

require (
Expand Down
5 changes: 4 additions & 1 deletion otellogrus/go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
module github.com/uptrace/opentelemetry-go-extra/otellogrus

go 1.14
go 1.15

replace github.com/uptrace/opentelemetry-go-extra/otelutil => ../otelutil

require (
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0
github.com/uptrace/opentelemetry-go-extra/otelutil v0.0.0-00010101000000-000000000000
go.opentelemetry.io/otel v1.2.0
go.opentelemetry.io/otel/sdk v1.0.1
go.opentelemetry.io/otel/trace v1.2.0
Expand Down
62 changes: 3 additions & 59 deletions otellogrus/otellogrus.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package otellogrus

import (
"encoding/json"
"fmt"
"reflect"
"strings"

Expand All @@ -12,6 +10,8 @@ import (
"go.opentelemetry.io/otel/codes"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
"go.opentelemetry.io/otel/trace"

"github.com/uptrace/opentelemetry-go-extra/otelutil"
)

var (
Expand Down Expand Up @@ -83,7 +83,7 @@ func (hook *Hook) Fire(entry *logrus.Entry) error {
}
}

attrs = append(attrs, attrAny(k, v))
attrs = append(attrs, otelutil.Attribute(k, v))
}

span.AddEvent("log", trace.WithAttributes(attrs...))
Expand All @@ -107,59 +107,3 @@ func levelString(lvl logrus.Level) string {
}
return strings.ToUpper(s)
}

func attrAny(key string, value interface{}) attribute.KeyValue {
switch value := value.(type) {
case nil:
return attribute.String(key, "<nil>")
case string:
return attribute.String(key, value)
case int:
return attribute.Int(key, value)
case int64:
return attribute.Int64(key, value)
case uint64:
return attribute.Int64(key, int64(value))
case float64:
return attribute.Float64(key, value)
case bool:
return attribute.Bool(key, value)
case fmt.Stringer:
return attribute.String(key, value.String())
}

rv := reflect.ValueOf(value)

switch rv.Kind() {
case reflect.Array:
rv = rv.Slice(0, rv.Len())
fallthrough
case reflect.Slice:
switch reflect.TypeOf(value).Elem().Kind() {
case reflect.Bool:
return attribute.BoolSlice(key, rv.Interface().([]bool))
case reflect.Int:
return attribute.IntSlice(key, rv.Interface().([]int))
case reflect.Int64:
return attribute.Int64Slice(key, rv.Interface().([]int64))
case reflect.Float64:
return attribute.Float64Slice(key, rv.Interface().([]float64))
case reflect.String:
return attribute.StringSlice(key, rv.Interface().([]string))
default:
return attribute.KeyValue{Key: attribute.Key(key)}
}
case reflect.Bool:
return attribute.Bool(key, rv.Bool())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return attribute.Int64(key, rv.Int())
case reflect.Float64:
return attribute.Float64(key, rv.Float())
case reflect.String:
return attribute.String(key, rv.String())
}
if b, err := json.Marshal(value); b != nil && err == nil {
return attribute.String(key, string(b))
}
return attribute.String(key, fmt.Sprint(value))
}
65 changes: 65 additions & 0 deletions otelutil/attribute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package otelutil

import (
"encoding/json"
"fmt"
"reflect"

"go.opentelemetry.io/otel/attribute"
)

func Attribute(key string, value interface{}) attribute.KeyValue {
switch value := value.(type) {
case nil:
return attribute.String(key, "<nil>")
case string:
return attribute.String(key, value)
case int:
return attribute.Int(key, value)
case int64:
return attribute.Int64(key, value)
case uint64:
return attribute.Int64(key, int64(value))
case float64:
return attribute.Float64(key, value)
case bool:
return attribute.Bool(key, value)
case fmt.Stringer:
return attribute.String(key, value.String())
}

rv := reflect.ValueOf(value)

switch rv.Kind() {
case reflect.Array:
rv = rv.Slice(0, rv.Len())
fallthrough
case reflect.Slice:
switch reflect.TypeOf(value).Elem().Kind() {
case reflect.Bool:
return attribute.BoolSlice(key, rv.Interface().([]bool))
case reflect.Int:
return attribute.IntSlice(key, rv.Interface().([]int))
case reflect.Int64:
return attribute.Int64Slice(key, rv.Interface().([]int64))
case reflect.Float64:
return attribute.Float64Slice(key, rv.Interface().([]float64))
case reflect.String:
return attribute.StringSlice(key, rv.Interface().([]string))
default:
return attribute.KeyValue{Key: attribute.Key(key)}
}
case reflect.Bool:
return attribute.Bool(key, rv.Bool())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return attribute.Int64(key, rv.Int())
case reflect.Float64:
return attribute.Float64(key, rv.Float())
case reflect.String:
return attribute.String(key, rv.String())
}
if b, err := json.Marshal(value); b != nil && err == nil {
return attribute.String(key, string(b))
}
return attribute.String(key, fmt.Sprint(value))
}
5 changes: 5 additions & 0 deletions otelutil/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/uptrace/opentelemetry-go-extra/otelutil

go 1.15

require go.opentelemetry.io/otel v1.2.0
18 changes: 18 additions & 0 deletions otelutil/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
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/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
go.opentelemetry.io/otel v1.2.0 h1:YOQDvxO1FayUcT9MIhJhgMyNO1WqoduiyvQHzGN0kUQ=
go.opentelemetry.io/otel v1.2.0/go.mod h1:aT17Fk0Z1Nor9e0uisf98LrntPGMnk4frBO9+dkf69I=
go.opentelemetry.io/otel/trace v1.2.0/go.mod h1:N5FLswTubnxKxOJHM7XZC074qpeEdLy3CgAVsdMucK0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
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.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 2 additions & 0 deletions otelzap/example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ go 1.16

replace github.com/uptrace/opentelemetry-go-extra/otelzap => ../

replace github.com/uptrace/opentelemetry-go-extra/otelutil => ../../otelutil

replace github.com/uptrace/opentelemetry-go-extra/otelplay => ../../otelplay

require (
Expand Down
3 changes: 2 additions & 1 deletion otelzap/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ module github.com/uptrace/opentelemetry-go-extra/otelzap

go 1.15

replace go.uber.org/zap => github.com/uptrace/zap v1.16.1-0.20210206140206-cdb6ad27a440
replace github.com/uptrace/opentelemetry-go-extra/otelutil => ../otelutil

require (
github.com/stretchr/testify v1.7.0
github.com/uptrace/opentelemetry-go-extra/otelutil v0.0.0-00010101000000-000000000000
go.opentelemetry.io/otel v1.2.0
go.opentelemetry.io/otel/sdk v1.0.1
go.opentelemetry.io/otel/trace v1.2.0
Expand Down
Loading

0 comments on commit 69b84d7

Please sign in to comment.