Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RegisterTracerProviderWithContext
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Dye <[email protected]>
andrewwdye committed Jun 24, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 1b5eb0f commit d504995
Showing 6 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/single/start.go
Original file line number Diff line number Diff line change
@@ -202,7 +202,7 @@ var startCmd = &cobra.Command{
for _, serviceName := range []string{otelutils.AdminClientTracer, otelutils.AdminGormTracer, otelutils.AdminServerTracer,
otelutils.BlobstoreClientTracer, otelutils.DataCatalogClientTracer, otelutils.DataCatalogGormTracer,
otelutils.DataCatalogServerTracer, otelutils.FlytePropellerTracer, otelutils.K8sClientTracer} {
if err := otelutils.RegisterTracerProvider(serviceName, otelutils.GetConfig()); err != nil {
if err := otelutils.RegisterTracerProviderWithContext(ctx, serviceName, otelutils.GetConfig()); err != nil {
logger.Errorf(ctx, "Failed to create otel tracer provider. %v", err)
return err
}
2 changes: 1 addition & 1 deletion datacatalog/cmd/entrypoints/serve.go
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ var serveCmd = &cobra.Command{

// register otel tracer providers
for _, serviceName := range []string{otelutils.DataCatalogGormTracer, otelutils.DataCatalogServerTracer} {
if err := otelutils.RegisterTracerProvider(serviceName, otelutils.GetConfig()); err != nil {
if err := otelutils.RegisterTracerProviderWithContext(ctx, serviceName, otelutils.GetConfig()); err != nil {
logger.Errorf(ctx, "Failed to create otel tracer provider. %v", err)
return err
}
2 changes: 1 addition & 1 deletion flyteadmin/cmd/entrypoints/serve.go
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ var serveCmd = &cobra.Command{

// register otel tracer providers
for _, serviceName := range []string{otelutils.AdminGormTracer, otelutils.AdminServerTracer, otelutils.BlobstoreClientTracer} {
if err := otelutils.RegisterTracerProvider(serviceName, otelutils.GetConfig()); err != nil {
if err := otelutils.RegisterTracerProviderWithContext(ctx, serviceName, otelutils.GetConfig()); err != nil {
logger.Errorf(ctx, "Failed to create otel tracer provider. %v", err)
return err
}
2 changes: 1 addition & 1 deletion flytepropeller/cmd/controller/cmd/root.go
Original file line number Diff line number Diff line change
@@ -121,7 +121,7 @@ func executeRootCmd(baseCtx context.Context, cfg *config2.Config) error {
// register opentelementry tracer providers
for _, serviceName := range []string{otelutils.AdminClientTracer, otelutils.BlobstoreClientTracer,
otelutils.DataCatalogClientTracer, otelutils.FlytePropellerTracer, otelutils.K8sClientTracer} {
if err := otelutils.RegisterTracerProvider(serviceName, otelutils.GetConfig()); err != nil {
if err := otelutils.RegisterTracerProviderWithContext(ctx, serviceName, otelutils.GetConfig()); err != nil {
logger.Errorf(ctx, "Failed to create otel tracer provider. %v", err)
return err
}
15 changes: 12 additions & 3 deletions flytestdlib/otelutils/factory.go
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ import (
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
rawtrace "go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/noop"

@@ -35,7 +35,14 @@ const (
var tracerProviders = make(map[string]*trace.TracerProvider)
var noopTracerProvider = noop.NewTracerProvider()

// Deprecated: RegisterTracerProvider registers a tracer provider for the given service name. It uses a default context if necessary.
// Instead, use RegisterTracerProviderWithContext.
func RegisterTracerProvider(serviceName string, config *Config) error {
return RegisterTracerProviderWithContext(context.Background(), serviceName, config)

Check warning on line 41 in flytestdlib/otelutils/factory.go

Codecov / codecov/patch

flytestdlib/otelutils/factory.go#L41

Added line #L41 was not covered by tests
}

// RegisterTracerProviderWithContext registers a tracer provider for the given service name.
func RegisterTracerProviderWithContext(ctx context.Context, serviceName string, config *Config) error {
if config == nil {
return nil
}
@@ -71,15 +78,15 @@ func RegisterTracerProvider(serviceName string, config *Config) error {
}
case OtlpGrpcExporter:
exporter, err = otlptracegrpc.New(
context.Background(),
ctx,
otlptracegrpc.WithEndpointURL(config.OtlpGrpcConfig.Endpoint),
)
if err != nil {
return err

Check warning on line 85 in flytestdlib/otelutils/factory.go

Codecov / codecov/patch

flytestdlib/otelutils/factory.go#L79-L85

Added lines #L79 - L85 were not covered by tests
}
case OtlpHttpExporter:
exporter, err = otlptracehttp.New(
context.Background(),
ctx,
otlptracehttp.WithEndpointURL(config.OtlpHttpConfig.Endpoint),
)
if err != nil {
@@ -121,6 +128,7 @@ func RegisterTracerProvider(serviceName string, config *Config) error {
return nil
}

// GetTracerProvider returns the tracer provider for the given service name.
func GetTracerProvider(serviceName string) rawtrace.TracerProvider {
if t, ok := tracerProviders[serviceName]; ok {
return t
@@ -129,6 +137,7 @@ func GetTracerProvider(serviceName string) rawtrace.TracerProvider {
return noopTracerProvider
}

// NewSpan creates a new span with the given service name and span name.
func NewSpan(ctx context.Context, serviceName string, spanName string) (context.Context, rawtrace.Span) {
var attributes []attribute.KeyValue
for key, value := range contextutils.GetLogFields(ctx) {
7 changes: 4 additions & 3 deletions flytestdlib/otelutils/factory_test.go
Original file line number Diff line number Diff line change
@@ -7,11 +7,12 @@ import (
"github.com/stretchr/testify/assert"
)

func TestRegisterTracerProvider(t *testing.T) {
func TestRegisterTracerProviderWithContext(t *testing.T) {
ctx := context.Background()
serviceName := "foo"

// register tracer provider with no exporters
err := RegisterTracerProvider(serviceName, defaultConfig)
err := RegisterTracerProviderWithContext(ctx, serviceName, defaultConfig)
assert.Nil(t, err)

// validate no tracerProviders are registered
@@ -28,7 +29,7 @@ func TestRegisterTracerProvider(t *testing.T) {
ParentSampler: AlwaysSample,
},
}
err = RegisterTracerProvider(serviceName, &fullConfig)
err = RegisterTracerProviderWithContext(ctx, serviceName, &fullConfig)
assert.Nil(t, err)

// validate tracerProvider is registered

0 comments on commit d504995

Please sign in to comment.