Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Rammer <[email protected]>
  • Loading branch information
hamersaw committed Mar 11, 2023
1 parent cdd2316 commit be42e3f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 22 deletions.
15 changes: 15 additions & 0 deletions telemetryutils/factory.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package telemetryutils

import (
"context"
"os"

"github.com/flyteorg/flytestdlib/contextutils"
"github.com/flyteorg/flytestdlib/version"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/jaeger"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
"go.opentelemetry.io/otel/sdk/resource"
Expand Down Expand Up @@ -93,3 +96,15 @@ func GetTracerProvider(serviceName string) rawtrace.TracerProvider {

return noopTracerProvider
}

func NewSpan(ctx context.Context, serviceName string, spanName string) (context.Context, rawtrace.Span) {
var attributes []attribute.KeyValue
for key, value := range contextutils.GetLogFields(ctx) {
if value, ok := value.(string); ok {
attributes = append(attributes, attribute.String(key, value))
}
}

tracerProvider := GetTracerProvider(serviceName)
return tracerProvider.Tracer("default").Start(ctx, spanName, rawtrace.WithAttributes(attributes...))
}
42 changes: 42 additions & 0 deletions telemetryutils/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package telemetryutils

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func TestRegisterTracerProvider(t *testing.T) {
serviceName := "foo"

// register tracer provider with no exporters
emptyConfig := Config{}
err := RegisterTracerProvider(serviceName, &emptyConfig)
assert.Nil(t, err)

// validate no tracerProviders are registered
assert.Len(t, tracerProviders, 0)

// register tracer provider with all exporters
fullConfig := Config{
FileConfig: FileConfig{
Enabled: true,
Filename: "/dev/null",
},
JaegerConfig: JaegerConfig{
Enabled: true,
},
}
err = RegisterTracerProvider(serviceName, &fullConfig)
assert.Nil(t, err)

// validate tracerProvider is registered
assert.Len(t, tracerProviders, 1)
}

func TestNewSpan(t *testing.T) {
ctx := context.TODO()
_, span := NewSpan(ctx, "bar", "baz/bat")
span.End()
}
22 changes: 0 additions & 22 deletions telemetryutils/utils.go

This file was deleted.

0 comments on commit be42e3f

Please sign in to comment.