Skip to content

Commit

Permalink
Tracing: Allow trace id to propagate and included in logs even though…
Browse files Browse the repository at this point in the history
… tracing disabled (#1126)
  • Loading branch information
marefr authored Oct 24, 2024
1 parent 850229a commit 000fe01
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
8 changes: 8 additions & 0 deletions backend/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,14 @@ func SetupTracer(pluginID string, tracingOpts tracing.Opts) error {

// Initialize global tracer for plugin developer usage
tracing.InitDefaultTracer(otel.Tracer(pluginID))
} else {
pf, err := tracerprovider.NewTextMapPropagator(string(tracerprovider.PropagatorFormatW3C))
if err != nil {
return fmt.Errorf("new propagator format: %w", err)
}

tp := tracerprovider.NewNoOpTracerProvider()
tracerprovider.InitGlobalTracerProvider(tp, pf)
}

enabled := tracingCfg.isEnabled()
Expand Down
11 changes: 11 additions & 0 deletions backend/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package backend
import (
"testing"

"github.com/grafana/grafana-plugin-sdk-go/backend/tracing"
"github.com/grafana/grafana-plugin-sdk-go/build"
"github.com/grafana/grafana-plugin-sdk-go/internal/tracerprovider"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel"
)

func TestGetTracingConfig(t *testing.T) {
Expand Down Expand Up @@ -120,6 +122,15 @@ func TestGetTracingConfig(t *testing.T) {
cfg := getTracingConfig(tc.buildInfoGetter)
require.Equal(t, tc.expEnabled, cfg.isEnabled())
require.Equal(t, tc.expCfg, cfg)

err := SetupTracer("test", tracing.Opts{})
require.NoError(t, err)

tp := otel.GetTracerProvider()
require.NotNil(t, tp)

_, ok := tp.(tracerprovider.TracerProvider)
require.True(t, ok)
})
}
}
6 changes: 3 additions & 3 deletions internal/tracerprovider/tracerprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func (noopTracerProvider) Shutdown(_ context.Context) error {
return nil
}

// newNoOpTracerProvider returns a new noopTracerProvider.
func newNoOpTracerProvider() TracerProvider {
// NewNoOpTracerProvider returns a new noopTracerProvider.
func NewNoOpTracerProvider() TracerProvider {
return &noopTracerProvider{TracerProvider: noop.NewTracerProvider()}
}

Expand Down Expand Up @@ -74,7 +74,7 @@ func newOtelExporter(address string) (tracesdk.SpanExporter, error) {
// It returns a noopTracerProvider if the address is empty, otherwise it returns a new OpenTelemetry TracerProvider.
func NewTracerProvider(address string, samplerOpts SamplerOptions, opts tracing.Opts) (TracerProvider, error) {
if address == "" {
return newNoOpTracerProvider(), nil
return NewNoOpTracerProvider(), nil
}
exp, err := newOtelExporter(address)
if err != nil {
Expand Down

0 comments on commit 000fe01

Please sign in to comment.