| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package metric // import "go.opentelemetry.io/otel/sdk/metric" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
"log" |
| 10 |
"os" |
| 11 |
"runtime" |
| 12 |
"strings" |
| 13 |
"sync" |
| 14 |
"sync/atomic" |
| 15 |
"testing" |
| 16 |
|
| 17 |
"github.com/go-logr/logr" |
| 18 |
"github.com/go-logr/logr/funcr" |
| 19 |
"github.com/go-logr/stdr" |
| 20 |
"github.com/stretchr/testify/assert" |
| 21 |
"github.com/stretchr/testify/require" |
| 22 |
|
| 23 |
"go.opentelemetry.io/otel" |
| 24 |
"go.opentelemetry.io/otel/attribute" |
| 25 |
"go.opentelemetry.io/otel/metric" |
| 26 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 27 |
"go.opentelemetry.io/otel/sdk/metric/exemplar" |
| 28 |
"go.opentelemetry.io/otel/sdk/metric/internal/aggregate" |
| 29 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 30 |
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" |
| 31 |
"go.opentelemetry.io/otel/sdk/resource" |
| 32 |
"go.opentelemetry.io/otel/trace" |
| 33 |
) |
| 34 |
|
| 35 |
func testSumAggregateOutput( |
| 36 |
dest *metricdata.Aggregation, //nolint:gocritic // The pointer is needed for the ComputeAggregation interface |
| 37 |
) int { |
| 38 |
*dest = metricdata.Sum[int64]{ |
| 39 |
Temporality: metricdata.CumulativeTemporality, |
| 40 |
IsMonotonic: false, |
| 41 |
DataPoints: []metricdata.DataPoint[int64]{{Value: 1}}, |
| 42 |
} |
| 43 |
return 1 |
| 44 |
} |
| 45 |
|
| 46 |
func TestNewPipeline(t *testing.T) { |
| 47 |
pipe := newPipeline(nil, nil, nil, exemplar.AlwaysOffFilter, 0) |
| 48 |
|
| 49 |
output := metricdata.ResourceMetrics{} |
| 50 |
err := pipe.produce(t.Context(), &output) |
| 51 |
require.NoError(t, err) |
| 52 |
assert.Equal(t, resource.Empty(), output.Resource) |
| 53 |
assert.Empty(t, output.ScopeMetrics) |
| 54 |
|
| 55 |
iSync := instrumentSync{"name", "desc", "1", testSumAggregateOutput} |
| 56 |
assert.NotPanics(t, func() { |
| 57 |
pipe.addSync(instrumentation.Scope{}, iSync) |
| 58 |
}) |
| 59 |
|
| 60 |
require.NotPanics(t, func() { |
| 61 |
pipe.addMultiCallback(func(context.Context) error { return nil }) |
| 62 |
}) |
| 63 |
|
| 64 |
err = pipe.produce(t.Context(), &output) |
| 65 |
require.NoError(t, err) |
| 66 |
assert.Equal(t, resource.Empty(), output.Resource) |
| 67 |
require.Len(t, output.ScopeMetrics, 1) |
| 68 |
require.Len(t, output.ScopeMetrics[0].Metrics, 1) |
| 69 |
} |
| 70 |
|
| 71 |
func TestPipelineUsesResource(t *testing.T) { |
| 72 |
res := resource.NewWithAttributes("noSchema", attribute.String("test", "resource")) |
| 73 |
pipe := newPipeline(res, nil, nil, exemplar.AlwaysOffFilter, 0) |
| 74 |
|
| 75 |
output := metricdata.ResourceMetrics{} |
| 76 |
err := pipe.produce(t.Context(), &output) |
| 77 |
assert.NoError(t, err) |
| 78 |
assert.Equal(t, res, output.Resource) |
| 79 |
} |
| 80 |
|
| 81 |
func TestPipelineConcurrentSafe(t *testing.T) { |
| 82 |
pipe := newPipeline(nil, nil, nil, exemplar.AlwaysOffFilter, 0) |
| 83 |
ctx := t.Context() |
| 84 |
var output metricdata.ResourceMetrics |
| 85 |
|
| 86 |
var wg sync.WaitGroup |
| 87 |
const threads = 2 |
| 88 |
for i := range threads { |
| 89 |
wg.Go(func() { |
| 90 |
_ = pipe.produce(ctx, &output) |
| 91 |
}) |
| 92 |
|
| 93 |
wg.Add(1) |
| 94 |
go func(n int) { |
| 95 |
defer wg.Done() |
| 96 |
name := fmt.Sprintf("name %d", n) |
| 97 |
sync := instrumentSync{name, "desc", "1", testSumAggregateOutput} |
| 98 |
pipe.addSync(instrumentation.Scope{}, sync) |
| 99 |
}(i) |
| 100 |
|
| 101 |
wg.Go(func() { |
| 102 |
pipe.addMultiCallback(func(context.Context) error { return nil }) |
| 103 |
}) |
| 104 |
|
| 105 |
wg.Go(func() { |
| 106 |
b := aggregate.Builder[int64]{ |
| 107 |
Temporality: metricdata.CumulativeTemporality, |
| 108 |
ReservoirFunc: nil, |
| 109 |
AggregationLimit: 0, |
| 110 |
} |
| 111 |
var oID observableID[int64] |
| 112 |
m, _ := b.PrecomputedSum(false) |
| 113 |
measures := []aggregate.Measure[int64]{} |
| 114 |
measures = append(measures, m) |
| 115 |
pipe.addInt64Measure(oID, measures) |
| 116 |
}) |
| 117 |
} |
| 118 |
wg.Wait() |
| 119 |
} |
| 120 |
|
| 121 |
func TestDefaultViewImplicit(t *testing.T) { |
| 122 |
t.Run("Int64", testDefaultViewImplicit[int64]()) |
| 123 |
t.Run("Float64", testDefaultViewImplicit[float64]()) |
| 124 |
} |
| 125 |
|
| 126 |
func testDefaultViewImplicit[N int64 | float64]() func(t *testing.T) { |
| 127 |
inst := Instrument{ |
| 128 |
Name: "requests", |
| 129 |
Description: "count of requests received", |
| 130 |
Kind: InstrumentKindCounter, |
| 131 |
Unit: "1", |
| 132 |
} |
| 133 |
return func(t *testing.T) { |
| 134 |
reader := NewManualReader() |
| 135 |
tests := []struct { |
| 136 |
name string |
| 137 |
pipe *pipeline |
| 138 |
}{ |
| 139 |
{ |
| 140 |
name: "NoView", |
| 141 |
pipe: newPipeline(nil, reader, nil, exemplar.AlwaysOffFilter, 0), |
| 142 |
}, |
| 143 |
{ |
| 144 |
name: "NoMatchingView", |
| 145 |
pipe: newPipeline(nil, reader, []View{ |
| 146 |
NewView(Instrument{Name: "foo"}, Stream{Name: "bar"}), |
| 147 |
}, exemplar.AlwaysOffFilter, 0), |
| 148 |
}, |
| 149 |
} |
| 150 |
|
| 151 |
for _, test := range tests { |
| 152 |
t.Run(test.name, func(t *testing.T) { |
| 153 |
var c cache[string, instID] |
| 154 |
i := newInserter[N](test.pipe, &c) |
| 155 |
readerAggregation := i.readerDefaultAggregation(inst.Kind) |
| 156 |
got, err := i.Instrument(inst, readerAggregation) |
| 157 |
require.NoError(t, err) |
| 158 |
assert.Len(t, got, 1, "default view not applied") |
| 159 |
for _, in := range got { |
| 160 |
in(t.Context(), 1, *attribute.EmptySet()) |
| 161 |
} |
| 162 |
|
| 163 |
out := metricdata.ResourceMetrics{} |
| 164 |
err = test.pipe.produce(t.Context(), &out) |
| 165 |
require.NoError(t, err) |
| 166 |
require.Len(t, out.ScopeMetrics, 1, "Aggregator not registered with pipeline") |
| 167 |
sm := out.ScopeMetrics[0] |
| 168 |
require.Len(t, sm.Metrics, 1, "metrics not produced from default view") |
| 169 |
metricdatatest.AssertEqual(t, metricdata.Metrics{ |
| 170 |
Name: inst.Name, |
| 171 |
Description: inst.Description, |
| 172 |
Unit: "1", |
| 173 |
Data: metricdata.Sum[N]{ |
| 174 |
Temporality: metricdata.CumulativeTemporality, |
| 175 |
IsMonotonic: true, |
| 176 |
DataPoints: []metricdata.DataPoint[N]{{Value: N(1)}}, |
| 177 |
}, |
| 178 |
}, sm.Metrics[0], metricdatatest.IgnoreTimestamp()) |
| 179 |
}) |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
func TestLogConflictName(t *testing.T) { |
| 185 |
testcases := []struct { |
| 186 |
existing, name string |
| 187 |
conflict bool |
| 188 |
}{ |
| 189 |
{ |
| 190 |
existing: "requestCount", |
| 191 |
name: "requestCount", |
| 192 |
conflict: false, |
| 193 |
}, |
| 194 |
{ |
| 195 |
existing: "requestCount", |
| 196 |
name: "requestDuration", |
| 197 |
conflict: false, |
| 198 |
}, |
| 199 |
{ |
| 200 |
existing: "requestCount", |
| 201 |
name: "requestcount", |
| 202 |
conflict: true, |
| 203 |
}, |
| 204 |
{ |
| 205 |
existing: "requestCount", |
| 206 |
name: "REQUESTCOUNT", |
| 207 |
conflict: true, |
| 208 |
}, |
| 209 |
{ |
| 210 |
existing: "requestCount", |
| 211 |
name: "rEqUeStCoUnT", |
| 212 |
conflict: true, |
| 213 |
}, |
| 214 |
} |
| 215 |
|
| 216 |
var msg string |
| 217 |
t.Cleanup(func(orig logr.Logger) func() { |
| 218 |
otel.SetLogger(funcr.New(func(_, args string) { |
| 219 |
msg = args |
| 220 |
}, funcr.Options{Verbosity: 20})) |
| 221 |
return func() { otel.SetLogger(orig) } |
| 222 |
}(stdr.New(log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile)))) |
| 223 |
|
| 224 |
for _, tc := range testcases { |
| 225 |
var vc cache[string, instID] |
| 226 |
|
| 227 |
name := strings.ToLower(tc.existing) |
| 228 |
_ = vc.Lookup(name, func() instID { |
| 229 |
return instID{Name: tc.existing} |
| 230 |
}) |
| 231 |
|
| 232 |
i := newInserter[int64](newPipeline(nil, nil, nil, exemplar.AlwaysOffFilter, 0), &vc) |
| 233 |
i.logConflict(instID{Name: tc.name}) |
| 234 |
|
| 235 |
if tc.conflict { |
| 236 |
assert.Containsf( |
| 237 |
t, msg, "duplicate metric stream definitions", |
| 238 |
"warning not logged for conflicting names: %s, %s", |
| 239 |
tc.existing, tc.name, |
| 240 |
) |
| 241 |
} else { |
| 242 |
assert.Emptyf( |
| 243 |
t, msg, |
| 244 |
"warning logged for non-conflicting names: %s, %s", |
| 245 |
tc.existing, tc.name, |
| 246 |
) |
| 247 |
} |
| 248 |
|
| 249 |
// Reset. |
| 250 |
msg = "" |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
func TestLogConflictSuggestView(t *testing.T) { |
| 255 |
var msg string |
| 256 |
t.Cleanup(func(orig logr.Logger) func() { |
| 257 |
otel.SetLogger(funcr.New(func(_, args string) { |
| 258 |
msg = args |
| 259 |
}, funcr.Options{Verbosity: 20})) |
| 260 |
return func() { otel.SetLogger(orig) } |
| 261 |
}(stdr.New(log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile)))) |
| 262 |
|
| 263 |
orig := instID{ |
| 264 |
Name: "requestCount", |
| 265 |
Description: "number of requests", |
| 266 |
Kind: InstrumentKindCounter, |
| 267 |
Unit: "1", |
| 268 |
Number: "int64", |
| 269 |
} |
| 270 |
|
| 271 |
var vc cache[string, instID] |
| 272 |
name := strings.ToLower(orig.Name) |
| 273 |
_ = vc.Lookup(name, func() instID { return orig }) |
| 274 |
i := newInserter[int64](newPipeline(nil, nil, nil, exemplar.AlwaysOffFilter, 0), &vc) |
| 275 |
|
| 276 |
viewSuggestion := func(inst instID, stream string) string { |
| 277 |
return `"NewView(Instrument{` + |
| 278 |
`Name: \"` + inst.Name + |
| 279 |
`\", Description: \"` + inst.Description + |
| 280 |
`\", Kind: \"InstrumentKind` + inst.Kind.String() + |
| 281 |
`\", Unit: \"` + inst.Unit + |
| 282 |
`\"}, ` + |
| 283 |
stream + |
| 284 |
`)"` |
| 285 |
} |
| 286 |
|
| 287 |
t.Run("Name", func(t *testing.T) { |
| 288 |
inst := instID{ |
| 289 |
Name: "requestcount", |
| 290 |
Description: orig.Description, |
| 291 |
Kind: orig.Kind, |
| 292 |
Unit: orig.Unit, |
| 293 |
Number: orig.Number, |
| 294 |
} |
| 295 |
i.logConflict(inst) |
| 296 |
assert.Containsf(t, msg, viewSuggestion( |
| 297 |
inst, `Stream{Name: \"{{NEW_NAME}}\"}`, |
| 298 |
), "no suggestion logged: %v", inst) |
| 299 |
|
| 300 |
// Reset. |
| 301 |
msg = "" |
| 302 |
}) |
| 303 |
|
| 304 |
t.Run("Description", func(t *testing.T) { |
| 305 |
inst := instID{ |
| 306 |
Name: orig.Name, |
| 307 |
Description: "alt", |
| 308 |
Kind: orig.Kind, |
| 309 |
Unit: orig.Unit, |
| 310 |
Number: orig.Number, |
| 311 |
} |
| 312 |
i.logConflict(inst) |
| 313 |
assert.Containsf(t, msg, viewSuggestion( |
| 314 |
inst, `Stream{Description: \"`+orig.Description+`\"}`, |
| 315 |
), "no suggestion logged: %v", inst) |
| 316 |
|
| 317 |
// Reset. |
| 318 |
msg = "" |
| 319 |
}) |
| 320 |
|
| 321 |
t.Run("Kind", func(t *testing.T) { |
| 322 |
inst := instID{ |
| 323 |
Name: orig.Name, |
| 324 |
Description: orig.Description, |
| 325 |
Kind: InstrumentKindHistogram, |
| 326 |
Unit: orig.Unit, |
| 327 |
Number: orig.Number, |
| 328 |
} |
| 329 |
i.logConflict(inst) |
| 330 |
assert.Containsf(t, msg, viewSuggestion( |
| 331 |
inst, `Stream{Name: \"{{NEW_NAME}}\"}`, |
| 332 |
), "no suggestion logged: %v", inst) |
| 333 |
|
| 334 |
// Reset. |
| 335 |
msg = "" |
| 336 |
}) |
| 337 |
|
| 338 |
t.Run("Unit", func(t *testing.T) { |
| 339 |
inst := instID{ |
| 340 |
Name: orig.Name, |
| 341 |
Description: orig.Description, |
| 342 |
Kind: orig.Kind, |
| 343 |
Unit: "ms", |
| 344 |
Number: orig.Number, |
| 345 |
} |
| 346 |
i.logConflict(inst) |
| 347 |
assert.NotContains(t, msg, "NewView", "suggestion logged: %v", inst) |
| 348 |
|
| 349 |
// Reset. |
| 350 |
msg = "" |
| 351 |
}) |
| 352 |
|
| 353 |
t.Run("Number", func(t *testing.T) { |
| 354 |
inst := instID{ |
| 355 |
Name: orig.Name, |
| 356 |
Description: orig.Description, |
| 357 |
Kind: orig.Kind, |
| 358 |
Unit: orig.Unit, |
| 359 |
Number: "float64", |
| 360 |
} |
| 361 |
i.logConflict(inst) |
| 362 |
assert.NotContains(t, msg, "NewView", "suggestion logged: %v", inst) |
| 363 |
|
| 364 |
// Reset. |
| 365 |
msg = "" |
| 366 |
}) |
| 367 |
} |
| 368 |
|
| 369 |
func TestInserterCachedAggregatorNameConflict(t *testing.T) { |
| 370 |
const name = "requestCount" |
| 371 |
scope := instrumentation.Scope{Name: "pipeline_test"} |
| 372 |
kind := InstrumentKindCounter |
| 373 |
stream := Stream{ |
| 374 |
Name: name, |
| 375 |
Aggregation: AggregationSum{}, |
| 376 |
} |
| 377 |
|
| 378 |
var vc cache[string, instID] |
| 379 |
pipe := newPipeline(nil, NewManualReader(), nil, exemplar.AlwaysOffFilter, 0) |
| 380 |
i := newInserter[int64](pipe, &vc) |
| 381 |
|
| 382 |
readerAggregation := i.readerDefaultAggregation(kind) |
| 383 |
_, origID, err := i.cachedAggregator(scope, kind, stream, readerAggregation) |
| 384 |
require.NoError(t, err) |
| 385 |
|
| 386 |
require.Len(t, pipe.aggregations, 1) |
| 387 |
require.Contains(t, pipe.aggregations, scope) |
| 388 |
iSync := pipe.aggregations[scope] |
| 389 |
require.Len(t, iSync, 1) |
| 390 |
require.Equal(t, name, iSync[0].name) |
| 391 |
|
| 392 |
stream.Name = "RequestCount" |
| 393 |
_, id, err := i.cachedAggregator(scope, kind, stream, readerAggregation) |
| 394 |
require.NoError(t, err) |
| 395 |
assert.Equal(t, origID, id, "multiple aggregators for equivalent name") |
| 396 |
|
| 397 |
assert.Len(t, pipe.aggregations, 1, "additional scope added") |
| 398 |
require.Contains(t, pipe.aggregations, scope, "original scope removed") |
| 399 |
iSync = pipe.aggregations[scope] |
| 400 |
require.Len(t, iSync, 1, "registered instrumentSync changed") |
| 401 |
assert.Equal(t, name, iSync[0].name, "stream name changed") |
| 402 |
} |
| 403 |
|
| 404 |
func TestExemplars(t *testing.T) { |
| 405 |
nCPU := runtime.NumCPU() |
| 406 |
setup := func(name string) (metric.Meter, Reader) { |
| 407 |
r := NewManualReader() |
| 408 |
v := NewView(Instrument{Name: "int64-expo-histogram"}, Stream{ |
| 409 |
Aggregation: AggregationBase2ExponentialHistogram{ |
| 410 |
MaxSize: 160, // > 20, reservoir size should default to 20. |
| 411 |
MaxScale: 20, |
| 412 |
}, |
| 413 |
}) |
| 414 |
return NewMeterProvider(WithReader(r), WithView(v)).Meter(name), r |
| 415 |
} |
| 416 |
|
| 417 |
measure := func(ctx context.Context, m metric.Meter) { |
| 418 |
i, err := m.Int64Counter("int64-counter") |
| 419 |
require.NoError(t, err) |
| 420 |
|
| 421 |
h, err := m.Int64Histogram("int64-histogram") |
| 422 |
require.NoError(t, err) |
| 423 |
|
| 424 |
e, err := m.Int64Histogram("int64-expo-histogram") |
| 425 |
require.NoError(t, err) |
| 426 |
|
| 427 |
for j := 0; j < 20*nCPU; j++ { // will be >= 20 and > nCPU |
| 428 |
i.Add(ctx, 1) |
| 429 |
h.Record(ctx, 1) |
| 430 |
e.Record(ctx, 1) |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
check := func(t *testing.T, r Reader, nSum, nHist, nExpo int) { |
| 435 |
t.Helper() |
| 436 |
|
| 437 |
rm := new(metricdata.ResourceMetrics) |
| 438 |
require.NoError(t, r.Collect(t.Context(), rm)) |
| 439 |
|
| 440 |
require.Len(t, rm.ScopeMetrics, 1, "ScopeMetrics") |
| 441 |
sm := rm.ScopeMetrics[0] |
| 442 |
require.Len(t, sm.Metrics, 3, "Metrics") |
| 443 |
|
| 444 |
require.IsType(t, metricdata.Sum[int64]{}, sm.Metrics[0].Data, sm.Metrics[0].Name) |
| 445 |
sum := sm.Metrics[0].Data.(metricdata.Sum[int64]) |
| 446 |
assert.Len(t, sum.DataPoints[0].Exemplars, nSum) |
| 447 |
|
| 448 |
require.IsType(t, metricdata.Histogram[int64]{}, sm.Metrics[1].Data, sm.Metrics[1].Name) |
| 449 |
hist := sm.Metrics[1].Data.(metricdata.Histogram[int64]) |
| 450 |
assert.Len(t, hist.DataPoints[0].Exemplars, nHist) |
| 451 |
|
| 452 |
require.IsType(t, metricdata.ExponentialHistogram[int64]{}, sm.Metrics[2].Data, sm.Metrics[2].Name) |
| 453 |
expo := sm.Metrics[2].Data.(metricdata.ExponentialHistogram[int64]) |
| 454 |
assert.Len(t, expo.DataPoints[0].Exemplars, nExpo) |
| 455 |
} |
| 456 |
|
| 457 |
ctx := t.Context() |
| 458 |
sc := trace.NewSpanContext(trace.SpanContextConfig{ |
| 459 |
SpanID: trace.SpanID{0o1}, |
| 460 |
TraceID: trace.TraceID{0o1}, |
| 461 |
TraceFlags: trace.FlagsSampled, |
| 462 |
}) |
| 463 |
sampled := trace.ContextWithSpanContext(t.Context(), sc) |
| 464 |
|
| 465 |
t.Run("Default", func(t *testing.T) { |
| 466 |
m, r := setup("default") |
| 467 |
measure(ctx, m) |
| 468 |
check(t, r, 0, 0, 0) |
| 469 |
|
| 470 |
measure(sampled, m) |
| 471 |
check(t, r, nCPU, 1, 20) |
| 472 |
}) |
| 473 |
|
| 474 |
t.Run("Invalid", func(t *testing.T) { |
| 475 |
t.Setenv("OTEL_METRICS_EXEMPLAR_FILTER", "unrecognized") |
| 476 |
m, r := setup("default") |
| 477 |
measure(ctx, m) |
| 478 |
check(t, r, 0, 0, 0) |
| 479 |
|
| 480 |
measure(sampled, m) |
| 481 |
check(t, r, nCPU, 1, 20) |
| 482 |
}) |
| 483 |
|
| 484 |
t.Run("always_on", func(t *testing.T) { |
| 485 |
t.Setenv("OTEL_METRICS_EXEMPLAR_FILTER", "always_on") |
| 486 |
m, r := setup("always_on") |
| 487 |
measure(ctx, m) |
| 488 |
check(t, r, nCPU, 1, 20) |
| 489 |
}) |
| 490 |
|
| 491 |
t.Run("always_off", func(t *testing.T) { |
| 492 |
t.Setenv("OTEL_METRICS_EXEMPLAR_FILTER", "always_off") |
| 493 |
m, r := setup("always_off") |
| 494 |
measure(ctx, m) |
| 495 |
check(t, r, 0, 0, 0) |
| 496 |
}) |
| 497 |
|
| 498 |
t.Run("trace_based", func(t *testing.T) { |
| 499 |
t.Setenv("OTEL_METRICS_EXEMPLAR_FILTER", "trace_based") |
| 500 |
m, r := setup("trace_based") |
| 501 |
measure(ctx, m) |
| 502 |
check(t, r, 0, 0, 0) |
| 503 |
|
| 504 |
measure(sampled, m) |
| 505 |
check(t, r, nCPU, 1, 20) |
| 506 |
}) |
| 507 |
|
| 508 |
t.Run("Custom reservoir", func(t *testing.T) { |
| 509 |
r := NewManualReader() |
| 510 |
reservoirProviderSelector := func(Aggregation) exemplar.ReservoirProvider { |
| 511 |
return exemplar.FixedSizeReservoirProvider(2) |
| 512 |
} |
| 513 |
v1 := NewView(Instrument{Name: "int64-expo-histogram"}, Stream{ |
| 514 |
Aggregation: AggregationBase2ExponentialHistogram{ |
| 515 |
MaxSize: 160, // > 20, reservoir size should default to 20. |
| 516 |
MaxScale: 20, |
| 517 |
}, |
| 518 |
ExemplarReservoirProviderSelector: reservoirProviderSelector, |
| 519 |
}) |
| 520 |
v2 := NewView(Instrument{Name: "int64-counter"}, Stream{ |
| 521 |
ExemplarReservoirProviderSelector: reservoirProviderSelector, |
| 522 |
}) |
| 523 |
v3 := NewView(Instrument{Name: "int64-histogram"}, Stream{ |
| 524 |
ExemplarReservoirProviderSelector: reservoirProviderSelector, |
| 525 |
}) |
| 526 |
m := NewMeterProvider(WithReader(r), WithView(v1, v2, v3)).Meter("custom-reservoir") |
| 527 |
measure(ctx, m) |
| 528 |
check(t, r, 0, 0, 0) |
| 529 |
|
| 530 |
measure(sampled, m) |
| 531 |
check(t, r, 2, 2, 2) |
| 532 |
}) |
| 533 |
} |
| 534 |
|
| 535 |
func TestAddingAndObservingMeasureConcurrentSafe(t *testing.T) { |
| 536 |
r1 := NewManualReader() |
| 537 |
r2 := NewManualReader() |
| 538 |
|
| 539 |
mp := NewMeterProvider(WithReader(r1), WithReader(r2)) |
| 540 |
m := mp.Meter("test") |
| 541 |
|
| 542 |
oc1, err := m.Int64ObservableCounter("int64-observable-counter") |
| 543 |
require.NoError(t, err) |
| 544 |
|
| 545 |
wg := sync.WaitGroup{} |
| 546 |
wg.Go(func() { |
| 547 |
_, err := m.Int64ObservableCounter("int64-observable-counter-2") |
| 548 |
require.NoError(t, err) |
| 549 |
}) |
| 550 |
|
| 551 |
wg.Go(func() { |
| 552 |
_, err := m.RegisterCallback( |
| 553 |
func(_ context.Context, o metric.Observer) error { |
| 554 |
o.ObserveInt64(oc1, 2) |
| 555 |
return nil |
| 556 |
}, oc1) |
| 557 |
require.NoError(t, err) |
| 558 |
}) |
| 559 |
|
| 560 |
wg.Go(func() { |
| 561 |
_ = mp.pipes[0].produce(t.Context(), &metricdata.ResourceMetrics{}) |
| 562 |
}) |
| 563 |
|
| 564 |
wg.Go(func() { |
| 565 |
_ = mp.pipes[1].produce(t.Context(), &metricdata.ResourceMetrics{}) |
| 566 |
}) |
| 567 |
|
| 568 |
wg.Wait() |
| 569 |
} |
| 570 |
|
| 571 |
func TestPipelineWithMultipleReaders(t *testing.T) { |
| 572 |
r1 := NewManualReader() |
| 573 |
r2 := NewManualReader() |
| 574 |
mp := NewMeterProvider(WithReader(r1), WithReader(r2)) |
| 575 |
m := mp.Meter("test") |
| 576 |
var val atomic.Int64 |
| 577 |
oc, err := m.Int64ObservableCounter("int64-observable-counter") |
| 578 |
require.NoError(t, err) |
| 579 |
reg, err := m.RegisterCallback( |
| 580 |
// SDK calls this function when collecting data. |
| 581 |
func(_ context.Context, o metric.Observer) error { |
| 582 |
o.ObserveInt64(oc, val.Load()) |
| 583 |
return nil |
| 584 |
}, oc) |
| 585 |
require.NoError(t, err) |
| 586 |
t.Cleanup(func() { assert.NoError(t, reg.Unregister()) }) |
| 587 |
ctx := t.Context() |
| 588 |
rm := new(metricdata.ResourceMetrics) |
| 589 |
val.Add(1) |
| 590 |
err = r1.Collect(ctx, rm) |
| 591 |
require.NoError(t, err) |
| 592 |
if assert.Len(t, rm.ScopeMetrics, 1) && |
| 593 |
assert.Len(t, rm.ScopeMetrics[0].Metrics, 1) { |
| 594 |
assert.Equal(t, int64(1), rm.ScopeMetrics[0].Metrics[0].Data.(metricdata.Sum[int64]).DataPoints[0].Value) |
| 595 |
} |
| 596 |
val.Add(1) |
| 597 |
err = r2.Collect(ctx, rm) |
| 598 |
require.NoError(t, err) |
| 599 |
if assert.Len(t, rm.ScopeMetrics, 1) && |
| 600 |
assert.Len(t, rm.ScopeMetrics[0].Metrics, 1) { |
| 601 |
assert.Equal(t, int64(2), rm.ScopeMetrics[0].Metrics[0].Data.(metricdata.Sum[int64]).DataPoints[0].Value) |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
// TestPipelineProduceErrors tests the issue described in https://github.com/open-telemetry/opentelemetry-go/issues/6344. |
| 606 |
// Earlier implementations of the pipeline produce method could corrupt metric data point state when the passed context |
| 607 |
// was canceled during execution of callbacks. In this case, corroption was the result of some or all callbacks being |
| 608 |
// invoked without instrument compAgg functions called. |
| 609 |
func TestPipelineProduceErrors(t *testing.T) { |
| 610 |
// Create a test pipeline with aggregations |
| 611 |
pipeReader := NewManualReader() |
| 612 |
pipe := newPipeline(nil, pipeReader, nil, exemplar.AlwaysOffFilter, 0) |
| 613 |
|
| 614 |
// Set up an observable with callbacks |
| 615 |
var testObsID observableID[int64] |
| 616 |
aggBuilder := aggregate.Builder[int64]{Temporality: metricdata.CumulativeTemporality} |
| 617 |
measure, _ := aggBuilder.Sum(true) |
| 618 |
pipe.addInt64Measure(testObsID, []aggregate.Measure[int64]{measure}) |
| 619 |
|
| 620 |
// Add an aggregation that just sets the data point value to the number of times the aggregation is invoked |
| 621 |
aggCallCount := 0 |
| 622 |
inst := instrumentSync{ |
| 623 |
name: "test-metric", |
| 624 |
description: "test description", |
| 625 |
unit: "test unit", |
| 626 |
compAgg: func(dest *metricdata.Aggregation) int { |
| 627 |
aggCallCount++ |
| 628 |
|
| 629 |
*dest = metricdata.Sum[int64]{ |
| 630 |
Temporality: metricdata.CumulativeTemporality, |
| 631 |
IsMonotonic: false, |
| 632 |
DataPoints: []metricdata.DataPoint[int64]{{Value: int64(aggCallCount)}}, |
| 633 |
} |
| 634 |
return aggCallCount |
| 635 |
}, |
| 636 |
} |
| 637 |
pipe.addSync(instrumentation.Scope{Name: "test"}, inst) |
| 638 |
|
| 639 |
ctx, cancelCtx := context.WithCancel(t.Context()) |
| 640 |
var shouldCancelContext bool // When true, the second callback cancels ctx |
| 641 |
var shouldReturnError bool // When true, the third callback returns an error |
| 642 |
var callbackCounts [3]int |
| 643 |
|
| 644 |
pipe.callbacks = append(pipe.callbacks, |
| 645 |
// Callback 1: cancels the context during execution but continues to populate data |
| 646 |
func(ctx context.Context) error { |
| 647 |
callbackCounts[0]++ |
| 648 |
for _, m := range pipe.int64Measures[testObsID] { |
| 649 |
m(ctx, 123, *attribute.EmptySet()) |
| 650 |
} |
| 651 |
return nil |
| 652 |
}, |
| 653 |
// Callback 2: populates int64 observable data |
| 654 |
func(context.Context) error { |
| 655 |
callbackCounts[1]++ |
| 656 |
if shouldCancelContext { |
| 657 |
cancelCtx() |
| 658 |
} |
| 659 |
return nil |
| 660 |
}, |
| 661 |
// Callback 3: return an error |
| 662 |
func(context.Context) error { |
| 663 |
callbackCounts[2]++ |
| 664 |
if shouldReturnError { |
| 665 |
return fmt.Errorf("test callback error") |
| 666 |
} |
| 667 |
return nil |
| 668 |
}) |
| 669 |
|
| 670 |
assertMetrics := func(rm *metricdata.ResourceMetrics, expectVal int64) { |
| 671 |
require.Len(t, rm.ScopeMetrics, 1) |
| 672 |
require.Len(t, rm.ScopeMetrics[0].Metrics, 1) |
| 673 |
metricdatatest.AssertEqual(t, metricdata.Metrics{ |
| 674 |
Name: inst.name, |
| 675 |
Description: inst.description, |
| 676 |
Unit: inst.unit, |
| 677 |
Data: metricdata.Sum[int64]{ |
| 678 |
Temporality: metricdata.CumulativeTemporality, |
| 679 |
IsMonotonic: false, |
| 680 |
DataPoints: []metricdata.DataPoint[int64]{{Value: expectVal}}, |
| 681 |
}, |
| 682 |
}, rm.ScopeMetrics[0].Metrics[0], metricdatatest.IgnoreTimestamp()) |
| 683 |
} |
| 684 |
|
| 685 |
t.Run("no errors", func(t *testing.T) { |
| 686 |
var rm metricdata.ResourceMetrics |
| 687 |
err := pipe.produce(ctx, &rm) |
| 688 |
require.NoError(t, err) |
| 689 |
|
| 690 |
assert.Equal(t, [3]int{1, 1, 1}, callbackCounts) |
| 691 |
assert.Equal(t, 1, aggCallCount) |
| 692 |
|
| 693 |
assertMetrics(&rm, 1) |
| 694 |
}) |
| 695 |
|
| 696 |
t.Run("callback returns error", func(t *testing.T) { |
| 697 |
shouldReturnError = true |
| 698 |
|
| 699 |
var rm metricdata.ResourceMetrics |
| 700 |
err := pipe.produce(ctx, &rm) |
| 701 |
require.ErrorContains(t, err, "test callback error") |
| 702 |
|
| 703 |
// Even though a callback returned an error, the agg function is still called |
| 704 |
assert.Equal(t, [3]int{2, 2, 2}, callbackCounts) |
| 705 |
assert.Equal(t, 2, aggCallCount) |
| 706 |
|
| 707 |
assertMetrics(&rm, 2) |
| 708 |
}) |
| 709 |
|
| 710 |
t.Run("context canceled during produce", func(t *testing.T) { |
| 711 |
shouldCancelContext = true |
| 712 |
|
| 713 |
var rm metricdata.ResourceMetrics |
| 714 |
err := pipe.produce(ctx, &rm) |
| 715 |
require.ErrorContains(t, err, "test callback error") |
| 716 |
|
| 717 |
// Even though the context was canceled midway through invoking callbacks, |
| 718 |
// all remaining callbacks and agg functions are still called |
| 719 |
assert.Equal(t, [3]int{3, 3, 3}, callbackCounts) |
| 720 |
assert.Equal(t, 3, aggCallCount) |
| 721 |
}) |
| 722 |
|
| 723 |
t.Run("context already cancelled", func(t *testing.T) { |
| 724 |
var output metricdata.ResourceMetrics |
| 725 |
err := pipe.produce(ctx, &output) |
| 726 |
require.ErrorIs(t, err, context.Canceled) |
| 727 |
|
| 728 |
// No callbacks or agg functions are called since the context was canceled prior to invoking |
| 729 |
// the produce method |
| 730 |
assert.Equal(t, [3]int{3, 3, 3}, callbackCounts) |
| 731 |
assert.Equal(t, 3, aggCallCount) |
| 732 |
}) |
| 733 |
} |