Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,13 @@ formatters:
enable:
- gofumpt
- goimports
- golines
settings:
goimports:
local-prefixes:
- go.opentelemetry.io
golines:
max-len: 120
exclusions:
generated: lax
paths:
Expand Down
43 changes: 38 additions & 5 deletions attribute/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,47 @@ func TestSetDedup(t *testing.T) {
cases := []testCase{
expect("A=B", attribute.String("A", "2"), attribute.String("A", "B")),
expect("A=B", attribute.String("A", "2"), attribute.Int("A", 1), attribute.String("A", "B")),
expect("A=B", attribute.String("A", "B"), attribute.String("A", "C"), attribute.String("A", "D"), attribute.String("A", "B")),
expect(
"A=B",
attribute.String("A", "B"),
attribute.String("A", "C"),
attribute.String("A", "D"),
attribute.String("A", "B"),
),

expect("A=B,C=D", attribute.String("A", "1"), attribute.String("C", "D"), attribute.String("A", "B")),
expect("A=B,C=D", attribute.String("A", "2"), attribute.String("A", "B"), attribute.String("C", "D")),
expect("A=B,C=D", attribute.Float64("C", 1.2), attribute.String("A", "2"), attribute.String("A", "B"), attribute.String("C", "D")),
expect("A=B,C=D", attribute.String("C", "D"), attribute.String("A", "B"), attribute.String("A", "C"), attribute.String("A", "D"), attribute.String("A", "B")),
expect("A=B,C=D", attribute.String("A", "B"), attribute.String("C", "D"), attribute.String("A", "C"), attribute.String("A", "D"), attribute.String("A", "B")),
expect("A=B,C=D", attribute.String("A", "B"), attribute.String("A", "C"), attribute.String("A", "D"), attribute.String("A", "B"), attribute.String("C", "D")),
expect(
"A=B,C=D",
attribute.Float64("C", 1.2),
attribute.String("A", "2"),
attribute.String("A", "B"),
attribute.String("C", "D"),
),
expect(
"A=B,C=D",
attribute.String("C", "D"),
attribute.String("A", "B"),
attribute.String("A", "C"),
attribute.String("A", "D"),
attribute.String("A", "B"),
),
expect(
"A=B,C=D",
attribute.String("A", "B"),
attribute.String("C", "D"),
attribute.String("A", "C"),
attribute.String("A", "D"),
attribute.String("A", "B"),
),
expect(
"A=B,C=D",
attribute.String("A", "B"),
attribute.String("A", "C"),
attribute.String("A", "D"),
attribute.String("A", "B"),
attribute.String("C", "D"),
),
}
enc := attribute.DefaultEncoder()

Expand Down
4 changes: 3 additions & 1 deletion baggage/baggage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,9 @@ func BenchmarkParse(b *testing.B) {
b.ReportAllocs()

for i := 0; i < b.N; i++ {
benchBaggage, _ = Parse("userId=alice,serverNode = DF28 , isProduction = false,hasProp=stuff;propKey;propWValue=value, invalidUtf8=pr%ffo%ffp%fcValue")
benchBaggage, _ = Parse(
"userId=alice,serverNode = DF28 , isProduction = false,hasProp=stuff;propKey;propWValue=value, invalidUtf8=pr%ffo%ffp%fcValue",
)
}
}

Expand Down
6 changes: 5 additions & 1 deletion bridge/opencensus/internal/oc2otel/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ func TestAttributesFromMap(t *testing.T) {
gotAttributeSet := attribute.NewSet(got...)
wantAttributeSet := attribute.NewSet(want...)
if !gotAttributeSet.Equals(&wantAttributeSet) {
t.Errorf("Attributes conversion want %v, got %v", wantAttributeSet.Encoded(attribute.DefaultEncoder()), gotAttributeSet.Encoded(attribute.DefaultEncoder()))
t.Errorf(
"Attributes conversion want %v, got %v",
wantAttributeSet.Encoded(attribute.DefaultEncoder()),
gotAttributeSet.Encoded(attribute.DefaultEncoder()),
)
}
}

Expand Down
31 changes: 25 additions & 6 deletions bridge/opencensus/internal/ocmetric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ var (
errNegativeCount = errors.New("distribution or summary count is negative")
errNegativeBucketCount = errors.New("distribution bucket count is negative")
errMismatchedAttributeKeyValues = errors.New("mismatched number of attribute keys and values")
errInvalidExemplarSpanContext = errors.New("span context exemplar attachment does not contain an OpenCensus SpanContext")
errInvalidExemplarSpanContext = errors.New(
"span context exemplar attachment does not contain an OpenCensus SpanContext",
)
)

// ConvertMetrics converts metric data from OpenCensus to OpenTelemetry.
Expand Down Expand Up @@ -76,20 +78,29 @@ func convertAggregation(metric *ocmetricdata.Metric) (metricdata.Aggregation, er
}

// convertGauge converts an OpenCensus gauge to an OpenTelemetry gauge aggregation.
func convertGauge[N int64 | float64](labelKeys []ocmetricdata.LabelKey, ts []*ocmetricdata.TimeSeries) (metricdata.Gauge[N], error) {
func convertGauge[N int64 | float64](
labelKeys []ocmetricdata.LabelKey,
ts []*ocmetricdata.TimeSeries,
) (metricdata.Gauge[N], error) {
points, err := convertNumberDataPoints[N](labelKeys, ts)
return metricdata.Gauge[N]{DataPoints: points}, err
}

// convertSum converts an OpenCensus cumulative to an OpenTelemetry sum aggregation.
func convertSum[N int64 | float64](labelKeys []ocmetricdata.LabelKey, ts []*ocmetricdata.TimeSeries) (metricdata.Sum[N], error) {
func convertSum[N int64 | float64](
labelKeys []ocmetricdata.LabelKey,
ts []*ocmetricdata.TimeSeries,
) (metricdata.Sum[N], error) {
points, err := convertNumberDataPoints[N](labelKeys, ts)
// OpenCensus sums are always Cumulative
return metricdata.Sum[N]{DataPoints: points, Temporality: metricdata.CumulativeTemporality, IsMonotonic: true}, err
}

// convertNumberDataPoints converts OpenCensus TimeSeries to OpenTelemetry DataPoints.
func convertNumberDataPoints[N int64 | float64](labelKeys []ocmetricdata.LabelKey, ts []*ocmetricdata.TimeSeries) ([]metricdata.DataPoint[N], error) {
func convertNumberDataPoints[N int64 | float64](
labelKeys []ocmetricdata.LabelKey,
ts []*ocmetricdata.TimeSeries,
) ([]metricdata.DataPoint[N], error) {
var points []metricdata.DataPoint[N]
var err error
for _, t := range ts {
Expand Down Expand Up @@ -117,7 +128,10 @@ func convertNumberDataPoints[N int64 | float64](labelKeys []ocmetricdata.LabelKe

// convertHistogram converts OpenCensus Distribution timeseries to an
// OpenTelemetry Histogram aggregation.
func convertHistogram(labelKeys []ocmetricdata.LabelKey, ts []*ocmetricdata.TimeSeries) (metricdata.Histogram[float64], error) {
func convertHistogram(
labelKeys []ocmetricdata.LabelKey,
ts []*ocmetricdata.TimeSeries,
) (metricdata.Histogram[float64], error) {
points := make([]metricdata.HistogramDataPoint[float64], 0, len(ts))
var err error
for _, t := range ts {
Expand Down Expand Up @@ -390,7 +404,12 @@ func convertQuantiles(snapshot ocmetricdata.Snapshot) []metricdata.QuantileValue
// OpenTelemetry attribute Set.
func convertAttrs(keys []ocmetricdata.LabelKey, values []ocmetricdata.LabelValue) (attribute.Set, error) {
if len(keys) != len(values) {
return attribute.NewSet(), fmt.Errorf("%w: keys(%q) values(%q)", errMismatchedAttributeKeyValues, len(keys), len(values))
return attribute.NewSet(), fmt.Errorf(
"%w: keys(%q) values(%q)",
errMismatchedAttributeKeyValues,
len(keys),
len(values),
)
}
attrs := []attribute.KeyValue{}
for i, lv := range values {
Expand Down
16 changes: 14 additions & 2 deletions bridge/opencensus/internal/ocmetric/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,10 +989,22 @@ func TestConvertAttributes(t *testing.T) {
t.Run(tc.desc, func(t *testing.T) {
output, err := convertAttrs(tc.inputKeys, tc.inputValues)
if !errors.Is(err, tc.expectedErr) {
t.Errorf("convertAttrs(keys: %v, values: %v) = err(%v), want err(%v)", tc.inputKeys, tc.inputValues, err, tc.expectedErr)
t.Errorf(
"convertAttrs(keys: %v, values: %v) = err(%v), want err(%v)",
tc.inputKeys,
tc.inputValues,
err,
tc.expectedErr,
)
}
if !output.Equals(tc.expected) {
t.Errorf("convertAttrs(keys: %v, values: %v) = %+v, want %+v", tc.inputKeys, tc.inputValues, output.ToSlice(), tc.expected.ToSlice())
t.Errorf(
"convertAttrs(keys: %v, values: %v) = %+v, want %+v",
tc.inputKeys,
tc.inputValues,
output.ToSlice(),
tc.expected.ToSlice(),
)
}
})
}
Expand Down
26 changes: 22 additions & 4 deletions bridge/opencensus/internal/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,18 @@ func TestSpanSetStatus(t *testing.T) {
ocS.SetStatus(status)

if s.sCode != tt.wantCode {
t.Errorf("span.SetStatus failed to set OpenTelemetry status code. Expected %d, got %d", tt.wantCode, s.sCode)
t.Errorf(
"span.SetStatus failed to set OpenTelemetry status code. Expected %d, got %d",
tt.wantCode,
s.sCode,
)
}
if s.sMsg != tt.message {
t.Errorf("span.SetStatus failed to set OpenTelemetry status description. Expected %s, got %s", tt.message, s.sMsg)
t.Errorf(
"span.SetStatus failed to set OpenTelemetry status description. Expected %s, got %s",
tt.message,
s.sMsg,
)
}
})
}
Expand Down Expand Up @@ -295,12 +303,22 @@ func TestSpanAddLinkFails(t *testing.T) {

for i, l := range s.links {
if !l.SpanContext.Equal(wantLinks[i].SpanContext) {
t.Errorf("link[%v] has the wrong span context; want %+v, got %+v", i, wantLinks[i].SpanContext, l.SpanContext)
t.Errorf(
"link[%v] has the wrong span context; want %+v, got %+v",
i,
wantLinks[i].SpanContext,
l.SpanContext,
)
}
gotAttributeSet := attribute.NewSet(l.Attributes...)
wantAttributeSet := attribute.NewSet(wantLinks[i].Attributes...)
if !gotAttributeSet.Equals(&wantAttributeSet) {
t.Errorf("link[%v] has the wrong attributes; want %v, got %v", i, wantAttributeSet.Encoded(attribute.DefaultEncoder()), gotAttributeSet.Encoded(attribute.DefaultEncoder()))
t.Errorf(
"link[%v] has the wrong attributes; want %v, got %v",
i,
wantAttributeSet.Encoded(attribute.DefaultEncoder()),
gotAttributeSet.Encoded(attribute.DefaultEncoder()),
)
}
}
}
Expand Down
17 changes: 14 additions & 3 deletions bridge/opencensus/internal/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ func NewTracer(tracer trace.Tracer) octrace.Tracer {

// StartSpan starts a new child span of the current span in the context. If
// there is no span in the context, it creates a new trace and span.
func (o *Tracer) StartSpan(ctx context.Context, name string, s ...octrace.StartOption) (context.Context, *octrace.Span) {
func (o *Tracer) StartSpan(
ctx context.Context,
name string,
s ...octrace.StartOption,
) (context.Context, *octrace.Span) {
otelOpts, err := oc2otel.StartOptions(s)
if err != nil {
Handle(fmt.Errorf("starting span %q: %w", name, err))
Expand All @@ -36,7 +40,12 @@ func (o *Tracer) StartSpan(ctx context.Context, name string, s ...octrace.StartO

// StartSpanWithRemoteParent starts a new child span of the span from the
// given parent.
func (o *Tracer) StartSpanWithRemoteParent(ctx context.Context, name string, parent octrace.SpanContext, s ...octrace.StartOption) (context.Context, *octrace.Span) {
func (o *Tracer) StartSpanWithRemoteParent(
ctx context.Context,
name string,
parent octrace.SpanContext,
s ...octrace.StartOption,
) (context.Context, *octrace.Span) {
// make sure span context is zeroed out so we use the remote parent
ctx = trace.ContextWithSpan(ctx, nil)
ctx = trace.ContextWithRemoteSpanContext(ctx, oc2otel.SpanContext(parent))
Expand All @@ -53,6 +62,8 @@ func (o *Tracer) NewContext(parent context.Context, s *octrace.Span) context.Con
if otSpan, ok := s.Internal().(*Span); ok {
return trace.ContextWithSpan(parent, otSpan.otelSpan)
}
Handle(fmt.Errorf("unable to create context with span %q, since it was created using a different tracer", s.String()))
Handle(
fmt.Errorf("unable to create context with span %q, since it was created using a different tracer", s.String()),
)
return parent
}
6 changes: 5 additions & 1 deletion bridge/opencensus/test/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ func TestStartSpanWithRemoteParent(t *testing.T) {
ctx := context.Background()
ctx, parent := tracer.Start(ctx, "OpenTelemetrySpan1")

_, span := octrace.StartSpanWithRemoteParent(ctx, "OpenCensusSpan", ocbridge.OTelSpanContextToOC(parent.SpanContext()))
_, span := octrace.StartSpanWithRemoteParent(
ctx,
"OpenCensusSpan",
ocbridge.OTelSpanContextToOC(parent.SpanContext()),
)
span.End()

spans := sr.Ended()
Expand Down
24 changes: 18 additions & 6 deletions bridge/opentracing/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,9 @@ type bridgeSetTracer struct {
func (s *bridgeSetTracer) tracer() trace.Tracer {
if !s.isSet {
s.warnOnce.Do(func() {
s.warningHandler("The OpenTelemetry tracer is not set, default no-op tracer is used! Call SetOpenTelemetryTracer to set it up.\n")
s.warningHandler(
"The OpenTelemetry tracer is not set, default no-op tracer is used! Call SetOpenTelemetryTracer to set it up.\n",
)
})
}
return s.otelTracer
Expand Down Expand Up @@ -362,7 +364,9 @@ func (t *BridgeTracer) baggageSetHook(ctx context.Context, list iBaggage.List) c
}
bSpan, ok := span.(*bridgeSpan)
if !ok {
t.warningHandler("Encountered a foreign OpenTracing span, will not propagate the baggage items from OpenTelemetry context\n")
t.warningHandler(
"Encountered a foreign OpenTracing span, will not propagate the baggage items from OpenTelemetry context\n",
)
return ctx
}
for k, v := range list {
Expand All @@ -374,12 +378,16 @@ func (t *BridgeTracer) baggageSetHook(ctx context.Context, list iBaggage.List) c
func (t *BridgeTracer) baggageGetHook(ctx context.Context, list iBaggage.List) iBaggage.List {
span := ot.SpanFromContext(ctx)
if span == nil {
t.warningHandler("No active OpenTracing span, can not propagate the baggage items from OpenTracing span context\n")
t.warningHandler(
"No active OpenTracing span, can not propagate the baggage items from OpenTracing span context\n",
)
return list
}
bSpan, ok := span.(*bridgeSpan)
if !ok {
t.warningHandler("Encountered a foreign OpenTracing span, will not propagate the baggage items from OpenTracing span context\n")
t.warningHandler(
"Encountered a foreign OpenTracing span, will not propagate the baggage items from OpenTracing span context\n",
)
return list
}
items := bSpan.extraBaggageItems
Expand Down Expand Up @@ -427,7 +435,9 @@ func (t *BridgeTracer) StartSpan(operationName string, opts ...ot.StartSpanOptio
)
if ot.SpanFromContext(checkCtx2) != nil {
t.warnOnce.Do(func() {
t.warningHandler("SDK should have deferred the context setup, see the documentation of go.opentelemetry.io/otel/bridge/opentracing/migration\n")
t.warningHandler(
"SDK should have deferred the context setup, see the documentation of go.opentelemetry.io/otel/bridge/opentracing/migration\n",
)
})
}
if hadTrueErrorTag {
Expand Down Expand Up @@ -473,7 +483,9 @@ func (t *BridgeTracer) ContextWithBridgeSpan(ctx context.Context, span trace.Spa
func (t *BridgeTracer) ContextWithSpanHook(ctx context.Context, span ot.Span) context.Context {
bSpan, ok := span.(*bridgeSpan)
if !ok {
t.warningHandler("Encountered a foreign OpenTracing span, will not run a possible deferred context setup hook\n")
t.warningHandler(
"Encountered a foreign OpenTracing span, will not run a possible deferred context setup hook\n",
)
return ctx
}
if bSpan.skipDeferHook {
Expand Down
6 changes: 5 additions & 1 deletion bridge/opentracing/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,11 @@ type nonDeferWrapperTracer struct {
*WrapperTracer
}

func (t *nonDeferWrapperTracer) Start(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
func (t *nonDeferWrapperTracer) Start(
ctx context.Context,
name string,
opts ...trace.SpanStartOption,
) (context.Context, trace.Span) {
// Run start on the parent wrapper with a brand new context
// so `WithDeferredSetup` hasn't been called, and the OpenTracing context is injected.
return t.WrapperTracer.Start(context.Background(), name, opts...)
Expand Down
6 changes: 5 additions & 1 deletion bridge/opentracing/internal/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ func NewMockTracer() *MockTracer {
}
}

func (t *MockTracer) Start(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
func (t *MockTracer) Start(
ctx context.Context,
name string,
opts ...trace.SpanStartOption,
) (context.Context, trace.Span) {
config := trace.NewSpanStartConfig(opts...)
startTime := config.Timestamp()
if startTime.IsZero() {
Expand Down
Loading
Loading