| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package observ_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"strconv" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
"google.golang.org/grpc/codes" |
| 14 |
"google.golang.org/grpc/status" |
| 15 |
|
| 16 |
"go.opentelemetry.io/otel" |
| 17 |
"go.opentelemetry.io/otel/attribute" |
| 18 |
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc/internal" |
| 19 |
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc/internal/observ" |
| 20 |
mapi "go.opentelemetry.io/otel/metric" |
| 21 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 22 |
"go.opentelemetry.io/otel/sdk/metric" |
| 23 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 24 |
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" |
| 25 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 26 |
"go.opentelemetry.io/otel/semconv/v1.40.0/otelconv" |
| 27 |
) |
| 28 |
|
| 29 |
const ( |
| 30 |
ID = 0 |
| 31 |
ServerAddr = "localhost" |
| 32 |
ServerPort = 4317 |
| 33 |
) |
| 34 |
|
| 35 |
var Target = "dns://" + ServerAddr + ":" + strconv.Itoa(ServerPort) |
| 36 |
|
| 37 |
var Scope = instrumentation.Scope{ |
| 38 |
Name: observ.ScopeName, |
| 39 |
Version: observ.Version, |
| 40 |
SchemaURL: observ.SchemaURL, |
| 41 |
} |
| 42 |
|
| 43 |
type errMeterProvider struct { |
| 44 |
mapi.MeterProvider |
| 45 |
|
| 46 |
err error |
| 47 |
} |
| 48 |
|
| 49 |
func (m *errMeterProvider) Meter(string, ...mapi.MeterOption) mapi.Meter { |
| 50 |
return &errMeter{err: m.err} |
| 51 |
} |
| 52 |
|
| 53 |
type errMeter struct { |
| 54 |
mapi.Meter |
| 55 |
|
| 56 |
err error |
| 57 |
} |
| 58 |
|
| 59 |
func (m *errMeter) Int64UpDownCounter(string, ...mapi.Int64UpDownCounterOption) (mapi.Int64UpDownCounter, error) { |
| 60 |
return nil, m.err |
| 61 |
} |
| 62 |
|
| 63 |
func (m *errMeter) Int64Counter(string, ...mapi.Int64CounterOption) (mapi.Int64Counter, error) { |
| 64 |
return nil, m.err |
| 65 |
} |
| 66 |
|
| 67 |
func (m *errMeter) Float64Histogram(string, ...mapi.Float64HistogramOption) (mapi.Float64Histogram, error) { |
| 68 |
return nil, m.err |
| 69 |
} |
| 70 |
|
| 71 |
func TestNewInstrumentationObservabilityErrors(t *testing.T) { |
| 72 |
orig := otel.GetMeterProvider() |
| 73 |
t.Cleanup(func() { otel.SetMeterProvider(orig) }) |
| 74 |
mp := &errMeterProvider{err: assert.AnError} |
| 75 |
otel.SetMeterProvider(mp) |
| 76 |
|
| 77 |
t.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 78 |
|
| 79 |
_, err := observ.NewInstrumentation(ID, Target) |
| 80 |
require.ErrorIs(t, err, assert.AnError, "new instrument errors") |
| 81 |
|
| 82 |
assert.ErrorContains(t, err, "inflight metric") |
| 83 |
assert.ErrorContains(t, err, "span exported metric") |
| 84 |
assert.ErrorContains(t, err, "operation duration metric") |
| 85 |
} |
| 86 |
|
| 87 |
func TestNewInstrumentationObservabilityDisabled(t *testing.T) { |
| 88 |
// Do not set OTEL_GO_X_OBSERVABILITY. |
| 89 |
got, err := observ.NewInstrumentation(ID, Target) |
| 90 |
assert.NoError(t, err) |
| 91 |
assert.Nil(t, got) |
| 92 |
} |
| 93 |
|
| 94 |
func setup(t *testing.T) (*observ.Instrumentation, func() metricdata.ScopeMetrics) { |
| 95 |
t.Helper() |
| 96 |
|
| 97 |
t.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 98 |
|
| 99 |
original := otel.GetMeterProvider() |
| 100 |
t.Cleanup(func() { otel.SetMeterProvider(original) }) |
| 101 |
|
| 102 |
r := metric.NewManualReader() |
| 103 |
mp := metric.NewMeterProvider(metric.WithReader(r)) |
| 104 |
otel.SetMeterProvider(mp) |
| 105 |
|
| 106 |
inst, err := observ.NewInstrumentation(ID, Target) |
| 107 |
require.NoError(t, err) |
| 108 |
require.NotNil(t, inst) |
| 109 |
|
| 110 |
return inst, func() metricdata.ScopeMetrics { |
| 111 |
var rm metricdata.ResourceMetrics |
| 112 |
require.NoError(t, r.Collect(t.Context(), &rm)) |
| 113 |
|
| 114 |
require.Len(t, rm.ScopeMetrics, 1) |
| 115 |
return rm.ScopeMetrics[0] |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
func baseAttrs(err error) []attribute.KeyValue { |
| 120 |
attrs := []attribute.KeyValue{ |
| 121 |
semconv.OTelComponentName(observ.ComponentName(ID)), |
| 122 |
semconv.OTelComponentTypeOtlpGRPCSpanExporter, |
| 123 |
semconv.ServerAddress(ServerAddr), |
| 124 |
semconv.ServerPort(ServerPort), |
| 125 |
} |
| 126 |
if err != nil { |
| 127 |
attrs = append(attrs, semconv.ErrorType(err)) |
| 128 |
} |
| 129 |
return attrs |
| 130 |
} |
| 131 |
|
| 132 |
func set(err error) attribute.Set { |
| 133 |
return attribute.NewSet(baseAttrs(err)...) |
| 134 |
} |
| 135 |
|
| 136 |
func spanInflight() metricdata.Metrics { |
| 137 |
return metricdata.Metrics{ |
| 138 |
Name: otelconv.SDKExporterSpanInflight{}.Name(), |
| 139 |
Description: otelconv.SDKExporterSpanInflight{}.Description(), |
| 140 |
Unit: otelconv.SDKExporterSpanInflight{}.Unit(), |
| 141 |
Data: metricdata.Sum[int64]{ |
| 142 |
Temporality: metricdata.CumulativeTemporality, |
| 143 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 144 |
{Attributes: set(nil), Value: 0}, |
| 145 |
}, |
| 146 |
}, |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
func spanExported(success, total int64, err error) metricdata.Metrics { |
| 151 |
dp := []metricdata.DataPoint[int64]{ |
| 152 |
{Attributes: set(nil), Value: success}, |
| 153 |
} |
| 154 |
if err != nil { |
| 155 |
dp = append(dp, metricdata.DataPoint[int64]{ |
| 156 |
Attributes: set(err), |
| 157 |
Value: total - success, |
| 158 |
}) |
| 159 |
} |
| 160 |
return metricdata.Metrics{ |
| 161 |
Name: otelconv.SDKExporterSpanExported{}.Name(), |
| 162 |
Description: otelconv.SDKExporterSpanExported{}.Description(), |
| 163 |
Unit: otelconv.SDKExporterSpanExported{}.Unit(), |
| 164 |
Data: metricdata.Sum[int64]{ |
| 165 |
Temporality: metricdata.CumulativeTemporality, |
| 166 |
IsMonotonic: true, |
| 167 |
DataPoints: dp, |
| 168 |
}, |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
func operationDuration(err error) metricdata.Metrics { |
| 173 |
rpcSet := func(err error) attribute.Set { |
| 174 |
c := status.Code(err) |
| 175 |
return attribute.NewSet(append( |
| 176 |
[]attribute.KeyValue{ |
| 177 |
semconv.RPCResponseStatusCode(c.String()), |
| 178 |
}, |
| 179 |
baseAttrs(err)..., |
| 180 |
)...) |
| 181 |
} |
| 182 |
return metricdata.Metrics{ |
| 183 |
Name: otelconv.SDKExporterOperationDuration{}.Name(), |
| 184 |
Description: otelconv.SDKExporterOperationDuration{}.Description(), |
| 185 |
Unit: otelconv.SDKExporterOperationDuration{}.Unit(), |
| 186 |
Data: metricdata.Histogram[float64]{ |
| 187 |
Temporality: metricdata.CumulativeTemporality, |
| 188 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 189 |
{Attributes: rpcSet(err)}, |
| 190 |
}, |
| 191 |
}, |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
func assertMetrics(t *testing.T, got metricdata.ScopeMetrics, spans, success int64, err error) { |
| 196 |
t.Helper() |
| 197 |
|
| 198 |
assert.Equal(t, Scope, got.Scope, "unexpected scope") |
| 199 |
|
| 200 |
m := got.Metrics |
| 201 |
require.Len(t, m, 3, "expected 3 metrics") |
| 202 |
|
| 203 |
o := metricdatatest.IgnoreTimestamp() |
| 204 |
want := spanInflight() |
| 205 |
metricdatatest.AssertEqual(t, want, m[0], o) |
| 206 |
|
| 207 |
want = spanExported(success, spans, err) |
| 208 |
metricdatatest.AssertEqual(t, want, m[1], o) |
| 209 |
|
| 210 |
want = operationDuration(err) |
| 211 |
metricdatatest.AssertEqual(t, want, m[2], o, metricdatatest.IgnoreValue()) |
| 212 |
} |
| 213 |
|
| 214 |
func TestInstrumentationExportSpans(t *testing.T) { |
| 215 |
inst, collect := setup(t) |
| 216 |
|
| 217 |
const n = 10 |
| 218 |
inst.ExportSpans(t.Context(), n).End(nil, codes.OK) |
| 219 |
|
| 220 |
assertMetrics(t, collect(), n, n, nil) |
| 221 |
} |
| 222 |
|
| 223 |
func TestInstrumentationExportSpansAllErrored(t *testing.T) { |
| 224 |
inst, collect := setup(t) |
| 225 |
|
| 226 |
const n = 10 |
| 227 |
c := codes.PermissionDenied |
| 228 |
err := status.Error(c, "go away") |
| 229 |
inst.ExportSpans(t.Context(), n).End(err, c) |
| 230 |
|
| 231 |
const success = 0 |
| 232 |
assertMetrics(t, collect(), n, success, err) |
| 233 |
} |
| 234 |
|
| 235 |
func TestInstrumentationExportSpansPartialErrored(t *testing.T) { |
| 236 |
inst, collect := setup(t) |
| 237 |
|
| 238 |
const n = 10 |
| 239 |
const success = n - 5 |
| 240 |
|
| 241 |
c := codes.Unavailable |
| 242 |
err := status.Error(c, "temporary failure") |
| 243 |
err = errors.Join(err, &internal.PartialSuccess{RejectedItems: 5}) |
| 244 |
inst.ExportSpans(t.Context(), n).End(err, c) |
| 245 |
|
| 246 |
assertMetrics(t, collect(), n, success, err) |
| 247 |
} |
| 248 |
|
| 249 |
func TestInstrumentationExportSpansInvalidPartialErrored(t *testing.T) { |
| 250 |
inst, collect := setup(t) |
| 251 |
|
| 252 |
const n = 10 |
| 253 |
pErr := &internal.PartialSuccess{RejectedItems: -5} |
| 254 |
c := codes.Unavailable |
| 255 |
err := errors.Join(status.Error(c, "temporary"), pErr) |
| 256 |
inst.ExportSpans(t.Context(), n).End(err, c) |
| 257 |
|
| 258 |
// Round -5 to 0. |
| 259 |
success := int64(n) // (n - 0) |
| 260 |
assertMetrics(t, collect(), n, success, err) |
| 261 |
|
| 262 |
// Note: the metrics are cumulative, so account for the previous |
| 263 |
// ExportSpans call. |
| 264 |
pErr.RejectedItems = n + 5 |
| 265 |
inst.ExportSpans(t.Context(), n).End(err, c) |
| 266 |
|
| 267 |
// Round n+5 to n. |
| 268 |
success += 0 // success + (n - n) |
| 269 |
assertMetrics(t, collect(), n+n, success, err) |
| 270 |
} |
| 271 |
|
| 272 |
func TestBaseAttrs(t *testing.T) { |
| 273 |
tests := []struct { |
| 274 |
name string |
| 275 |
target string |
| 276 |
want []attribute.KeyValue |
| 277 |
}{ |
| 278 |
{ |
| 279 |
name: "HostAndPort", |
| 280 |
target: "dns://localhost:4317", |
| 281 |
want: []attribute.KeyValue{ |
| 282 |
semconv.OTelComponentName(observ.ComponentName(ID)), |
| 283 |
semconv.OTelComponentTypeOtlpGRPCSpanExporter, |
| 284 |
semconv.ServerAddress("localhost"), |
| 285 |
semconv.ServerPort(4317), |
| 286 |
}, |
| 287 |
}, |
| 288 |
{ |
| 289 |
name: "Host", |
| 290 |
target: "dns://localhost", |
| 291 |
want: []attribute.KeyValue{ |
| 292 |
semconv.OTelComponentName(observ.ComponentName(ID)), |
| 293 |
semconv.OTelComponentTypeOtlpGRPCSpanExporter, |
| 294 |
semconv.ServerAddress("localhost"), |
| 295 |
}, |
| 296 |
}, |
| 297 |
{ |
| 298 |
name: "Port", |
| 299 |
target: "dns://:4317", |
| 300 |
want: []attribute.KeyValue{ |
| 301 |
semconv.OTelComponentName(observ.ComponentName(ID)), |
| 302 |
semconv.OTelComponentTypeOtlpGRPCSpanExporter, |
| 303 |
semconv.ServerPort(4317), |
| 304 |
}, |
| 305 |
}, |
| 306 |
{ |
| 307 |
name: "Empty", |
| 308 |
target: "", |
| 309 |
want: []attribute.KeyValue{ |
| 310 |
semconv.OTelComponentName(observ.ComponentName(ID)), |
| 311 |
semconv.OTelComponentTypeOtlpGRPCSpanExporter, |
| 312 |
}, |
| 313 |
}, |
| 314 |
{ |
| 315 |
name: "Invalid", |
| 316 |
target: "dns:///:invalid", |
| 317 |
want: []attribute.KeyValue{ |
| 318 |
semconv.OTelComponentName(observ.ComponentName(ID)), |
| 319 |
semconv.OTelComponentTypeOtlpGRPCSpanExporter, |
| 320 |
}, |
| 321 |
}, |
| 322 |
} |
| 323 |
for _, tt := range tests { |
| 324 |
t.Run(tt.name, func(t *testing.T) { |
| 325 |
got := observ.BaseAttrs(ID, tt.target) |
| 326 |
assert.Equal(t, tt.want, got) |
| 327 |
}) |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
func BenchmarkInstrumentationExportSpans(b *testing.B) { |
| 332 |
setup := func(b *testing.B) *observ.Instrumentation { |
| 333 |
b.Helper() |
| 334 |
b.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 335 |
inst, err := observ.NewInstrumentation(ID, Target) |
| 336 |
if err != nil { |
| 337 |
b.Fatalf("failed to create instrumentation: %v", err) |
| 338 |
} |
| 339 |
return inst |
| 340 |
} |
| 341 |
|
| 342 |
run := func(err error, c codes.Code) func(*testing.B) { |
| 343 |
return func(b *testing.B) { |
| 344 |
inst := setup(b) |
| 345 |
b.ReportAllocs() |
| 346 |
b.ResetTimer() |
| 347 |
for b.Loop() { |
| 348 |
inst.ExportSpans(b.Context(), 10).End(err, c) |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
b.Run("NoError", run(nil, codes.OK)) |
| 354 |
err := &internal.PartialSuccess{RejectedItems: 6} |
| 355 |
b.Run("PartialError", run(err, codes.Unavailable)) |
| 356 |
b.Run("FullError", run(assert.AnError, codes.Aborted)) |
| 357 |
} |