| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otlptracehttp_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"crypto/tls" |
| 9 |
"fmt" |
| 10 |
"io" |
| 11 |
"net/http" |
| 12 |
"net/http/httptest" |
| 13 |
"net/url" |
| 14 |
"strings" |
| 15 |
"sync" |
| 16 |
"testing" |
| 17 |
"time" |
| 18 |
|
| 19 |
"github.com/stretchr/testify/assert" |
| 20 |
"github.com/stretchr/testify/require" |
| 21 |
coltracepb "go.opentelemetry.io/proto/otlp/collector/trace/v1" |
| 22 |
|
| 23 |
"go.opentelemetry.io/otel" |
| 24 |
"go.opentelemetry.io/otel/attribute" |
| 25 |
"go.opentelemetry.io/otel/exporters/otlp/otlptrace" |
| 26 |
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" |
| 27 |
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp/internal" |
| 28 |
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp/internal/counter" |
| 29 |
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp/internal/observ" |
| 30 |
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp/internal/otlptracetest" |
| 31 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 32 |
"go.opentelemetry.io/otel/sdk/metric" |
| 33 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 34 |
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" |
| 35 |
"go.opentelemetry.io/otel/sdk/trace/tracetest" |
| 36 |
"go.opentelemetry.io/otel/semconv/v1.40.0/otelconv" |
| 37 |
) |
| 38 |
|
| 39 |
const ( |
| 40 |
relOtherTracesPath = "post/traces/here" |
| 41 |
otherTracesPath = "/post/traces/here" |
| 42 |
) |
| 43 |
|
| 44 |
var ( |
| 45 |
testHeaders = map[string]string{ |
| 46 |
"Otel-Go-Key-1": "somevalue", |
| 47 |
"Otel-Go-Key-2": "someothervalue", |
| 48 |
} |
| 49 |
|
| 50 |
customUserAgentHeader = map[string]string{ |
| 51 |
"user-agent": "custom-user-agent", |
| 52 |
} |
| 53 |
|
| 54 |
customProxyHeader = map[string]string{ |
| 55 |
"header-added-via-proxy": "proxy-value", |
| 56 |
} |
| 57 |
) |
| 58 |
|
| 59 |
func TestEndToEnd(t *testing.T) { |
| 60 |
tests := []struct { |
| 61 |
name string |
| 62 |
opts []otlptracehttp.Option |
| 63 |
mcCfg mockCollectorConfig |
| 64 |
tls bool |
| 65 |
withURLEndpoint bool |
| 66 |
}{ |
| 67 |
{ |
| 68 |
name: "no extra options", |
| 69 |
opts: nil, |
| 70 |
}, |
| 71 |
{ |
| 72 |
name: "with URL endpoint", |
| 73 |
withURLEndpoint: true, |
| 74 |
}, |
| 75 |
{ |
| 76 |
name: "with gzip compression", |
| 77 |
opts: []otlptracehttp.Option{ |
| 78 |
otlptracehttp.WithCompression(otlptracehttp.GzipCompression), |
| 79 |
}, |
| 80 |
}, |
| 81 |
{ |
| 82 |
name: "retry", |
| 83 |
opts: []otlptracehttp.Option{ |
| 84 |
otlptracehttp.WithRetry(otlptracehttp.RetryConfig{ |
| 85 |
Enabled: true, |
| 86 |
InitialInterval: time.Nanosecond, |
| 87 |
MaxInterval: time.Nanosecond, |
| 88 |
// Do not stop trying. |
| 89 |
MaxElapsedTime: 0, |
| 90 |
}), |
| 91 |
}, |
| 92 |
mcCfg: mockCollectorConfig{ |
| 93 |
InjectHTTPStatus: []int{503, 429}, |
| 94 |
}, |
| 95 |
}, |
| 96 |
{ |
| 97 |
name: "retry with gzip compression", |
| 98 |
opts: []otlptracehttp.Option{ |
| 99 |
otlptracehttp.WithCompression(otlptracehttp.GzipCompression), |
| 100 |
otlptracehttp.WithRetry(otlptracehttp.RetryConfig{ |
| 101 |
Enabled: true, |
| 102 |
InitialInterval: time.Nanosecond, |
| 103 |
MaxInterval: time.Nanosecond, |
| 104 |
// Do not stop trying. |
| 105 |
MaxElapsedTime: 0, |
| 106 |
}), |
| 107 |
}, |
| 108 |
mcCfg: mockCollectorConfig{ |
| 109 |
InjectHTTPStatus: []int{503, 502}, |
| 110 |
}, |
| 111 |
}, |
| 112 |
{ |
| 113 |
name: "retry with throttle", |
| 114 |
opts: []otlptracehttp.Option{ |
| 115 |
otlptracehttp.WithRetry(otlptracehttp.RetryConfig{ |
| 116 |
Enabled: true, |
| 117 |
InitialInterval: time.Nanosecond, |
| 118 |
MaxInterval: time.Nanosecond, |
| 119 |
// Do not stop trying. |
| 120 |
MaxElapsedTime: 0, |
| 121 |
}), |
| 122 |
}, |
| 123 |
mcCfg: mockCollectorConfig{ |
| 124 |
InjectHTTPStatus: []int{504}, |
| 125 |
InjectResponseHeader: []map[string]string{ |
| 126 |
{"Retry-After": "10"}, |
| 127 |
}, |
| 128 |
}, |
| 129 |
}, |
| 130 |
{ |
| 131 |
name: "with empty paths (forced to defaults)", |
| 132 |
opts: []otlptracehttp.Option{ |
| 133 |
otlptracehttp.WithURLPath(""), |
| 134 |
}, |
| 135 |
}, |
| 136 |
{ |
| 137 |
name: "with relative paths", |
| 138 |
opts: []otlptracehttp.Option{ |
| 139 |
otlptracehttp.WithURLPath(relOtherTracesPath), |
| 140 |
}, |
| 141 |
mcCfg: mockCollectorConfig{ |
| 142 |
TracesURLPath: otherTracesPath, |
| 143 |
}, |
| 144 |
}, |
| 145 |
{ |
| 146 |
name: "with TLS", |
| 147 |
opts: nil, |
| 148 |
mcCfg: mockCollectorConfig{ |
| 149 |
WithTLS: true, |
| 150 |
}, |
| 151 |
tls: true, |
| 152 |
}, |
| 153 |
{ |
| 154 |
name: "with extra headers", |
| 155 |
opts: []otlptracehttp.Option{ |
| 156 |
otlptracehttp.WithHeaders(testHeaders), |
| 157 |
}, |
| 158 |
mcCfg: mockCollectorConfig{ |
| 159 |
ExpectedHeaders: testHeaders, |
| 160 |
}, |
| 161 |
}, |
| 162 |
{ |
| 163 |
name: "with custom user agent", |
| 164 |
opts: []otlptracehttp.Option{ |
| 165 |
otlptracehttp.WithHeaders(customUserAgentHeader), |
| 166 |
}, |
| 167 |
mcCfg: mockCollectorConfig{ |
| 168 |
ExpectedHeaders: customUserAgentHeader, |
| 169 |
}, |
| 170 |
}, |
| 171 |
{ |
| 172 |
name: "with custom proxy", |
| 173 |
opts: []otlptracehttp.Option{ |
| 174 |
otlptracehttp.WithProxy(func(r *http.Request) (*url.URL, error) { |
| 175 |
for k, v := range customProxyHeader { |
| 176 |
r.Header.Set(k, v) |
| 177 |
} |
| 178 |
return r.URL, nil |
| 179 |
}), |
| 180 |
}, |
| 181 |
mcCfg: mockCollectorConfig{ |
| 182 |
ExpectedHeaders: customProxyHeader, |
| 183 |
}, |
| 184 |
}, |
| 185 |
} |
| 186 |
|
| 187 |
for _, tc := range tests { |
| 188 |
t.Run(tc.name, func(t *testing.T) { |
| 189 |
mc := runMockCollector(t, tc.mcCfg) |
| 190 |
defer mc.MustStop(t) |
| 191 |
allOpts := []otlptracehttp.Option{} |
| 192 |
|
| 193 |
if tc.withURLEndpoint { |
| 194 |
allOpts = append(allOpts, otlptracehttp.WithEndpointURL("http://"+mc.Endpoint())) |
| 195 |
} else { |
| 196 |
allOpts = append(allOpts, otlptracehttp.WithEndpoint(mc.Endpoint())) |
| 197 |
} |
| 198 |
if tc.tls { |
| 199 |
tlsConfig := mc.ClientTLSConfig() |
| 200 |
require.NotNil(t, tlsConfig) |
| 201 |
allOpts = append(allOpts, otlptracehttp.WithTLSClientConfig(tlsConfig)) |
| 202 |
} else { |
| 203 |
allOpts = append(allOpts, otlptracehttp.WithInsecure()) |
| 204 |
} |
| 205 |
allOpts = append(allOpts, tc.opts...) |
| 206 |
client := otlptracehttp.NewClient(allOpts...) |
| 207 |
ctx := t.Context() |
| 208 |
exporter, err := otlptrace.New(ctx, client) |
| 209 |
if assert.NoError(t, err) { |
| 210 |
defer func() { |
| 211 |
assert.NoError(t, exporter.Shutdown(ctx)) |
| 212 |
}() |
| 213 |
otlptracetest.RunEndToEndTest(ctx, t, exporter, mc) |
| 214 |
} |
| 215 |
}) |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
func TestExporterShutdown(t *testing.T) { |
| 220 |
mc := runMockCollector(t, mockCollectorConfig{}) |
| 221 |
defer func() { |
| 222 |
_ = mc.Stop() |
| 223 |
}() |
| 224 |
|
| 225 |
<-time.After(5 * time.Millisecond) |
| 226 |
|
| 227 |
otlptracetest.RunExporterShutdownTest(t, func() otlptrace.Client { |
| 228 |
return otlptracehttp.NewClient( |
| 229 |
otlptracehttp.WithInsecure(), |
| 230 |
otlptracehttp.WithEndpoint(mc.endpoint), |
| 231 |
) |
| 232 |
}) |
| 233 |
} |
| 234 |
|
| 235 |
func TestTimeout(t *testing.T) { |
| 236 |
delay := make(chan struct{}) |
| 237 |
mcCfg := mockCollectorConfig{Delay: delay} |
| 238 |
mc := runMockCollector(t, mcCfg) |
| 239 |
defer mc.MustStop(t) |
| 240 |
defer func() { close(delay) }() |
| 241 |
client := otlptracehttp.NewClient( |
| 242 |
otlptracehttp.WithEndpoint(mc.Endpoint()), |
| 243 |
otlptracehttp.WithInsecure(), |
| 244 |
otlptracehttp.WithTimeout(time.Nanosecond), |
| 245 |
otlptracehttp.WithRetry(otlptracehttp.RetryConfig{Enabled: false}), |
| 246 |
) |
| 247 |
ctx := t.Context() |
| 248 |
exporter, err := otlptrace.New(ctx, client) |
| 249 |
require.NoError(t, err) |
| 250 |
defer func() { |
| 251 |
assert.NoError(t, exporter.Shutdown(ctx)) |
| 252 |
}() |
| 253 |
err = exporter.ExportSpans(ctx, otlptracetest.SingleReadOnlySpan()) |
| 254 |
assert.ErrorContains(t, err, "Client.Timeout exceeded while awaiting headers") |
| 255 |
} |
| 256 |
|
| 257 |
func TestInsecureWithTLSClientConfig(t *testing.T) { |
| 258 |
client := otlptracehttp.NewClient( |
| 259 |
otlptracehttp.WithEndpoint("localhost:4318"), |
| 260 |
otlptracehttp.WithInsecure(), |
| 261 |
otlptracehttp.WithTLSClientConfig(&tls.Config{}), |
| 262 |
) |
| 263 |
exp, err := otlptrace.New(t.Context(), client) |
| 264 |
require.ErrorContains(t, err, "insecure HTTP endpoint cannot use TLS client configuration") |
| 265 |
assert.Nil(t, exp) |
| 266 |
} |
| 267 |
|
| 268 |
func TestNoRetry(t *testing.T) { |
| 269 |
mc := runMockCollector(t, mockCollectorConfig{ |
| 270 |
InjectHTTPStatus: []int{http.StatusBadRequest}, |
| 271 |
Partial: &coltracepb.ExportTracePartialSuccess{ |
| 272 |
ErrorMessage: "missing required attribute aaa", |
| 273 |
}, |
| 274 |
}) |
| 275 |
defer mc.MustStop(t) |
| 276 |
driver := otlptracehttp.NewClient( |
| 277 |
otlptracehttp.WithEndpoint(mc.Endpoint()), |
| 278 |
otlptracehttp.WithInsecure(), |
| 279 |
otlptracehttp.WithRetry(otlptracehttp.RetryConfig{ |
| 280 |
Enabled: true, |
| 281 |
InitialInterval: 1 * time.Nanosecond, |
| 282 |
MaxInterval: 1 * time.Nanosecond, |
| 283 |
// Never stop retry of retry-able status. |
| 284 |
MaxElapsedTime: 0, |
| 285 |
}), |
| 286 |
) |
| 287 |
ctx := t.Context() |
| 288 |
exporter, err := otlptrace.New(ctx, driver) |
| 289 |
require.NoError(t, err) |
| 290 |
defer func() { |
| 291 |
assert.NoError(t, exporter.Shutdown(ctx)) |
| 292 |
}() |
| 293 |
err = exporter.ExportSpans(ctx, otlptracetest.SingleReadOnlySpan()) |
| 294 |
assert.Error(t, err) |
| 295 |
assert.True(t, strings.HasPrefix(err.Error(), "traces export: ")) |
| 296 |
|
| 297 |
msg := fmt.Sprintf("failed to send to http://%s/v1/traces: 400 Bad Request", mc.endpoint) |
| 298 |
assert.ErrorContains(t, err, msg) |
| 299 |
|
| 300 |
msg = "missing required attribute aaa" |
| 301 |
assert.ErrorContains(t, err, msg) |
| 302 |
|
| 303 |
assert.Empty(t, mc.GetSpans()) |
| 304 |
} |
| 305 |
|
| 306 |
func TestEmptyData(t *testing.T) { |
| 307 |
mcCfg := mockCollectorConfig{} |
| 308 |
mc := runMockCollector(t, mcCfg) |
| 309 |
defer mc.MustStop(t) |
| 310 |
driver := otlptracehttp.NewClient( |
| 311 |
otlptracehttp.WithEndpoint(mc.Endpoint()), |
| 312 |
otlptracehttp.WithInsecure(), |
| 313 |
) |
| 314 |
ctx := t.Context() |
| 315 |
exporter, err := otlptrace.New(ctx, driver) |
| 316 |
require.NoError(t, err) |
| 317 |
defer func() { |
| 318 |
assert.NoError(t, exporter.Shutdown(ctx)) |
| 319 |
}() |
| 320 |
assert.NoError(t, err) |
| 321 |
err = exporter.ExportSpans(ctx, nil) |
| 322 |
assert.NoError(t, err) |
| 323 |
assert.Empty(t, mc.GetSpans()) |
| 324 |
} |
| 325 |
|
| 326 |
func TestCancelledContext(t *testing.T) { |
| 327 |
mcCfg := mockCollectorConfig{} |
| 328 |
mc := runMockCollector(t, mcCfg) |
| 329 |
defer mc.MustStop(t) |
| 330 |
driver := otlptracehttp.NewClient( |
| 331 |
otlptracehttp.WithEndpoint(mc.Endpoint()), |
| 332 |
otlptracehttp.WithInsecure(), |
| 333 |
) |
| 334 |
ctx, cancel := context.WithCancel(t.Context()) |
| 335 |
exporter, err := otlptrace.New(ctx, driver) |
| 336 |
require.NoError(t, err) |
| 337 |
defer func() { |
| 338 |
assert.NoError(t, exporter.Shutdown(t.Context())) |
| 339 |
}() |
| 340 |
cancel() |
| 341 |
err = exporter.ExportSpans(ctx, otlptracetest.SingleReadOnlySpan()) |
| 342 |
assert.Error(t, err) |
| 343 |
assert.Empty(t, mc.GetSpans()) |
| 344 |
} |
| 345 |
|
| 346 |
func TestDeadlineContext(t *testing.T) { |
| 347 |
statuses := make([]int, 0, 5) |
| 348 |
for i := 0; i < cap(statuses); i++ { |
| 349 |
statuses = append(statuses, http.StatusTooManyRequests) |
| 350 |
} |
| 351 |
mcCfg := mockCollectorConfig{ |
| 352 |
InjectHTTPStatus: statuses, |
| 353 |
} |
| 354 |
mc := runMockCollector(t, mcCfg) |
| 355 |
defer mc.MustStop(t) |
| 356 |
driver := otlptracehttp.NewClient( |
| 357 |
otlptracehttp.WithEndpoint(mc.Endpoint()), |
| 358 |
otlptracehttp.WithInsecure(), |
| 359 |
otlptracehttp.WithRetry(otlptracehttp.RetryConfig{ |
| 360 |
Enabled: true, |
| 361 |
InitialInterval: 1 * time.Hour, |
| 362 |
MaxInterval: 1 * time.Hour, |
| 363 |
// Never stop retry of retry-able status. |
| 364 |
MaxElapsedTime: 0, |
| 365 |
}), |
| 366 |
) |
| 367 |
ctx := t.Context() |
| 368 |
exporter, err := otlptrace.New(ctx, driver) |
| 369 |
require.NoError(t, err) |
| 370 |
defer func() { |
| 371 |
assert.NoError(t, exporter.Shutdown(t.Context())) |
| 372 |
}() |
| 373 |
ctx, cancel := context.WithTimeout(ctx, time.Second) |
| 374 |
defer cancel() |
| 375 |
err = exporter.ExportSpans(ctx, otlptracetest.SingleReadOnlySpan()) |
| 376 |
assert.Error(t, err) |
| 377 |
assert.Empty(t, mc.GetSpans()) |
| 378 |
} |
| 379 |
|
| 380 |
func TestStopWhileExportingConcurrentSafe(t *testing.T) { |
| 381 |
statuses := make([]int, 0, 5) |
| 382 |
for i := 0; i < cap(statuses); i++ { |
| 383 |
statuses = append(statuses, http.StatusTooManyRequests) |
| 384 |
} |
| 385 |
mcCfg := mockCollectorConfig{ |
| 386 |
InjectHTTPStatus: statuses, |
| 387 |
} |
| 388 |
mc := runMockCollector(t, mcCfg) |
| 389 |
defer mc.MustStop(t) |
| 390 |
driver := otlptracehttp.NewClient( |
| 391 |
otlptracehttp.WithEndpoint(mc.Endpoint()), |
| 392 |
otlptracehttp.WithInsecure(), |
| 393 |
otlptracehttp.WithRetry(otlptracehttp.RetryConfig{ |
| 394 |
Enabled: true, |
| 395 |
InitialInterval: 1 * time.Hour, |
| 396 |
MaxInterval: 1 * time.Hour, |
| 397 |
// Never stop retry of retry-able status. |
| 398 |
MaxElapsedTime: 0, |
| 399 |
}), |
| 400 |
) |
| 401 |
ctx := t.Context() |
| 402 |
exporter, err := otlptrace.New(ctx, driver) |
| 403 |
require.NoError(t, err) |
| 404 |
defer func() { |
| 405 |
assert.NoError(t, exporter.Shutdown(ctx)) |
| 406 |
}() |
| 407 |
doneCh := make(chan struct{}) |
| 408 |
go func() { |
| 409 |
err := exporter.ExportSpans(ctx, otlptracetest.SingleReadOnlySpan()) |
| 410 |
assert.Error(t, err) |
| 411 |
assert.Empty(t, mc.GetSpans()) |
| 412 |
close(doneCh) |
| 413 |
}() |
| 414 |
<-time.After(time.Second) |
| 415 |
err = exporter.Shutdown(ctx) |
| 416 |
assert.NoError(t, err) |
| 417 |
<-doneCh |
| 418 |
} |
| 419 |
|
| 420 |
func TestPartialSuccess(t *testing.T) { |
| 421 |
mcCfg := mockCollectorConfig{ |
| 422 |
Partial: &coltracepb.ExportTracePartialSuccess{ |
| 423 |
RejectedSpans: 2, |
| 424 |
ErrorMessage: "partially successful", |
| 425 |
}, |
| 426 |
} |
| 427 |
mc := runMockCollector(t, mcCfg) |
| 428 |
defer mc.MustStop(t) |
| 429 |
driver := otlptracehttp.NewClient( |
| 430 |
otlptracehttp.WithEndpoint(mc.Endpoint()), |
| 431 |
otlptracehttp.WithInsecure(), |
| 432 |
) |
| 433 |
ctx := t.Context() |
| 434 |
exporter, err := otlptrace.New(ctx, driver) |
| 435 |
require.NoError(t, err) |
| 436 |
defer func() { |
| 437 |
assert.NoError(t, exporter.Shutdown(t.Context())) |
| 438 |
}() |
| 439 |
|
| 440 |
err = exporter.ExportSpans(ctx, otlptracetest.SingleReadOnlySpan()) |
| 441 |
want := internal.TracePartialSuccessError(0, "") |
| 442 |
assert.ErrorIs(t, err, want) |
| 443 |
} |
| 444 |
|
| 445 |
func TestOtherHTTPSuccess(t *testing.T) { |
| 446 |
for code := 201; code <= 299; code++ { |
| 447 |
t.Run(fmt.Sprintf("status_%d", code), func(t *testing.T) { |
| 448 |
mcCfg := mockCollectorConfig{ |
| 449 |
InjectHTTPStatus: []int{code}, |
| 450 |
} |
| 451 |
mc := runMockCollector(t, mcCfg) |
| 452 |
defer mc.MustStop(t) |
| 453 |
driver := otlptracehttp.NewClient( |
| 454 |
otlptracehttp.WithEndpoint(mc.Endpoint()), |
| 455 |
otlptracehttp.WithInsecure(), |
| 456 |
) |
| 457 |
ctx := t.Context() |
| 458 |
exporter, err := otlptrace.New(ctx, driver) |
| 459 |
require.NoError(t, err) |
| 460 |
defer func() { |
| 461 |
assert.NoError(t, exporter.Shutdown(t.Context())) |
| 462 |
}() |
| 463 |
|
| 464 |
errs := []error{} |
| 465 |
otel.SetErrorHandler(otel.ErrorHandlerFunc(func(err error) { |
| 466 |
errs = append(errs, err) |
| 467 |
})) |
| 468 |
err = exporter.ExportSpans(ctx, otlptracetest.SingleReadOnlySpan()) |
| 469 |
assert.NoError(t, err) |
| 470 |
|
| 471 |
assert.Empty(t, errs) |
| 472 |
}) |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
func TestCollectorRespondingNonProtobufContent(t *testing.T) { |
| 477 |
mcCfg := mockCollectorConfig{ |
| 478 |
InjectContentType: "application/octet-stream", |
| 479 |
} |
| 480 |
mc := runMockCollector(t, mcCfg) |
| 481 |
defer mc.MustStop(t) |
| 482 |
driver := otlptracehttp.NewClient( |
| 483 |
otlptracehttp.WithEndpoint(mc.Endpoint()), |
| 484 |
otlptracehttp.WithInsecure(), |
| 485 |
) |
| 486 |
ctx := t.Context() |
| 487 |
exporter, err := otlptrace.New(ctx, driver) |
| 488 |
require.NoError(t, err) |
| 489 |
defer func() { |
| 490 |
assert.NoError(t, exporter.Shutdown(t.Context())) |
| 491 |
}() |
| 492 |
err = exporter.ExportSpans(ctx, otlptracetest.SingleReadOnlySpan()) |
| 493 |
assert.NoError(t, err) |
| 494 |
assert.Len(t, mc.GetSpans(), 1) |
| 495 |
} |
| 496 |
|
| 497 |
func TestClientInstrumentation(t *testing.T) { |
| 498 |
// Enable instrumentation for this test. |
| 499 |
t.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 500 |
|
| 501 |
// Reset client ID to be deterministic |
| 502 |
const id = 0 |
| 503 |
counter.SetExporterID(id) |
| 504 |
|
| 505 |
// Save original meter provider and restore at end of test. |
| 506 |
orig := otel.GetMeterProvider() |
| 507 |
t.Cleanup(func() { otel.SetMeterProvider(orig) }) |
| 508 |
|
| 509 |
// Create a new meter provider to capture metrics. |
| 510 |
reader := metric.NewManualReader() |
| 511 |
mp := metric.NewMeterProvider(metric.WithReader(reader)) |
| 512 |
otel.SetMeterProvider(mp) |
| 513 |
|
| 514 |
const n, msg = 2, "partially successful" |
| 515 |
mc := runMockCollector(t, mockCollectorConfig{ |
| 516 |
InjectHTTPStatus: []int{400}, |
| 517 |
Partial: &coltracepb.ExportTracePartialSuccess{ |
| 518 |
RejectedSpans: n, |
| 519 |
ErrorMessage: msg, |
| 520 |
}, |
| 521 |
}) |
| 522 |
t.Cleanup(func() { require.NoError(t, mc.Stop()) }) |
| 523 |
|
| 524 |
driver := otlptracehttp.NewClient( |
| 525 |
otlptracehttp.WithEndpoint(mc.Endpoint()), |
| 526 |
otlptracehttp.WithInsecure(), |
| 527 |
) |
| 528 |
exporter, err := otlptrace.New(t.Context(), driver) |
| 529 |
require.NoError(t, err) |
| 530 |
|
| 531 |
err = exporter.ExportSpans(t.Context(), otlptracetest.SingleReadOnlySpan()) |
| 532 |
assert.Error(t, err) |
| 533 |
|
| 534 |
require.NoError(t, exporter.Shutdown(t.Context())) |
| 535 |
var got metricdata.ResourceMetrics |
| 536 |
require.NoError(t, reader.Collect(t.Context(), &got)) |
| 537 |
|
| 538 |
attrs := observ.BaseAttrs(id, mc.endpoint) |
| 539 |
|
| 540 |
want := metricdata.ScopeMetrics{ |
| 541 |
Scope: instrumentation.Scope{ |
| 542 |
Name: observ.ScopeName, |
| 543 |
Version: observ.Version, |
| 544 |
SchemaURL: observ.SchemaURL, |
| 545 |
}, |
| 546 |
Metrics: []metricdata.Metrics{ |
| 547 |
{ |
| 548 |
Name: otelconv.SDKExporterSpanInflight{}.Name(), |
| 549 |
Description: otelconv.SDKExporterSpanInflight{}.Description(), |
| 550 |
Unit: otelconv.SDKExporterSpanInflight{}.Unit(), |
| 551 |
Data: metricdata.Sum[int64]{ |
| 552 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 553 |
{Attributes: attribute.NewSet(attrs...)}, |
| 554 |
}, |
| 555 |
Temporality: metricdata.CumulativeTemporality, |
| 556 |
}, |
| 557 |
}, |
| 558 |
{ |
| 559 |
Name: otelconv.SDKExporterSpanExported{}.Name(), |
| 560 |
Description: otelconv.SDKExporterSpanExported{}.Description(), |
| 561 |
Unit: otelconv.SDKExporterSpanExported{}.Unit(), |
| 562 |
Data: metricdata.Sum[int64]{ |
| 563 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 564 |
{Attributes: attribute.NewSet(attrs...)}, |
| 565 |
{Attributes: attribute.NewSet(append( |
| 566 |
attrs, |
| 567 |
otelconv.SDKExporterSpanExported{}.AttrErrorType("*errors.joinError"), |
| 568 |
)...)}, |
| 569 |
}, |
| 570 |
Temporality: 0x1, |
| 571 |
IsMonotonic: true, |
| 572 |
}, |
| 573 |
}, |
| 574 |
{ |
| 575 |
Name: otelconv.SDKExporterOperationDuration{}.Name(), |
| 576 |
Description: otelconv.SDKExporterOperationDuration{}.Description(), |
| 577 |
Unit: otelconv.SDKExporterOperationDuration{}.Unit(), |
| 578 |
Data: metricdata.Histogram[float64]{ |
| 579 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 580 |
{Attributes: attribute.NewSet(append( |
| 581 |
attrs, |
| 582 |
otelconv.SDKExporterOperationDuration{}.AttrErrorType("*errors.joinError"), |
| 583 |
otelconv.SDKExporterOperationDuration{}.AttrHTTPResponseStatusCode(400), |
| 584 |
)...)}, |
| 585 |
}, |
| 586 |
Temporality: 0x1, |
| 587 |
}, |
| 588 |
}, |
| 589 |
}, |
| 590 |
} |
| 591 |
require.Len(t, got.ScopeMetrics, 1) |
| 592 |
opt := []metricdatatest.Option{ |
| 593 |
metricdatatest.IgnoreTimestamp(), |
| 594 |
metricdatatest.IgnoreExemplars(), |
| 595 |
metricdatatest.IgnoreValue(), |
| 596 |
} |
| 597 |
metricdatatest.AssertEqual(t, want, got.ScopeMetrics[0], opt...) |
| 598 |
} |
| 599 |
|
| 600 |
func TestResponseBodySizeLimit(t *testing.T) { |
| 601 |
// Override the limit to 1 byte so any non-empty response body exceeds it. |
| 602 |
orig := *otlptracehttp.MaxResponseBodySize |
| 603 |
*otlptracehttp.MaxResponseBodySize = 1 |
| 604 |
t.Cleanup(func() { *otlptracehttp.MaxResponseBodySize = orig }) |
| 605 |
|
| 606 |
// largeBody is larger than the 1-byte limit. |
| 607 |
largeBody := []byte("xx") |
| 608 |
|
| 609 |
tests := []struct { |
| 610 |
name string |
| 611 |
status int |
| 612 |
contentType string |
| 613 |
}{ |
| 614 |
{ |
| 615 |
name: "success response body too large", |
| 616 |
status: http.StatusOK, |
| 617 |
contentType: "application/x-protobuf", |
| 618 |
}, |
| 619 |
{ |
| 620 |
name: "error response body too large", |
| 621 |
status: http.StatusServiceUnavailable, |
| 622 |
contentType: "text/plain", |
| 623 |
}, |
| 624 |
} |
| 625 |
for _, tc := range tests { |
| 626 |
t.Run(tc.name, func(t *testing.T) { |
| 627 |
var calls int |
| 628 |
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 629 |
calls++ |
| 630 |
w.Header().Set("Content-Type", tc.contentType) |
| 631 |
w.WriteHeader(tc.status) |
| 632 |
_, _ = w.Write(largeBody) |
| 633 |
})) |
| 634 |
t.Cleanup(srv.Close) |
| 635 |
|
| 636 |
client := otlptracehttp.NewClient( |
| 637 |
otlptracehttp.WithEndpointURL(srv.URL), |
| 638 |
otlptracehttp.WithInsecure(), |
| 639 |
otlptracehttp.WithRetry(otlptracehttp.RetryConfig{Enabled: false}), |
| 640 |
) |
| 641 |
exporter, err := otlptrace.New(t.Context(), client) |
| 642 |
require.NoError(t, err) |
| 643 |
t.Cleanup(func() { _ = exporter.Shutdown(t.Context()) }) |
| 644 |
|
| 645 |
err = exporter.ExportSpans(t.Context(), otlptracetest.SingleReadOnlySpan()) |
| 646 |
assert.ErrorContains(t, err, "response body too large") |
| 647 |
assert.Equal(t, 1, calls, "request must not be retried after body-too-large error") |
| 648 |
}) |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
func TestGetBodyCalledOnRedirect(t *testing.T) { |
| 653 |
// Test that req.GetBody is set correctly, allowing the HTTP transport |
| 654 |
// to re-send the body on 307 redirects. |
| 655 |
|
| 656 |
var mu sync.Mutex |
| 657 |
var requestBodies [][]byte |
| 658 |
|
| 659 |
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 660 |
body, err := io.ReadAll(r.Body) |
| 661 |
if err != nil { |
| 662 |
w.WriteHeader(http.StatusInternalServerError) |
| 663 |
return |
| 664 |
} |
| 665 |
|
| 666 |
mu.Lock() |
| 667 |
requestBodies = append(requestBodies, body) |
| 668 |
isFirstRequest := len(requestBodies) == 1 |
| 669 |
mu.Unlock() |
| 670 |
|
| 671 |
if isFirstRequest { |
| 672 |
w.Header().Set("Location", "/v1/traces/final") |
| 673 |
w.WriteHeader(http.StatusTemporaryRedirect) |
| 674 |
return |
| 675 |
} |
| 676 |
|
| 677 |
w.Header().Set("Content-Type", "application/x-protobuf") |
| 678 |
w.WriteHeader(http.StatusOK) |
| 679 |
}) |
| 680 |
|
| 681 |
server := httptest.NewServer(handler) |
| 682 |
defer server.Close() |
| 683 |
|
| 684 |
client := otlptracehttp.NewClient( |
| 685 |
otlptracehttp.WithEndpoint(server.Listener.Addr().String()), |
| 686 |
otlptracehttp.WithInsecure(), |
| 687 |
) |
| 688 |
|
| 689 |
ctx := t.Context() |
| 690 |
exporter, err := otlptrace.New(ctx, client) |
| 691 |
require.NoError(t, err) |
| 692 |
defer func() { _ = exporter.Shutdown(ctx) }() |
| 693 |
|
| 694 |
err = exporter.ExportSpans(ctx, otlptracetest.SingleReadOnlySpan()) |
| 695 |
require.NoError(t, err) |
| 696 |
|
| 697 |
mu.Lock() |
| 698 |
defer mu.Unlock() |
| 699 |
|
| 700 |
require.Len(t, requestBodies, 2, "expected 2 requests (original + redirect)") |
| 701 |
assert.NotEmpty(t, requestBodies[0], "original request body should not be empty") |
| 702 |
assert.Equal(t, requestBodies[0], requestBodies[1], "redirect body should match original") |
| 703 |
} |
| 704 |
|
| 705 |
func BenchmarkExporterExportSpans(b *testing.B) { |
| 706 |
const n = 10 |
| 707 |
|
| 708 |
run := func(b *testing.B) { |
| 709 |
mc := runMockCollector(b, mockCollectorConfig{ |
| 710 |
Partial: &coltracepb.ExportTracePartialSuccess{ |
| 711 |
RejectedSpans: 5, |
| 712 |
ErrorMessage: "partially successful", |
| 713 |
}, |
| 714 |
}) |
| 715 |
b.Cleanup(func() { require.NoError(b, mc.Stop()) }) |
| 716 |
|
| 717 |
c := otlptracehttp.NewClient( |
| 718 |
otlptracehttp.WithEndpoint(mc.Endpoint()), |
| 719 |
otlptracehttp.WithInsecure(), |
| 720 |
) |
| 721 |
exp, err := otlptrace.New(b.Context(), c) |
| 722 |
require.NoError(b, err) |
| 723 |
b.Cleanup(func() { |
| 724 |
//nolint:usetesting // required to avoid getting a canceled context at cleanup. |
| 725 |
assert.NoError(b, exp.Shutdown(context.Background())) |
| 726 |
}) |
| 727 |
|
| 728 |
stubs := make([]tracetest.SpanStub, n) |
| 729 |
for i := range stubs { |
| 730 |
stubs[i].Name = fmt.Sprintf("Span %d", i) |
| 731 |
} |
| 732 |
spans := tracetest.SpanStubs(stubs).Snapshots() |
| 733 |
|
| 734 |
b.ReportAllocs() |
| 735 |
b.ResetTimer() |
| 736 |
|
| 737 |
for b.Loop() { |
| 738 |
err = exp.ExportSpans(b.Context(), spans) |
| 739 |
} |
| 740 |
_ = err |
| 741 |
} |
| 742 |
|
| 743 |
b.Run("Observability", func(b *testing.B) { |
| 744 |
b.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 745 |
run(b) |
| 746 |
}) |
| 747 |
|
| 748 |
b.Run("NoObservability", func(b *testing.B) { |
| 749 |
b.Setenv("OTEL_GO_X_OBSERVABILITY", "false") |
| 750 |
run(b) |
| 751 |
}) |
| 752 |
} |