Skip to content

Commit

Permalink
Add "service.instance.id" to every span (#199)
Browse files Browse the repository at this point in the history
* Add serviceInstanceId to every span, add test, add uuid package to go mod

* Bump to latest

* lint

* Use google uuid package

* Update to use WithAttributes instead
  • Loading branch information
prodion23 committed Jan 6, 2023
1 parent 700794e commit c886e58
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 4 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ require (
google.golang.org/protobuf v1.28.1
)

require github.com/google/uuid v1.1.2

require (
cloud.google.com/go v0.81.0 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ github.com/google/pprof v0.0.0-20201203190320-1bf35d6f28c2/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20210122040257-d980be63207e/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20210226084205-cbba55b83ad5/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
Expand Down
15 changes: 12 additions & 3 deletions instrumentation/opentelemetry/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ func TestOtlpService(t *testing.T) {
shutdown := Init(cfg)
defer shutdown()

_, tp, err := RegisterService("custom_service", map[string]string{"test1": "val1"})
startSpan, tp, err := RegisterService("custom_service", map[string]string{"test1": "val1"})
_, s, _ := startSpan(context.Background(), "test_span", nil)
assert.False(t, s.IsNoop())
assert.NotEqual(t, trace.NewNoopTracerProvider(), tp)
assert.Len(t, s.GetAttributes().GetValue("service.instance.id"), 36)
if err != nil {
log.Fatalf("Error while initializing service: %v", err)
}
Expand Down Expand Up @@ -364,9 +367,12 @@ func TestInitWithSpanProcessorWrapper(t *testing.T) {
defer shutdown()

// test wrapper is called for spans created by global trace provider
_, _, spanEnder := StartSpan(context.Background(), "my_span", nil)
_, span, spanEnder := StartSpan(context.Background(), "my_span", nil)
id1 := span.GetAttributes().GetValue("service.instance.id")
spanEnder()

assert.Len(t, id1, 36)

// my_span and startup spans
assert.Equal(t, 2, wrapper.onStartCount)
assert.Equal(t, 2, wrapper.onEndCount)
Expand All @@ -377,7 +383,10 @@ func TestInitWithSpanProcessorWrapper(t *testing.T) {
log.Fatalf("Error while initializing service: %v", err)
}

_, _, spanEnder = startSpan(context.Background(), "service_span", nil)
_, serviceSpan, spanEnder := startSpan(context.Background(), "service_span", nil)
id2 := serviceSpan.GetAttributes().GetValue("service.instance.id")
assert.Len(t, id2, 36)
assert.Equal(t, id1, id2)
spanEnder()

// service_span, my_span and startup spans
Expand Down
12 changes: 12 additions & 0 deletions instrumentation/opentelemetry/internal/identifier/identifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package identifier

import (
"github.com/google/uuid"
"go.opentelemetry.io/otel/attribute"
)

var ServiceInstanceIDAttr = attribute.StringValue(uuid.New().String())

const ServiceInstanceIDKey = "service.instance.id"

var ServiceInstanceKeyValue = attribute.KeyValue{Key: ServiceInstanceIDKey, Value: ServiceInstanceIDAttr}
4 changes: 3 additions & 1 deletion instrumentation/opentelemetry/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"time"

"github.com/hypertrace/goagent/instrumentation/opentelemetry/internal/identifier"
"github.com/hypertrace/goagent/sdk"
"github.com/hypertrace/goagent/version"
"go.opentelemetry.io/otel"
Expand Down Expand Up @@ -111,17 +112,18 @@ func startSpan(provider getTracerProvider) sdk.StartSpan {

if opts != nil {
startOpts = append(startOpts, trace.WithSpanKind(mapSpanKind(opts.Kind)))

if opts.Timestamp.IsZero() {
startOpts = append(startOpts, trace.WithTimestamp(time.Now()))
} else {
startOpts = append(startOpts, trace.WithTimestamp(opts.Timestamp))
}
}
startOpts = append(startOpts, trace.WithAttributes(identifier.ServiceInstanceKeyValue))

ctx, span := provider().
Tracer(TracerDomain, trace.WithInstrumentationVersion(version.Version)).
Start(ctx, name, startOpts...)

return ctx, &Span{span}, func() { span.End() }
}
}
Expand Down
11 changes: 11 additions & 0 deletions instrumentation/opentelemetry/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package opentelemetry
import (
"context"
"errors"
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -48,6 +49,16 @@ func TestSetAttributeSuccess(t *testing.T) {
s.SetAttribute("test_key_4", errors.New("xyz"))
}

func TestSpanHasSameServiceInstanceId(t *testing.T) {
_, original, _ := StartSpan(context.Background(), "test_span", &sdk.SpanOptions{})
firstId := original.GetAttributes().GetValue("service.instance.id")
for i := 0; i < 300; i++ {
_, anotherSpan, _ := StartSpan(context.Background(), fmt.Sprintf("%s%d", "test_span", i), &sdk.SpanOptions{})
nextId := anotherSpan.GetAttributes().GetValue("service.instance.id")
assert.Equal(t, firstId, nextId)
}
}

func TestGenerateAttribute(t *testing.T) {
assert.Equal(t, attribute.BOOL, generateAttribute("key", true).Value.Type())
assert.Equal(t, attribute.BOOLSLICE, generateAttribute("key", []bool{true}).Value.Type())
Expand Down

0 comments on commit c886e58

Please sign in to comment.