| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otlptracegrpc_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
"net" |
| 11 |
"strings" |
| 12 |
"testing" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/stretchr/testify/assert" |
| 16 |
"github.com/stretchr/testify/require" |
| 17 |
coltracepb "go.opentelemetry.io/proto/otlp/collector/trace/v1" |
| 18 |
commonpb "go.opentelemetry.io/proto/otlp/common/v1" |
| 19 |
"go.uber.org/goleak" |
| 20 |
"google.golang.org/grpc" |
| 21 |
"google.golang.org/grpc/backoff" |
| 22 |
"google.golang.org/grpc/codes" |
| 23 |
"google.golang.org/grpc/credentials/insecure" |
| 24 |
"google.golang.org/grpc/encoding/gzip" |
| 25 |
"google.golang.org/grpc/metadata" |
| 26 |
"google.golang.org/grpc/status" |
| 27 |
|
| 28 |
"go.opentelemetry.io/otel" |
| 29 |
"go.opentelemetry.io/otel/attribute" |
| 30 |
"go.opentelemetry.io/otel/exporters/otlp/otlptrace" |
| 31 |
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" |
| 32 |
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc/internal" |
| 33 |
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc/internal/counter" |
| 34 |
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc/internal/observ" |
| 35 |
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc/internal/otlptracetest" |
| 36 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 37 |
"go.opentelemetry.io/otel/sdk/metric" |
| 38 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 39 |
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" |
| 40 |
sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 41 |
"go.opentelemetry.io/otel/sdk/trace/tracetest" |
| 42 |
"go.opentelemetry.io/otel/semconv/v1.40.0/otelconv" |
| 43 |
) |
| 44 |
|
| 45 |
func TestMain(m *testing.M) { |
| 46 |
goleak.VerifyTestMain(m) |
| 47 |
} |
| 48 |
|
| 49 |
var roSpans = tracetest.SpanStubs{{Name: "Span 0"}}.Snapshots() |
| 50 |
|
| 51 |
func contextWithTimeout( |
| 52 |
parent context.Context, |
| 53 |
t *testing.T, |
| 54 |
timeout time.Duration, |
| 55 |
) (context.Context, context.CancelFunc) { |
| 56 |
d, ok := t.Deadline() |
| 57 |
if !ok { |
| 58 |
d = time.Now().Add(timeout) |
| 59 |
} else { |
| 60 |
d = d.Add(-1 * time.Millisecond) |
| 61 |
now := time.Now() |
| 62 |
if d.Sub(now) > timeout { |
| 63 |
d = now.Add(timeout) |
| 64 |
} |
| 65 |
} |
| 66 |
return context.WithDeadline(parent, d) |
| 67 |
} |
| 68 |
|
| 69 |
func TestNewEndToEnd(t *testing.T) { |
| 70 |
tests := []struct { |
| 71 |
name string |
| 72 |
additionalOpts []otlptracegrpc.Option |
| 73 |
}{ |
| 74 |
{ |
| 75 |
name: "StandardExporter", |
| 76 |
}, |
| 77 |
{ |
| 78 |
name: "WithCompressor", |
| 79 |
additionalOpts: []otlptracegrpc.Option{ |
| 80 |
otlptracegrpc.WithCompressor(gzip.Name), |
| 81 |
}, |
| 82 |
}, |
| 83 |
{ |
| 84 |
name: "WithServiceConfig", |
| 85 |
additionalOpts: []otlptracegrpc.Option{ |
| 86 |
otlptracegrpc.WithServiceConfig("{}"), |
| 87 |
}, |
| 88 |
}, |
| 89 |
{ |
| 90 |
name: "WithDialOptions", |
| 91 |
additionalOpts: []otlptracegrpc.Option{ |
| 92 |
otlptracegrpc.WithDialOption( |
| 93 |
grpc.WithConnectParams(grpc.ConnectParams{ |
| 94 |
Backoff: backoff.DefaultConfig, |
| 95 |
MinConnectTimeout: time.Second, |
| 96 |
})), |
| 97 |
}, |
| 98 |
}, |
| 99 |
} |
| 100 |
|
| 101 |
for _, test := range tests { |
| 102 |
t.Run(test.name, func(t *testing.T) { |
| 103 |
newExporterEndToEndTest(t, test.additionalOpts) |
| 104 |
}) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
func TestWithEndpointURL(t *testing.T) { |
| 109 |
mc := runMockCollector(t) |
| 110 |
|
| 111 |
ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup. |
| 112 |
exp := newGRPCExporter(t, ctx, "", []otlptracegrpc.Option{ |
| 113 |
otlptracegrpc.WithEndpointURL("http://" + mc.endpoint), |
| 114 |
}...) |
| 115 |
t.Cleanup(func() { |
| 116 |
ctx, cancel := contextWithTimeout(ctx, t, 10*time.Second) |
| 117 |
defer cancel() |
| 118 |
|
| 119 |
require.NoError(t, exp.Shutdown(ctx)) |
| 120 |
}) |
| 121 |
|
| 122 |
// RunEndToEndTest closes mc. |
| 123 |
otlptracetest.RunEndToEndTest(ctx, t, exp, mc) |
| 124 |
} |
| 125 |
|
| 126 |
func newGRPCExporter( |
| 127 |
tb testing.TB, |
| 128 |
ctx context.Context, |
| 129 |
endpoint string, |
| 130 |
additionalOpts ...otlptracegrpc.Option, |
| 131 |
) *otlptrace.Exporter { |
| 132 |
opts := []otlptracegrpc.Option{ |
| 133 |
otlptracegrpc.WithInsecure(), |
| 134 |
otlptracegrpc.WithEndpoint(endpoint), |
| 135 |
otlptracegrpc.WithReconnectionPeriod(50 * time.Millisecond), |
| 136 |
} |
| 137 |
|
| 138 |
opts = append(opts, additionalOpts...) |
| 139 |
client := otlptracegrpc.NewClient(opts...) |
| 140 |
exp, err := otlptrace.New(ctx, client) |
| 141 |
if err != nil { |
| 142 |
tb.Fatalf("failed to create a new collector exporter: %v", err) |
| 143 |
} |
| 144 |
return exp |
| 145 |
} |
| 146 |
|
| 147 |
func newExporterEndToEndTest(t *testing.T, additionalOpts []otlptracegrpc.Option) { |
| 148 |
mc := runMockCollector(t) |
| 149 |
|
| 150 |
ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup. |
| 151 |
exp := newGRPCExporter(t, ctx, mc.endpoint, additionalOpts...) |
| 152 |
t.Cleanup(func() { |
| 153 |
ctx, cancel := contextWithTimeout(ctx, t, 10*time.Second) |
| 154 |
defer cancel() |
| 155 |
|
| 156 |
require.NoError(t, exp.Shutdown(ctx)) |
| 157 |
}) |
| 158 |
|
| 159 |
// RunEndToEndTest closes mc. |
| 160 |
otlptracetest.RunEndToEndTest(ctx, t, exp, mc) |
| 161 |
} |
| 162 |
|
| 163 |
func TestExporterShutdown(t *testing.T) { |
| 164 |
mc := runMockCollectorAtEndpoint(t, "localhost:0") |
| 165 |
t.Cleanup(func() { require.NoError(t, mc.stop()) }) |
| 166 |
|
| 167 |
factory := func() otlptrace.Client { |
| 168 |
return otlptracegrpc.NewClient( |
| 169 |
otlptracegrpc.WithEndpoint(mc.endpoint), |
| 170 |
otlptracegrpc.WithInsecure(), |
| 171 |
) |
| 172 |
} |
| 173 |
otlptracetest.RunExporterShutdownTest(t, factory) |
| 174 |
} |
| 175 |
|
| 176 |
func TestNewInvokeStartThenStopManyTimes(t *testing.T) { |
| 177 |
mc := runMockCollector(t) |
| 178 |
t.Cleanup(func() { require.NoError(t, mc.stop()) }) |
| 179 |
|
| 180 |
ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup. |
| 181 |
exp := newGRPCExporter(t, ctx, mc.endpoint) |
| 182 |
t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) }) |
| 183 |
|
| 184 |
// Invoke Start numerous times, should return errAlreadyStarted |
| 185 |
for i := range 10 { |
| 186 |
if err := exp.Start(ctx); err == nil || !strings.Contains(err.Error(), "already started") { |
| 187 |
t.Fatalf("#%d unexpected Start error: %v", i, err) |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
if err := exp.Shutdown(ctx); err != nil { |
| 192 |
t.Fatalf("failed to Shutdown the exporter: %v", err) |
| 193 |
} |
| 194 |
// Invoke Shutdown numerous times |
| 195 |
for i := range 10 { |
| 196 |
if err := exp.Shutdown(ctx); err != nil { |
| 197 |
t.Fatalf(`#%d got error (%v) expected none`, i, err) |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
// This test takes a long time to run: to skip it, run tests using: -short. |
| 203 |
func TestNewCollectorOnBadConnection(t *testing.T) { |
| 204 |
if testing.Short() { |
| 205 |
t.Skipf("Skipping this long running test") |
| 206 |
} |
| 207 |
|
| 208 |
ln, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "localhost:0") |
| 209 |
if err != nil { |
| 210 |
t.Fatalf("Failed to grab an available port: %v", err) |
| 211 |
} |
| 212 |
// Firstly close the "collector's" channel: optimistically this endpoint won't get reused ASAP |
| 213 |
// However, our goal of closing it is to simulate an unavailable connection |
| 214 |
_ = ln.Close() |
| 215 |
|
| 216 |
_, collectorPortStr, _ := net.SplitHostPort(ln.Addr().String()) |
| 217 |
|
| 218 |
endpoint := fmt.Sprintf("localhost:%s", collectorPortStr) |
| 219 |
ctx := t.Context() |
| 220 |
exp := newGRPCExporter(t, ctx, endpoint) |
| 221 |
require.NoError(t, exp.Shutdown(ctx)) |
| 222 |
} |
| 223 |
|
| 224 |
func TestNewWithEndpoint(t *testing.T) { |
| 225 |
mc := runMockCollector(t) |
| 226 |
t.Cleanup(func() { require.NoError(t, mc.stop()) }) |
| 227 |
|
| 228 |
ctx := t.Context() |
| 229 |
exp := newGRPCExporter(t, ctx, mc.endpoint) |
| 230 |
require.NoError(t, exp.Shutdown(ctx)) |
| 231 |
} |
| 232 |
|
| 233 |
func TestNewWithHeaders(t *testing.T) { |
| 234 |
mc := runMockCollector(t) |
| 235 |
t.Cleanup(func() { require.NoError(t, mc.stop()) }) |
| 236 |
|
| 237 |
ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup. |
| 238 |
additionalKey := "additional-custom-header" |
| 239 |
ctx = metadata.AppendToOutgoingContext(ctx, additionalKey, "additional-value") |
| 240 |
exp := newGRPCExporter(t, ctx, mc.endpoint, |
| 241 |
otlptracegrpc.WithHeaders(map[string]string{"header1": "value1"})) |
| 242 |
t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) }) |
| 243 |
require.NoError(t, exp.ExportSpans(ctx, roSpans)) |
| 244 |
|
| 245 |
headers := mc.getHeaders() |
| 246 |
require.Regexp(t, "OTel OTLP Exporter Go/1\\..*", headers.Get("user-agent")) |
| 247 |
require.Len(t, headers.Get("header1"), 1) |
| 248 |
require.Len(t, headers.Get(additionalKey), 1) |
| 249 |
assert.Equal(t, "value1", headers.Get("header1")[0]) |
| 250 |
} |
| 251 |
|
| 252 |
func TestExportSpansTimeoutHonored(t *testing.T) { |
| 253 |
//nolint:usetesting // required to avoid getting a canceled context at cleanup. |
| 254 |
ctx, cancel := contextWithTimeout(context.Background(), t, 1*time.Minute) |
| 255 |
t.Cleanup(cancel) |
| 256 |
|
| 257 |
mc := runMockCollector(t) |
| 258 |
exportBlock := make(chan struct{}) |
| 259 |
mc.traceSvc.exportBlock = exportBlock |
| 260 |
t.Cleanup(func() { require.NoError(t, mc.stop()) }) |
| 261 |
|
| 262 |
exp := newGRPCExporter( |
| 263 |
t, |
| 264 |
ctx, |
| 265 |
mc.endpoint, |
| 266 |
otlptracegrpc.WithTimeout(1*time.Nanosecond), |
| 267 |
otlptracegrpc.WithRetry(otlptracegrpc.RetryConfig{Enabled: false}), |
| 268 |
) |
| 269 |
t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) }) |
| 270 |
|
| 271 |
err := exp.ExportSpans(ctx, roSpans) |
| 272 |
// Release the export so everything is cleaned up on shutdown. |
| 273 |
close(exportBlock) |
| 274 |
|
| 275 |
unwrapped := errors.Unwrap(err) |
| 276 |
require.Equal(t, codes.DeadlineExceeded, status.Convert(unwrapped).Code()) |
| 277 |
require.True(t, strings.HasPrefix(err.Error(), "traces export: "), "%+v", err) |
| 278 |
} |
| 279 |
|
| 280 |
func TestNewWithMultipleAttributeTypes(t *testing.T) { |
| 281 |
mc := runMockCollector(t) |
| 282 |
|
| 283 |
//nolint:usetesting // required to avoid getting a canceled context at cleanup. |
| 284 |
ctx, cancel := contextWithTimeout(context.Background(), t, 10*time.Second) |
| 285 |
t.Cleanup(cancel) |
| 286 |
|
| 287 |
exp := newGRPCExporter(t, ctx, mc.endpoint) |
| 288 |
t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) }) |
| 289 |
|
| 290 |
tp := sdktrace.NewTracerProvider( |
| 291 |
sdktrace.WithSampler(sdktrace.AlwaysSample()), |
| 292 |
sdktrace.WithBatcher( |
| 293 |
exp, |
| 294 |
// add following two options to ensure flush |
| 295 |
sdktrace.WithBatchTimeout(5*time.Second), |
| 296 |
sdktrace.WithMaxExportBatchSize(10), |
| 297 |
), |
| 298 |
) |
| 299 |
t.Cleanup(func() { require.NoError(t, tp.Shutdown(ctx)) }) |
| 300 |
|
| 301 |
tr := tp.Tracer("test-tracer") |
| 302 |
testKvs := []attribute.KeyValue{ |
| 303 |
attribute.Int("Int", 1), |
| 304 |
attribute.Int64("Int64", int64(3)), |
| 305 |
attribute.Float64("Float64", 2.22), |
| 306 |
attribute.Bool("Bool", true), |
| 307 |
attribute.String("String", "test"), |
| 308 |
} |
| 309 |
_, span := tr.Start(ctx, "AlwaysSample") |
| 310 |
span.SetAttributes(testKvs...) |
| 311 |
span.End() |
| 312 |
|
| 313 |
// Flush and close. |
| 314 |
func() { |
| 315 |
ctx, cancel := contextWithTimeout(ctx, t, 10*time.Second) |
| 316 |
defer cancel() |
| 317 |
require.NoError(t, tp.Shutdown(ctx)) |
| 318 |
}() |
| 319 |
|
| 320 |
// Wait >2 cycles. |
| 321 |
<-time.After(40 * time.Millisecond) |
| 322 |
|
| 323 |
// Now shutdown the exporter |
| 324 |
require.NoError(t, exp.Shutdown(ctx)) |
| 325 |
|
| 326 |
// Shutdown the collector too so that we can begin |
| 327 |
// verification checks of expected data back. |
| 328 |
require.NoError(t, mc.stop()) |
| 329 |
|
| 330 |
// Now verify that we only got one span |
| 331 |
rss := mc.getSpans() |
| 332 |
if got, want := len(rss), 1; got != want { |
| 333 |
t.Fatalf("resource span count: got %d, want %d\n", got, want) |
| 334 |
} |
| 335 |
|
| 336 |
expected := []*commonpb.KeyValue{ |
| 337 |
{ |
| 338 |
Key: "Int", |
| 339 |
Value: &commonpb.AnyValue{ |
| 340 |
Value: &commonpb.AnyValue_IntValue{ |
| 341 |
IntValue: 1, |
| 342 |
}, |
| 343 |
}, |
| 344 |
}, |
| 345 |
{ |
| 346 |
Key: "Int64", |
| 347 |
Value: &commonpb.AnyValue{ |
| 348 |
Value: &commonpb.AnyValue_IntValue{ |
| 349 |
IntValue: 3, |
| 350 |
}, |
| 351 |
}, |
| 352 |
}, |
| 353 |
{ |
| 354 |
Key: "Float64", |
| 355 |
Value: &commonpb.AnyValue{ |
| 356 |
Value: &commonpb.AnyValue_DoubleValue{ |
| 357 |
DoubleValue: 2.22, |
| 358 |
}, |
| 359 |
}, |
| 360 |
}, |
| 361 |
{ |
| 362 |
Key: "Bool", |
| 363 |
Value: &commonpb.AnyValue{ |
| 364 |
Value: &commonpb.AnyValue_BoolValue{ |
| 365 |
BoolValue: true, |
| 366 |
}, |
| 367 |
}, |
| 368 |
}, |
| 369 |
{ |
| 370 |
Key: "String", |
| 371 |
Value: &commonpb.AnyValue{ |
| 372 |
Value: &commonpb.AnyValue_StringValue{ |
| 373 |
StringValue: "test", |
| 374 |
}, |
| 375 |
}, |
| 376 |
}, |
| 377 |
} |
| 378 |
|
| 379 |
// Verify attributes |
| 380 |
if !assert.Len(t, rss[0].Attributes, len(expected)) { |
| 381 |
t.Fatalf("attributes count: got %d, want %d\n", len(rss[0].Attributes), len(expected)) |
| 382 |
} |
| 383 |
for i, actual := range rss[0].Attributes { |
| 384 |
if a, ok := actual.Value.Value.(*commonpb.AnyValue_DoubleValue); ok { |
| 385 |
e, ok := expected[i].Value.Value.(*commonpb.AnyValue_DoubleValue) |
| 386 |
if !ok { |
| 387 |
t.Errorf("expected AnyValue_DoubleValue, got %T", expected[i].Value.Value) |
| 388 |
continue |
| 389 |
} |
| 390 |
if !assert.InDelta(t, e.DoubleValue, a.DoubleValue, 0.01) { |
| 391 |
continue |
| 392 |
} |
| 393 |
e.DoubleValue = a.DoubleValue |
| 394 |
} |
| 395 |
assert.Equal(t, expected[i], actual) |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
func TestEmptyData(t *testing.T) { |
| 400 |
mc := runMockCollector(t) |
| 401 |
t.Cleanup(func() { require.NoError(t, mc.stop()) }) |
| 402 |
|
| 403 |
ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup. |
| 404 |
exp := newGRPCExporter(t, ctx, mc.endpoint) |
| 405 |
t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) }) |
| 406 |
|
| 407 |
assert.NoError(t, exp.ExportSpans(ctx, nil)) |
| 408 |
} |
| 409 |
|
| 410 |
func TestPartialSuccess(t *testing.T) { |
| 411 |
mc := runMockCollectorWithConfig(t, &mockConfig{ |
| 412 |
partial: &coltracepb.ExportTracePartialSuccess{ |
| 413 |
RejectedSpans: 2, |
| 414 |
ErrorMessage: "partially successful", |
| 415 |
}, |
| 416 |
}) |
| 417 |
t.Cleanup(func() { require.NoError(t, mc.stop()) }) |
| 418 |
|
| 419 |
ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup. |
| 420 |
exp := newGRPCExporter(t, ctx, mc.endpoint) |
| 421 |
t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) }) |
| 422 |
|
| 423 |
err := exp.ExportSpans(ctx, roSpans) |
| 424 |
want := internal.TracePartialSuccessError(0, "") |
| 425 |
assert.ErrorIs(t, err, want) |
| 426 |
} |
| 427 |
|
| 428 |
func TestCustomUserAgent(t *testing.T) { |
| 429 |
customUserAgent := "custom-user-agent" |
| 430 |
mc := runMockCollector(t) |
| 431 |
t.Cleanup(func() { require.NoError(t, mc.stop()) }) |
| 432 |
|
| 433 |
ctx := context.Background() //nolint:usetesting // required to avoid getting a canceled context at cleanup. |
| 434 |
exp := newGRPCExporter(t, ctx, mc.endpoint, |
| 435 |
otlptracegrpc.WithDialOption(grpc.WithUserAgent(customUserAgent))) |
| 436 |
t.Cleanup(func() { require.NoError(t, exp.Shutdown(ctx)) }) |
| 437 |
require.NoError(t, exp.ExportSpans(ctx, roSpans)) |
| 438 |
|
| 439 |
headers := mc.getHeaders() |
| 440 |
require.Contains(t, headers.Get("user-agent")[0], customUserAgent) |
| 441 |
} |
| 442 |
|
| 443 |
func TestClientInstrumentation(t *testing.T) { |
| 444 |
// Enable instrumentation for this test. |
| 445 |
t.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 446 |
|
| 447 |
// Reset client ID to be deterministic |
| 448 |
const id = 0 |
| 449 |
counter.SetExporterID(id) |
| 450 |
|
| 451 |
// Save original meter provider and restore at end of test. |
| 452 |
orig := otel.GetMeterProvider() |
| 453 |
t.Cleanup(func() { otel.SetMeterProvider(orig) }) |
| 454 |
|
| 455 |
// Create a new meter provider to capture metrics. |
| 456 |
reader := metric.NewManualReader() |
| 457 |
mp := metric.NewMeterProvider(metric.WithReader(reader)) |
| 458 |
otel.SetMeterProvider(mp) |
| 459 |
|
| 460 |
const n, msg = 2, "partially successful" |
| 461 |
mc := runMockCollectorWithConfig(t, &mockConfig{ |
| 462 |
endpoint: "localhost:0", // Determine canonical endpoint. |
| 463 |
partial: &coltracepb.ExportTracePartialSuccess{ |
| 464 |
RejectedSpans: n, |
| 465 |
ErrorMessage: msg, |
| 466 |
}, |
| 467 |
}) |
| 468 |
t.Cleanup(func() { require.NoError(t, mc.stop()) }) |
| 469 |
|
| 470 |
exp := newGRPCExporter(t, t.Context(), mc.endpoint) |
| 471 |
err := exp.ExportSpans(t.Context(), roSpans) |
| 472 |
assert.ErrorIs(t, err, internal.TracePartialSuccessError(n, msg)) |
| 473 |
require.NoError(t, exp.Shutdown(t.Context())) |
| 474 |
|
| 475 |
var got metricdata.ResourceMetrics |
| 476 |
require.NoError(t, reader.Collect(t.Context(), &got)) |
| 477 |
|
| 478 |
attrs := observ.BaseAttrs(id, canonical(t, mc.endpoint)) |
| 479 |
want := metricdata.ScopeMetrics{ |
| 480 |
Scope: instrumentation.Scope{ |
| 481 |
Name: observ.ScopeName, |
| 482 |
Version: observ.Version, |
| 483 |
SchemaURL: observ.SchemaURL, |
| 484 |
}, |
| 485 |
Metrics: []metricdata.Metrics{ |
| 486 |
{ |
| 487 |
Name: otelconv.SDKExporterSpanInflight{}.Name(), |
| 488 |
Description: otelconv.SDKExporterSpanInflight{}.Description(), |
| 489 |
Unit: otelconv.SDKExporterSpanInflight{}.Unit(), |
| 490 |
Data: metricdata.Sum[int64]{ |
| 491 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 492 |
{Attributes: attribute.NewSet(attrs...)}, |
| 493 |
}, |
| 494 |
Temporality: metricdata.CumulativeTemporality, |
| 495 |
}, |
| 496 |
}, |
| 497 |
{ |
| 498 |
Name: otelconv.SDKExporterSpanExported{}.Name(), |
| 499 |
Description: otelconv.SDKExporterSpanExported{}.Description(), |
| 500 |
Unit: otelconv.SDKExporterSpanExported{}.Unit(), |
| 501 |
Data: metricdata.Sum[int64]{ |
| 502 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 503 |
{Attributes: attribute.NewSet(attrs...)}, |
| 504 |
{Attributes: attribute.NewSet(append( |
| 505 |
attrs, |
| 506 |
otelconv.SDKExporterSpanExported{}.AttrErrorType("*errors.joinError"), |
| 507 |
)...)}, |
| 508 |
}, |
| 509 |
Temporality: 0x1, |
| 510 |
IsMonotonic: true, |
| 511 |
}, |
| 512 |
}, |
| 513 |
{ |
| 514 |
Name: otelconv.SDKExporterOperationDuration{}.Name(), |
| 515 |
Description: otelconv.SDKExporterOperationDuration{}.Description(), |
| 516 |
Unit: otelconv.SDKExporterOperationDuration{}.Unit(), |
| 517 |
Data: metricdata.Histogram[float64]{ |
| 518 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 519 |
{Attributes: attribute.NewSet(append( |
| 520 |
attrs, |
| 521 |
otelconv.SDKExporterOperationDuration{}.AttrErrorType("*errors.joinError"), |
| 522 |
otelconv.SDKExporterOperationDuration{}.AttrRPCResponseStatusCode( |
| 523 |
codes.OK.String(), |
| 524 |
), |
| 525 |
)...)}, |
| 526 |
}, |
| 527 |
Temporality: 0x1, |
| 528 |
}, |
| 529 |
}, |
| 530 |
}, |
| 531 |
} |
| 532 |
require.Len(t, got.ScopeMetrics, 1) |
| 533 |
opt := []metricdatatest.Option{ |
| 534 |
metricdatatest.IgnoreTimestamp(), |
| 535 |
metricdatatest.IgnoreExemplars(), |
| 536 |
metricdatatest.IgnoreValue(), |
| 537 |
} |
| 538 |
metricdatatest.AssertEqual(t, want, got.ScopeMetrics[0], opt...) |
| 539 |
} |
| 540 |
|
| 541 |
func canonical(t *testing.T, endpoint string) string { |
| 542 |
t.Helper() |
| 543 |
|
| 544 |
opt := grpc.WithTransportCredentials(insecure.NewCredentials()) |
| 545 |
c, err := grpc.NewClient(endpoint, opt) // Used to normaliz endpoint. |
| 546 |
if err != nil { |
| 547 |
t.Fatalf("failed to create grpc client: %v", err) |
| 548 |
} |
| 549 |
out := c.CanonicalTarget() |
| 550 |
_ = c.Close() |
| 551 |
|
| 552 |
return out |
| 553 |
} |
| 554 |
|
| 555 |
func BenchmarkExporterExportSpans(b *testing.B) { |
| 556 |
const n = 10 |
| 557 |
|
| 558 |
run := func(b *testing.B) { |
| 559 |
mc := runMockCollectorWithConfig(b, &mockConfig{ |
| 560 |
endpoint: "localhost:0", |
| 561 |
partial: &coltracepb.ExportTracePartialSuccess{ |
| 562 |
RejectedSpans: 5, |
| 563 |
ErrorMessage: "partially successful", |
| 564 |
}, |
| 565 |
}) |
| 566 |
b.Cleanup(func() { require.NoError(b, mc.stop()) }) |
| 567 |
|
| 568 |
exp := newGRPCExporter(b, b.Context(), mc.endpoint) |
| 569 |
b.Cleanup(func() { |
| 570 |
//nolint:usetesting // required to avoid getting a canceled context at cleanup. |
| 571 |
assert.NoError(b, exp.Shutdown(context.Background())) |
| 572 |
}) |
| 573 |
|
| 574 |
stubs := make([]tracetest.SpanStub, n) |
| 575 |
for i := range stubs { |
| 576 |
stubs[i].Name = fmt.Sprintf("Span %d", i) |
| 577 |
} |
| 578 |
spans := tracetest.SpanStubs(stubs).Snapshots() |
| 579 |
|
| 580 |
b.ReportAllocs() |
| 581 |
b.ResetTimer() |
| 582 |
|
| 583 |
var err error |
| 584 |
for b.Loop() { |
| 585 |
err = exp.ExportSpans(b.Context(), spans) |
| 586 |
} |
| 587 |
_ = err |
| 588 |
} |
| 589 |
|
| 590 |
b.Run("Observability", func(b *testing.B) { |
| 591 |
b.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 592 |
run(b) |
| 593 |
}) |
| 594 |
|
| 595 |
b.Run("NoObservability", func(b *testing.B) { |
| 596 |
b.Setenv("OTEL_GO_X_OBSERVABILITY", "false") |
| 597 |
run(b) |
| 598 |
}) |
| 599 |
} |