| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package stdoutmetric_test // import "go.opentelemetry.io/otel/exporters/stdout/stdoutmetric" |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"context" |
| 9 |
"encoding/json" |
| 10 |
"errors" |
| 11 |
"io" |
| 12 |
"strconv" |
| 13 |
"testing" |
| 14 |
"time" |
| 15 |
|
| 16 |
"github.com/stretchr/testify/assert" |
| 17 |
"github.com/stretchr/testify/require" |
| 18 |
|
| 19 |
"go.opentelemetry.io/otel" |
| 20 |
"go.opentelemetry.io/otel/attribute" |
| 21 |
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric" |
| 22 |
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric/internal/counter" |
| 23 |
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric/internal/observ" |
| 24 |
"go.opentelemetry.io/otel/sdk" |
| 25 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 26 |
"go.opentelemetry.io/otel/sdk/metric" |
| 27 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 28 |
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" |
| 29 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 30 |
"go.opentelemetry.io/otel/semconv/v1.40.0/otelconv" |
| 31 |
) |
| 32 |
|
| 33 |
func testEncoderOption() stdoutmetric.Option { |
| 34 |
// Discard export output for testing. |
| 35 |
enc := json.NewEncoder(io.Discard) |
| 36 |
return stdoutmetric.WithEncoder(enc) |
| 37 |
} |
| 38 |
|
| 39 |
var errEnc = errors.New("encoding failed") |
| 40 |
|
| 41 |
// failingEncoder always returns an error when Encode is called. |
| 42 |
type failingEncoder struct{} |
| 43 |
|
| 44 |
func (failingEncoder) Encode(any) error { |
| 45 |
return errEnc |
| 46 |
} |
| 47 |
|
| 48 |
func testCtxErrHonored(factory func(*testing.T) func(context.Context) error) func(t *testing.T) { |
| 49 |
return func(t *testing.T) { |
| 50 |
t.Helper() |
| 51 |
ctx := t.Context() |
| 52 |
|
| 53 |
t.Run("DeadlineExceeded", func(t *testing.T) { |
| 54 |
innerCtx, innerCancel := context.WithTimeout(ctx, time.Nanosecond) |
| 55 |
t.Cleanup(innerCancel) |
| 56 |
<-innerCtx.Done() |
| 57 |
|
| 58 |
f := factory(t) |
| 59 |
assert.ErrorIs(t, f(innerCtx), context.DeadlineExceeded) |
| 60 |
}) |
| 61 |
|
| 62 |
t.Run("Canceled", func(t *testing.T) { |
| 63 |
innerCtx, innerCancel := context.WithCancel(ctx) |
| 64 |
innerCancel() |
| 65 |
|
| 66 |
f := factory(t) |
| 67 |
assert.ErrorIs(t, f(innerCtx), context.Canceled) |
| 68 |
}) |
| 69 |
|
| 70 |
t.Run("NoError", func(t *testing.T) { |
| 71 |
f := factory(t) |
| 72 |
assert.NoError(t, f(ctx)) |
| 73 |
}) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
func testCtxErrIgnored(factory func(*testing.T) func(context.Context) error) func(t *testing.T) { |
| 78 |
return func(t *testing.T) { |
| 79 |
t.Helper() |
| 80 |
ctx := t.Context() |
| 81 |
|
| 82 |
t.Run("Canceled Ignored", func(t *testing.T) { |
| 83 |
innerCtx, innerCancel := context.WithCancel(ctx) |
| 84 |
innerCancel() |
| 85 |
|
| 86 |
f := factory(t) |
| 87 |
assert.NoError(t, f(innerCtx)) |
| 88 |
}) |
| 89 |
|
| 90 |
t.Run("NoError", func(t *testing.T) { |
| 91 |
f := factory(t) |
| 92 |
assert.NoError(t, f(ctx)) |
| 93 |
}) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
func TestExporterExportHonorsContextErrors(t *testing.T) { |
| 98 |
t.Run("Export", testCtxErrHonored(func(t *testing.T) func(context.Context) error { |
| 99 |
exp, err := stdoutmetric.New(testEncoderOption()) |
| 100 |
require.NoError(t, err) |
| 101 |
return func(ctx context.Context) error { |
| 102 |
data := new(metricdata.ResourceMetrics) |
| 103 |
return exp.Export(ctx, data) |
| 104 |
} |
| 105 |
})) |
| 106 |
} |
| 107 |
|
| 108 |
func TestExporterForceFlushIgnoresContextErrors(t *testing.T) { |
| 109 |
t.Run("ForceFlush", testCtxErrIgnored(func(t *testing.T) func(context.Context) error { |
| 110 |
exp, err := stdoutmetric.New(testEncoderOption()) |
| 111 |
require.NoError(t, err) |
| 112 |
return exp.ForceFlush |
| 113 |
})) |
| 114 |
} |
| 115 |
|
| 116 |
func TestExporterShutdownIgnoresContextErrors(t *testing.T) { |
| 117 |
t.Run("Shutdown", testCtxErrIgnored(func(t *testing.T) func(context.Context) error { |
| 118 |
exp, err := stdoutmetric.New(testEncoderOption()) |
| 119 |
require.NoError(t, err) |
| 120 |
return exp.Shutdown |
| 121 |
})) |
| 122 |
} |
| 123 |
|
| 124 |
func TestShutdownExporterReturnsShutdownErrorOnExport(t *testing.T) { |
| 125 |
var ( |
| 126 |
data = new(metricdata.ResourceMetrics) |
| 127 |
ctx = t.Context() |
| 128 |
exp, err = stdoutmetric.New(testEncoderOption()) |
| 129 |
) |
| 130 |
require.NoError(t, err) |
| 131 |
require.NoError(t, exp.Shutdown(ctx)) |
| 132 |
assert.EqualError(t, exp.Export(ctx, data), "exporter shutdown") |
| 133 |
} |
| 134 |
|
| 135 |
func deltaSelector(metric.InstrumentKind) metricdata.Temporality { |
| 136 |
return metricdata.DeltaTemporality |
| 137 |
} |
| 138 |
|
| 139 |
func TestExportWithOptions(t *testing.T) { |
| 140 |
var ( |
| 141 |
data = new(metricdata.ResourceMetrics) |
| 142 |
ctx = t.Context() |
| 143 |
) |
| 144 |
|
| 145 |
for _, tt := range []struct { |
| 146 |
name string |
| 147 |
opts []stdoutmetric.Option |
| 148 |
|
| 149 |
expectedData string |
| 150 |
}{ |
| 151 |
{ |
| 152 |
name: "with no options", |
| 153 |
expectedData: "{\"Resource\":null,\"ScopeMetrics\":null}\n", |
| 154 |
}, |
| 155 |
{ |
| 156 |
name: "with pretty print", |
| 157 |
opts: []stdoutmetric.Option{ |
| 158 |
stdoutmetric.WithPrettyPrint(), |
| 159 |
}, |
| 160 |
expectedData: "{\n\t\"Resource\": null,\n\t\"ScopeMetrics\": null\n}\n", |
| 161 |
}, |
| 162 |
} { |
| 163 |
t.Run(tt.name, func(t *testing.T) { |
| 164 |
var b bytes.Buffer |
| 165 |
opts := append(tt.opts, stdoutmetric.WithWriter(&b)) |
| 166 |
|
| 167 |
exp, err := stdoutmetric.New(opts...) |
| 168 |
require.NoError(t, err) |
| 169 |
require.NoError(t, exp.Export(ctx, data)) |
| 170 |
|
| 171 |
assert.Equal(t, tt.expectedData, b.String()) |
| 172 |
}) |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
func TestTemporalitySelector(t *testing.T) { |
| 177 |
exp, err := stdoutmetric.New( |
| 178 |
testEncoderOption(), |
| 179 |
stdoutmetric.WithTemporalitySelector(deltaSelector), |
| 180 |
) |
| 181 |
require.NoError(t, err) |
| 182 |
|
| 183 |
var unknownKind metric.InstrumentKind |
| 184 |
assert.Equal(t, metricdata.DeltaTemporality, exp.Temporality(unknownKind)) |
| 185 |
} |
| 186 |
|
| 187 |
func dropSelector(metric.InstrumentKind) metric.Aggregation { |
| 188 |
return metric.AggregationDrop{} |
| 189 |
} |
| 190 |
|
| 191 |
func TestAggregationSelector(t *testing.T) { |
| 192 |
exp, err := stdoutmetric.New( |
| 193 |
testEncoderOption(), |
| 194 |
stdoutmetric.WithAggregationSelector(dropSelector), |
| 195 |
) |
| 196 |
require.NoError(t, err) |
| 197 |
|
| 198 |
var unknownKind metric.InstrumentKind |
| 199 |
assert.Equal(t, metric.AggregationDrop{}, exp.Aggregation(unknownKind)) |
| 200 |
} |
| 201 |
|
| 202 |
func TestExporterExportObservability(t *testing.T) { |
| 203 |
componentNameAttr := observ.ExporterComponentName(0) |
| 204 |
componentTypeAttr := semconv.OTelComponentTypeKey.String(observ.ComponentType) |
| 205 |
|
| 206 |
tests := []struct { |
| 207 |
name string |
| 208 |
exporterOpts []stdoutmetric.Option |
| 209 |
observabilityEnabled bool |
| 210 |
expectedExportedCount int64 |
| 211 |
inflightAttrs attribute.Set |
| 212 |
attributes attribute.Set |
| 213 |
wantErr error |
| 214 |
}{ |
| 215 |
{ |
| 216 |
name: "Enabled", |
| 217 |
exporterOpts: []stdoutmetric.Option{testEncoderOption()}, |
| 218 |
observabilityEnabled: true, |
| 219 |
expectedExportedCount: expectedDataPointCount, |
| 220 |
inflightAttrs: attribute.NewSet(componentNameAttr, componentTypeAttr), |
| 221 |
attributes: attribute.NewSet(componentNameAttr, componentTypeAttr), |
| 222 |
}, |
| 223 |
{ |
| 224 |
name: "Disabled", |
| 225 |
exporterOpts: []stdoutmetric.Option{testEncoderOption()}, |
| 226 |
observabilityEnabled: false, |
| 227 |
expectedExportedCount: 0, |
| 228 |
}, |
| 229 |
{ |
| 230 |
name: "EncodingError", |
| 231 |
exporterOpts: []stdoutmetric.Option{stdoutmetric.WithEncoder(failingEncoder{})}, |
| 232 |
observabilityEnabled: true, |
| 233 |
expectedExportedCount: expectedDataPointCount, |
| 234 |
inflightAttrs: attribute.NewSet(componentNameAttr, componentTypeAttr), |
| 235 |
attributes: attribute.NewSet( |
| 236 |
componentNameAttr, |
| 237 |
componentTypeAttr, |
| 238 |
semconv.ErrorType(errEnc), |
| 239 |
), |
| 240 |
wantErr: errEnc, |
| 241 |
}, |
| 242 |
} |
| 243 |
for _, tt := range tests { |
| 244 |
t.Run(tt.name, func(t *testing.T) { |
| 245 |
t.Setenv("OTEL_GO_X_OBSERVABILITY", strconv.FormatBool(tt.observabilityEnabled)) |
| 246 |
|
| 247 |
// Reset the exporter ID counter to ensure consistent component names |
| 248 |
_ = counter.SetExporterID(0) |
| 249 |
|
| 250 |
reader := metric.NewManualReader() |
| 251 |
mp := metric.NewMeterProvider(metric.WithReader(reader)) |
| 252 |
origMp := otel.GetMeterProvider() |
| 253 |
otel.SetMeterProvider(mp) |
| 254 |
t.Cleanup(func() { otel.SetMeterProvider(origMp) }) |
| 255 |
|
| 256 |
exp, err := stdoutmetric.New(tt.exporterOpts...) |
| 257 |
require.NoError(t, err) |
| 258 |
rm := &metricdata.ResourceMetrics{ScopeMetrics: scopeMetrics()} |
| 259 |
|
| 260 |
ctx := t.Context() |
| 261 |
err = exp.Export(ctx, rm) |
| 262 |
if tt.wantErr != nil { |
| 263 |
assert.ErrorIs(t, err, tt.wantErr) |
| 264 |
} else { |
| 265 |
assert.NoError(t, err) |
| 266 |
} |
| 267 |
|
| 268 |
var metrics metricdata.ResourceMetrics |
| 269 |
err = reader.Collect(ctx, &metrics) |
| 270 |
require.NoError(t, err) |
| 271 |
|
| 272 |
if !tt.observabilityEnabled { |
| 273 |
assert.Empty(t, metrics.ScopeMetrics) |
| 274 |
return |
| 275 |
} |
| 276 |
|
| 277 |
expectedMetrics := metricdata.ScopeMetrics{ |
| 278 |
Scope: instrumentation.Scope{ |
| 279 |
Name: "go.opentelemetry.io/otel/exporters/stdout/stdoutmetric/internal/observ", |
| 280 |
Version: sdk.Version(), |
| 281 |
SchemaURL: semconv.SchemaURL, |
| 282 |
}, |
| 283 |
Metrics: []metricdata.Metrics{ |
| 284 |
{ |
| 285 |
Name: otelconv.SDKExporterMetricDataPointInflight{}.Name(), |
| 286 |
Description: otelconv.SDKExporterMetricDataPointInflight{}.Description(), |
| 287 |
Unit: otelconv.SDKExporterMetricDataPointInflight{}.Unit(), |
| 288 |
Data: metricdata.Sum[int64]{ |
| 289 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 290 |
{ |
| 291 |
Value: 0, |
| 292 |
Attributes: tt.inflightAttrs, |
| 293 |
}, |
| 294 |
}, |
| 295 |
Temporality: metricdata.CumulativeTemporality, |
| 296 |
}, |
| 297 |
}, |
| 298 |
{ |
| 299 |
Name: otelconv.SDKExporterMetricDataPointExported{}.Name(), |
| 300 |
Description: otelconv.SDKExporterMetricDataPointExported{}.Description(), |
| 301 |
Unit: otelconv.SDKExporterMetricDataPointExported{}.Unit(), |
| 302 |
Data: metricdata.Sum[int64]{ |
| 303 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 304 |
{ |
| 305 |
Value: tt.expectedExportedCount, |
| 306 |
Attributes: tt.attributes, |
| 307 |
}, |
| 308 |
}, |
| 309 |
Temporality: metricdata.CumulativeTemporality, |
| 310 |
IsMonotonic: true, |
| 311 |
}, |
| 312 |
}, |
| 313 |
{ |
| 314 |
Name: otelconv.SDKExporterOperationDuration{}.Name(), |
| 315 |
Description: otelconv.SDKExporterOperationDuration{}.Description(), |
| 316 |
Unit: otelconv.SDKExporterOperationDuration{}.Unit(), |
| 317 |
Data: metricdata.Histogram[float64]{ |
| 318 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 319 |
{ |
| 320 |
Attributes: tt.attributes, |
| 321 |
}, |
| 322 |
}, |
| 323 |
Temporality: metricdata.CumulativeTemporality, |
| 324 |
}, |
| 325 |
}, |
| 326 |
}, |
| 327 |
} |
| 328 |
require.Len(t, metrics.ScopeMetrics, 1) |
| 329 |
assert.Equal(t, expectedMetrics.Scope, metrics.ScopeMetrics[0].Scope) |
| 330 |
require.Len(t, expectedMetrics.Metrics, 3) |
| 331 |
metricdatatest.AssertEqual( |
| 332 |
t, |
| 333 |
expectedMetrics.Metrics[0], |
| 334 |
metrics.ScopeMetrics[0].Metrics[0], |
| 335 |
metricdatatest.IgnoreTimestamp(), |
| 336 |
) |
| 337 |
metricdatatest.AssertEqual( |
| 338 |
t, |
| 339 |
expectedMetrics.Metrics[1], |
| 340 |
metrics.ScopeMetrics[0].Metrics[1], |
| 341 |
metricdatatest.IgnoreTimestamp(), |
| 342 |
) |
| 343 |
metricdatatest.AssertEqual( |
| 344 |
t, |
| 345 |
expectedMetrics.Metrics[2], |
| 346 |
metrics.ScopeMetrics[0].Metrics[2], |
| 347 |
metricdatatest.IgnoreTimestamp(), |
| 348 |
metricdatatest.IgnoreValue(), |
| 349 |
) |
| 350 |
}) |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
const expectedDataPointCount = 19 |
| 355 |
|
| 356 |
func scopeMetrics() []metricdata.ScopeMetrics { |
| 357 |
return []metricdata.ScopeMetrics{ |
| 358 |
{ |
| 359 |
Metrics: []metricdata.Metrics{ |
| 360 |
{ |
| 361 |
Name: "gauge_int64", |
| 362 |
Data: metricdata.Gauge[int64]{ |
| 363 |
DataPoints: []metricdata.DataPoint[int64]{{Value: 1}, {Value: 2}}, |
| 364 |
}, |
| 365 |
}, |
| 366 |
{ |
| 367 |
Name: "gauge_float64", |
| 368 |
Data: metricdata.Gauge[float64]{ |
| 369 |
DataPoints: []metricdata.DataPoint[float64]{ |
| 370 |
{Value: 1.0}, |
| 371 |
{Value: 2.0}, |
| 372 |
{Value: 3.0}, |
| 373 |
}, |
| 374 |
}, |
| 375 |
}, |
| 376 |
{ |
| 377 |
Name: "sum_int64", |
| 378 |
Data: metricdata.Sum[int64]{ |
| 379 |
DataPoints: []metricdata.DataPoint[int64]{{Value: 10}}, |
| 380 |
}, |
| 381 |
}, |
| 382 |
{ |
| 383 |
Name: "sum_float64", |
| 384 |
Data: metricdata.Sum[float64]{ |
| 385 |
DataPoints: []metricdata.DataPoint[float64]{{Value: 10.5}, {Value: 20.5}}, |
| 386 |
}, |
| 387 |
}, |
| 388 |
{ |
| 389 |
Name: "histogram_int64", |
| 390 |
Data: metricdata.Histogram[int64]{ |
| 391 |
DataPoints: []metricdata.HistogramDataPoint[int64]{ |
| 392 |
{Count: 1}, |
| 393 |
{Count: 2}, |
| 394 |
{Count: 3}, |
| 395 |
}, |
| 396 |
}, |
| 397 |
}, |
| 398 |
{ |
| 399 |
Name: "histogram_float64", |
| 400 |
Data: metricdata.Histogram[float64]{ |
| 401 |
DataPoints: []metricdata.HistogramDataPoint[float64]{{Count: 1}}, |
| 402 |
}, |
| 403 |
}, |
| 404 |
{ |
| 405 |
Name: "exponential_histogram_int64", |
| 406 |
Data: metricdata.ExponentialHistogram[int64]{ |
| 407 |
DataPoints: []metricdata.ExponentialHistogramDataPoint[int64]{ |
| 408 |
{Count: 1}, |
| 409 |
{Count: 2}, |
| 410 |
}, |
| 411 |
}, |
| 412 |
}, |
| 413 |
{ |
| 414 |
Name: "exponential_histogram_float64", |
| 415 |
Data: metricdata.ExponentialHistogram[float64]{ |
| 416 |
DataPoints: []metricdata.ExponentialHistogramDataPoint[float64]{ |
| 417 |
{Count: 1}, |
| 418 |
{Count: 2}, |
| 419 |
{Count: 3}, |
| 420 |
{Count: 4}, |
| 421 |
}, |
| 422 |
}, |
| 423 |
}, |
| 424 |
{ |
| 425 |
Name: "summary", |
| 426 |
Data: metricdata.Summary{ |
| 427 |
DataPoints: []metricdata.SummaryDataPoint{{Count: 1}}, |
| 428 |
}, |
| 429 |
}, |
| 430 |
}, |
| 431 |
}, |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
func TestExporterExportEncodingErrorTracking(t *testing.T) { |
| 436 |
t.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 437 |
reader := metric.NewManualReader() |
| 438 |
mp := metric.NewMeterProvider(metric.WithReader(reader)) |
| 439 |
origMp := otel.GetMeterProvider() |
| 440 |
otel.SetMeterProvider(mp) |
| 441 |
t.Cleanup(func() { otel.SetMeterProvider(origMp) }) |
| 442 |
|
| 443 |
exp, err := stdoutmetric.New(stdoutmetric.WithEncoder(failingEncoder{})) |
| 444 |
assert.NoError(t, err) |
| 445 |
|
| 446 |
rm := &metricdata.ResourceMetrics{ |
| 447 |
ScopeMetrics: []metricdata.ScopeMetrics{ |
| 448 |
{ |
| 449 |
Metrics: []metricdata.Metrics{ |
| 450 |
{ |
| 451 |
Name: "test_gauge", |
| 452 |
Data: metricdata.Gauge[int64]{ |
| 453 |
DataPoints: []metricdata.DataPoint[int64]{{Value: 1}, {Value: 2}}, |
| 454 |
}, |
| 455 |
}, |
| 456 |
}, |
| 457 |
}, |
| 458 |
}, |
| 459 |
} |
| 460 |
|
| 461 |
ctx := t.Context() |
| 462 |
err = exp.Export(ctx, rm) |
| 463 |
assert.ErrorIs(t, err, errEnc) |
| 464 |
|
| 465 |
var metrics metricdata.ResourceMetrics |
| 466 |
err = reader.Collect(ctx, &metrics) |
| 467 |
require.NoError(t, err) |
| 468 |
|
| 469 |
var foundErrorType bool |
| 470 |
for _, sm := range metrics.ScopeMetrics { |
| 471 |
for _, m := range sm.Metrics { |
| 472 |
x := otelconv.SDKExporterMetricDataPointExported{}.Name() |
| 473 |
if m.Name == x { |
| 474 |
if sum, ok := m.Data.(metricdata.Sum[int64]); ok { |
| 475 |
for _, dp := range sum.DataPoints { |
| 476 |
var attr attribute.Value |
| 477 |
attr, foundErrorType = dp.Attributes.Value(semconv.ErrorTypeKey) |
| 478 |
assert.Equal(t, "*errors.errorString", attr.AsString()) |
| 479 |
} |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
} |
| 484 |
assert.True(t, foundErrorType) |
| 485 |
} |
| 486 |
|
| 487 |
func BenchmarkExporterExport(b *testing.B) { |
| 488 |
rm := &metricdata.ResourceMetrics{ScopeMetrics: scopeMetrics()} |
| 489 |
|
| 490 |
run := func(b *testing.B) { |
| 491 |
ex, err := stdoutmetric.New(stdoutmetric.WithWriter(io.Discard)) |
| 492 |
if err != nil { |
| 493 |
b.Fatalf("failed to create exporter: %v", err) |
| 494 |
} |
| 495 |
|
| 496 |
b.ReportAllocs() |
| 497 |
b.ResetTimer() |
| 498 |
for b.Loop() { |
| 499 |
err = ex.Export(b.Context(), rm) |
| 500 |
} |
| 501 |
_ = err |
| 502 |
} |
| 503 |
|
| 504 |
b.Run("Observability", func(b *testing.B) { |
| 505 |
b.Setenv("OTEL_GO_X_OBSERVABILITY", "true") |
| 506 |
run(b) |
| 507 |
}) |
| 508 |
|
| 509 |
b.Run("NoObservability", run) |
| 510 |
} |