| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package prometheus |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/prometheus/client_golang/prometheus" |
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
"go.opentelemetry.io/otel/attribute" |
| 14 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 15 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 16 |
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" |
| 17 |
) |
| 18 |
|
| 19 |
const ( |
| 20 |
traceIDStr = "4bf92f3577b34da6a3ce929d0e0e4736" |
| 21 |
spanIDStr = "00f067aa0ba902b7" |
| 22 |
) |
| 23 |
|
| 24 |
func TestProduce(t *testing.T) { |
| 25 |
testCases := []struct { |
| 26 |
name string |
| 27 |
testFn func(*prometheus.Registry) |
| 28 |
expected []metricdata.ScopeMetrics |
| 29 |
wantErr error |
| 30 |
}{ |
| 31 |
{ |
| 32 |
name: "no metrics registered", |
| 33 |
testFn: func(*prometheus.Registry) {}, |
| 34 |
}, |
| 35 |
{ |
| 36 |
name: "gauge", |
| 37 |
testFn: func(reg *prometheus.Registry) { |
| 38 |
metric := prometheus.NewGauge(prometheus.GaugeOpts{ |
| 39 |
Name: "test_gauge_metric", |
| 40 |
Help: "A gauge metric for testing", |
| 41 |
ConstLabels: prometheus.Labels(map[string]string{ |
| 42 |
"foo": "bar", |
| 43 |
}), |
| 44 |
}) |
| 45 |
reg.MustRegister(metric) |
| 46 |
metric.Set(123.4) |
| 47 |
}, |
| 48 |
expected: []metricdata.ScopeMetrics{{ |
| 49 |
Scope: instrumentation.Scope{ |
| 50 |
Name: scopeName, |
| 51 |
}, |
| 52 |
Metrics: []metricdata.Metrics{ |
| 53 |
{ |
| 54 |
Name: "test_gauge_metric", |
| 55 |
Description: "A gauge metric for testing", |
| 56 |
Data: metricdata.Gauge[float64]{ |
| 57 |
DataPoints: []metricdata.DataPoint[float64]{ |
| 58 |
{ |
| 59 |
Attributes: attribute.NewSet(attribute.String("foo", "bar")), |
| 60 |
Value: 123.4, |
| 61 |
}, |
| 62 |
}, |
| 63 |
}, |
| 64 |
}, |
| 65 |
}, |
| 66 |
}}, |
| 67 |
}, |
| 68 |
{ |
| 69 |
name: "counter", |
| 70 |
testFn: func(reg *prometheus.Registry) { |
| 71 |
metric := prometheus.NewCounter(prometheus.CounterOpts{ |
| 72 |
Name: "test_counter_metric", |
| 73 |
Help: "A counter metric for testing", |
| 74 |
ConstLabels: prometheus.Labels(map[string]string{ |
| 75 |
"foo": "bar", |
| 76 |
}), |
| 77 |
}) |
| 78 |
reg.MustRegister(metric) |
| 79 |
metric.Add(245.3) |
| 80 |
}, |
| 81 |
expected: []metricdata.ScopeMetrics{{ |
| 82 |
Scope: instrumentation.Scope{ |
| 83 |
Name: scopeName, |
| 84 |
}, |
| 85 |
Metrics: []metricdata.Metrics{ |
| 86 |
{ |
| 87 |
Name: "test_counter_metric", |
| 88 |
Description: "A counter metric for testing", |
| 89 |
Data: metricdata.Sum[float64]{ |
| 90 |
Temporality: metricdata.CumulativeTemporality, |
| 91 |
IsMonotonic: true, |
| 92 |
DataPoints: []metricdata.DataPoint[float64]{ |
| 93 |
{ |
| 94 |
Attributes: attribute.NewSet(attribute.String("foo", "bar")), |
| 95 |
Value: 245.3, |
| 96 |
}, |
| 97 |
}, |
| 98 |
}, |
| 99 |
}, |
| 100 |
}, |
| 101 |
}}, |
| 102 |
}, |
| 103 |
{ |
| 104 |
name: "counter with exemplar", |
| 105 |
testFn: func(reg *prometheus.Registry) { |
| 106 |
metric := prometheus.NewCounter(prometheus.CounterOpts{ |
| 107 |
Name: "test_counter_metric", |
| 108 |
Help: "A counter metric for testing", |
| 109 |
ConstLabels: prometheus.Labels(map[string]string{ |
| 110 |
"foo": "bar", |
| 111 |
}), |
| 112 |
}) |
| 113 |
reg.MustRegister(metric) |
| 114 |
metric.(prometheus.ExemplarAdder).AddWithExemplar( |
| 115 |
245.3, prometheus.Labels{ |
| 116 |
"trace_id": traceIDStr, |
| 117 |
"span_id": spanIDStr, |
| 118 |
"other_attribute": "abcd", |
| 119 |
}, |
| 120 |
) |
| 121 |
}, |
| 122 |
expected: []metricdata.ScopeMetrics{{ |
| 123 |
Scope: instrumentation.Scope{ |
| 124 |
Name: scopeName, |
| 125 |
}, |
| 126 |
Metrics: []metricdata.Metrics{ |
| 127 |
{ |
| 128 |
Name: "test_counter_metric", |
| 129 |
Description: "A counter metric for testing", |
| 130 |
Data: metricdata.Sum[float64]{ |
| 131 |
Temporality: metricdata.CumulativeTemporality, |
| 132 |
IsMonotonic: true, |
| 133 |
DataPoints: []metricdata.DataPoint[float64]{ |
| 134 |
{ |
| 135 |
Attributes: attribute.NewSet(attribute.String("foo", "bar")), |
| 136 |
Value: 245.3, |
| 137 |
Exemplars: []metricdata.Exemplar[float64]{ |
| 138 |
{ |
| 139 |
Value: 245.3, |
| 140 |
TraceID: []byte(traceIDStr), |
| 141 |
SpanID: []byte(spanIDStr), |
| 142 |
FilteredAttributes: []attribute.KeyValue{attribute.String("other_attribute", "abcd")}, |
| 143 |
}, |
| 144 |
}, |
| 145 |
}, |
| 146 |
}, |
| 147 |
}, |
| 148 |
}, |
| 149 |
}, |
| 150 |
}}, |
| 151 |
}, |
| 152 |
{ |
| 153 |
name: "summary", |
| 154 |
testFn: func(reg *prometheus.Registry) { |
| 155 |
metric := prometheus.NewSummary(prometheus.SummaryOpts{ |
| 156 |
Name: "test_summary_metric", |
| 157 |
Help: "A summary metric for testing", |
| 158 |
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, |
| 159 |
ConstLabels: prometheus.Labels(map[string]string{ |
| 160 |
"foo": "bar", |
| 161 |
}), |
| 162 |
}) |
| 163 |
reg.MustRegister(metric) |
| 164 |
metric.Observe(15.0) |
| 165 |
}, |
| 166 |
expected: []metricdata.ScopeMetrics{{ |
| 167 |
Scope: instrumentation.Scope{ |
| 168 |
Name: scopeName, |
| 169 |
}, |
| 170 |
Metrics: []metricdata.Metrics{ |
| 171 |
{ |
| 172 |
Name: "test_summary_metric", |
| 173 |
Description: "A summary metric for testing", |
| 174 |
Data: metricdata.Summary{ |
| 175 |
DataPoints: []metricdata.SummaryDataPoint{ |
| 176 |
{ |
| 177 |
Count: 1, |
| 178 |
Sum: 15.0, |
| 179 |
QuantileValues: []metricdata.QuantileValue{ |
| 180 |
{Quantile: 0.5, Value: 15}, |
| 181 |
{Quantile: 0.9, Value: 15}, |
| 182 |
{Quantile: 0.99, Value: 15}, |
| 183 |
}, |
| 184 |
Attributes: attribute.NewSet(attribute.String("foo", "bar")), |
| 185 |
}, |
| 186 |
}, |
| 187 |
}, |
| 188 |
}, |
| 189 |
}, |
| 190 |
}}, |
| 191 |
}, |
| 192 |
{ |
| 193 |
name: "histogram", |
| 194 |
testFn: func(reg *prometheus.Registry) { |
| 195 |
metric := prometheus.NewHistogram(prometheus.HistogramOpts{ |
| 196 |
Name: "test_histogram_metric", |
| 197 |
Help: "A histogram metric for testing", |
| 198 |
ConstLabels: prometheus.Labels(map[string]string{ |
| 199 |
"foo": "bar", |
| 200 |
}), |
| 201 |
}) |
| 202 |
reg.MustRegister(metric) |
| 203 |
metric.Observe(578.3) |
| 204 |
}, |
| 205 |
expected: []metricdata.ScopeMetrics{{ |
| 206 |
Scope: instrumentation.Scope{ |
| 207 |
Name: scopeName, |
| 208 |
}, |
| 209 |
Metrics: []metricdata.Metrics{ |
| 210 |
{ |
| 211 |
Name: "test_histogram_metric", |
| 212 |
Description: "A histogram metric for testing", |
| 213 |
Data: metricdata.Histogram[float64]{ |
| 214 |
Temporality: metricdata.CumulativeTemporality, |
| 215 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 216 |
{ |
| 217 |
Count: 1, |
| 218 |
Sum: 578.3, |
| 219 |
Bounds: []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10}, |
| 220 |
BucketCounts: []uint64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, |
| 221 |
Attributes: attribute.NewSet(attribute.String("foo", "bar")), |
| 222 |
Exemplars: []metricdata.Exemplar[float64]{}, |
| 223 |
}, |
| 224 |
}, |
| 225 |
}, |
| 226 |
}, |
| 227 |
}, |
| 228 |
}}, |
| 229 |
}, |
| 230 |
{ |
| 231 |
name: "histogram cumulative values to non-cumulative", |
| 232 |
testFn: func(reg *prometheus.Registry) { |
| 233 |
metric := prometheus.NewHistogram(prometheus.HistogramOpts{ |
| 234 |
Name: "test_histogram_metric", |
| 235 |
Help: "A histogram metric for testing", |
| 236 |
ConstLabels: prometheus.Labels(map[string]string{ |
| 237 |
"foo": "bar", |
| 238 |
}), |
| 239 |
}) |
| 240 |
reg.MustRegister(metric) |
| 241 |
metric.Observe(0.01) |
| 242 |
}, |
| 243 |
expected: []metricdata.ScopeMetrics{{ |
| 244 |
Scope: instrumentation.Scope{ |
| 245 |
Name: scopeName, |
| 246 |
}, |
| 247 |
Metrics: []metricdata.Metrics{ |
| 248 |
{ |
| 249 |
Name: "test_histogram_metric", |
| 250 |
Description: "A histogram metric for testing", |
| 251 |
Data: metricdata.Histogram[float64]{ |
| 252 |
Temporality: metricdata.CumulativeTemporality, |
| 253 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 254 |
{ |
| 255 |
Count: 1, |
| 256 |
Sum: 0.01, |
| 257 |
Bounds: []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10}, |
| 258 |
BucketCounts: []uint64{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 259 |
Attributes: attribute.NewSet(attribute.String("foo", "bar")), |
| 260 |
Exemplars: []metricdata.Exemplar[float64]{}, |
| 261 |
}, |
| 262 |
}, |
| 263 |
}, |
| 264 |
}, |
| 265 |
}, |
| 266 |
}}, |
| 267 |
}, |
| 268 |
{ |
| 269 |
name: "histogram with exemplar", |
| 270 |
testFn: func(reg *prometheus.Registry) { |
| 271 |
metric := prometheus.NewHistogram(prometheus.HistogramOpts{ |
| 272 |
Name: "test_histogram_metric_with_exemplar", |
| 273 |
Help: "A histogram metric for testing", |
| 274 |
ConstLabels: prometheus.Labels(map[string]string{ |
| 275 |
"foo": "bar", |
| 276 |
}), |
| 277 |
}) |
| 278 |
reg.MustRegister(metric) |
| 279 |
metric.(prometheus.ExemplarObserver).ObserveWithExemplar( |
| 280 |
578.3, prometheus.Labels{ |
| 281 |
"trace_id": traceIDStr, |
| 282 |
"span_id": spanIDStr, |
| 283 |
"other_attribute": "efgh", |
| 284 |
}, |
| 285 |
) |
| 286 |
}, |
| 287 |
expected: []metricdata.ScopeMetrics{{ |
| 288 |
Scope: instrumentation.Scope{ |
| 289 |
Name: scopeName, |
| 290 |
}, |
| 291 |
Metrics: []metricdata.Metrics{ |
| 292 |
{ |
| 293 |
Name: "test_histogram_metric_with_exemplar", |
| 294 |
Description: "A histogram metric for testing", |
| 295 |
Data: metricdata.Histogram[float64]{ |
| 296 |
Temporality: metricdata.CumulativeTemporality, |
| 297 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 298 |
{ |
| 299 |
Count: 1, |
| 300 |
Sum: 578.3, |
| 301 |
Bounds: []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10}, |
| 302 |
BucketCounts: []uint64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, |
| 303 |
Attributes: attribute.NewSet(attribute.String("foo", "bar")), |
| 304 |
Exemplars: []metricdata.Exemplar[float64]{ |
| 305 |
{ |
| 306 |
Value: 578.3, |
| 307 |
TraceID: []byte(traceIDStr), |
| 308 |
SpanID: []byte(spanIDStr), |
| 309 |
FilteredAttributes: []attribute.KeyValue{ |
| 310 |
attribute.String("other_attribute", "efgh"), |
| 311 |
}, |
| 312 |
}, |
| 313 |
}, |
| 314 |
}, |
| 315 |
}, |
| 316 |
}, |
| 317 |
}, |
| 318 |
}, |
| 319 |
}}, |
| 320 |
}, |
| 321 |
{ |
| 322 |
name: "exponential histogram", |
| 323 |
testFn: func(reg *prometheus.Registry) { |
| 324 |
metric := prometheus.NewHistogram(prometheus.HistogramOpts{ |
| 325 |
Name: "test_exponential_histogram_metric", |
| 326 |
Help: "An exponential histogram metric for testing", |
| 327 |
|
| 328 |
NativeHistogramBucketFactor: 1.5, |
| 329 |
ConstLabels: prometheus.Labels(map[string]string{ |
| 330 |
"foo": "bar", |
| 331 |
}), |
| 332 |
}) |
| 333 |
reg.MustRegister(metric) |
| 334 |
metric.Observe(78.3) |
| 335 |
metric.Observe(2.3) |
| 336 |
metric.Observe(2.3) |
| 337 |
metric.Observe(.5) |
| 338 |
metric.Observe(-78.3) |
| 339 |
metric.Observe(-.15) |
| 340 |
metric.Observe(0.0) |
| 341 |
}, |
| 342 |
expected: []metricdata.ScopeMetrics{{ |
| 343 |
Scope: instrumentation.Scope{ |
| 344 |
Name: scopeName, |
| 345 |
}, |
| 346 |
Metrics: []metricdata.Metrics{ |
| 347 |
{ |
| 348 |
Name: "test_exponential_histogram_metric", |
| 349 |
Description: "An exponential histogram metric for testing", |
| 350 |
Data: metricdata.ExponentialHistogram[float64]{ |
| 351 |
Temporality: metricdata.CumulativeTemporality, |
| 352 |
DataPoints: []metricdata.ExponentialHistogramDataPoint[float64]{ |
| 353 |
{ |
| 354 |
Count: 7, |
| 355 |
Sum: 4.949999999999994, |
| 356 |
Scale: 1, |
| 357 |
ZeroCount: 1, |
| 358 |
PositiveBucket: metricdata.ExponentialBucket{ |
| 359 |
Offset: -3, |
| 360 |
Counts: []uint64{1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, |
| 361 |
}, |
| 362 |
NegativeBucket: metricdata.ExponentialBucket{ |
| 363 |
Offset: -6, |
| 364 |
Counts: []uint64{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, |
| 365 |
}, |
| 366 |
Attributes: attribute.NewSet(attribute.String("foo", "bar")), |
| 367 |
ZeroThreshold: prometheus.DefNativeHistogramZeroThreshold, |
| 368 |
}, |
| 369 |
}, |
| 370 |
}, |
| 371 |
}, |
| 372 |
}, |
| 373 |
}}, |
| 374 |
}, |
| 375 |
{ |
| 376 |
name: "exponential histogram with only positive observations", |
| 377 |
testFn: func(reg *prometheus.Registry) { |
| 378 |
metric := prometheus.NewHistogram(prometheus.HistogramOpts{ |
| 379 |
Name: "test_exponential_histogram_metric", |
| 380 |
Help: "An exponential histogram metric for testing", |
| 381 |
|
| 382 |
NativeHistogramBucketFactor: 1.5, |
| 383 |
ConstLabels: prometheus.Labels(map[string]string{ |
| 384 |
"foo": "bar", |
| 385 |
}), |
| 386 |
}) |
| 387 |
reg.MustRegister(metric) |
| 388 |
metric.Observe(78.3) |
| 389 |
metric.Observe(2.3) |
| 390 |
metric.Observe(2.3) |
| 391 |
metric.Observe(.5) |
| 392 |
metric.Observe(0.0) |
| 393 |
}, |
| 394 |
expected: []metricdata.ScopeMetrics{{ |
| 395 |
Scope: instrumentation.Scope{ |
| 396 |
Name: scopeName, |
| 397 |
}, |
| 398 |
Metrics: []metricdata.Metrics{ |
| 399 |
{ |
| 400 |
Name: "test_exponential_histogram_metric", |
| 401 |
Description: "An exponential histogram metric for testing", |
| 402 |
Data: metricdata.ExponentialHistogram[float64]{ |
| 403 |
Temporality: metricdata.CumulativeTemporality, |
| 404 |
DataPoints: []metricdata.ExponentialHistogramDataPoint[float64]{ |
| 405 |
{ |
| 406 |
Count: 5, |
| 407 |
Sum: 83.39999999999999, |
| 408 |
Scale: 1, |
| 409 |
ZeroCount: 1, |
| 410 |
PositiveBucket: metricdata.ExponentialBucket{ |
| 411 |
Offset: -3, |
| 412 |
Counts: []uint64{1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, |
| 413 |
}, |
| 414 |
NegativeBucket: metricdata.ExponentialBucket{}, |
| 415 |
Attributes: attribute.NewSet(attribute.String("foo", "bar")), |
| 416 |
ZeroThreshold: prometheus.DefNativeHistogramZeroThreshold, |
| 417 |
}, |
| 418 |
}, |
| 419 |
}, |
| 420 |
}, |
| 421 |
}, |
| 422 |
}}, |
| 423 |
}, |
| 424 |
{ |
| 425 |
name: "partial success", |
| 426 |
testFn: func(reg *prometheus.Registry) { |
| 427 |
metric := prometheus.NewGauge(prometheus.GaugeOpts{ |
| 428 |
Name: "test_gauge_metric", |
| 429 |
Help: "A gauge metric for testing", |
| 430 |
ConstLabels: prometheus.Labels(map[string]string{ |
| 431 |
"foo": "bar", |
| 432 |
}), |
| 433 |
}) |
| 434 |
reg.MustRegister(metric) |
| 435 |
metric.Set(123.4) |
| 436 |
unsupportedMetric := prometheus.NewUntypedFunc(prometheus.UntypedOpts{ |
| 437 |
Name: "test_untyped_metric", |
| 438 |
Help: "An untyped metric for testing", |
| 439 |
}, func() float64 { |
| 440 |
return 135.8 |
| 441 |
}) |
| 442 |
reg.MustRegister(unsupportedMetric) |
| 443 |
}, |
| 444 |
expected: []metricdata.ScopeMetrics{{ |
| 445 |
Scope: instrumentation.Scope{ |
| 446 |
Name: scopeName, |
| 447 |
}, |
| 448 |
Metrics: []metricdata.Metrics{ |
| 449 |
{ |
| 450 |
Name: "test_gauge_metric", |
| 451 |
Description: "A gauge metric for testing", |
| 452 |
Data: metricdata.Gauge[float64]{ |
| 453 |
DataPoints: []metricdata.DataPoint[float64]{ |
| 454 |
{ |
| 455 |
Attributes: attribute.NewSet(attribute.String("foo", "bar")), |
| 456 |
Value: 123.4, |
| 457 |
}, |
| 458 |
}, |
| 459 |
}, |
| 460 |
}, |
| 461 |
}, |
| 462 |
}}, |
| 463 |
wantErr: errUnsupportedType, |
| 464 |
}, |
| 465 |
} |
| 466 |
for _, tt := range testCases { |
| 467 |
t.Run(tt.name, func(t *testing.T) { |
| 468 |
reg := prometheus.NewRegistry() |
| 469 |
tt.testFn(reg) |
| 470 |
p := NewMetricProducer(WithGatherer(reg)) |
| 471 |
output, err := p.Produce(t.Context()) |
| 472 |
if tt.wantErr == nil { |
| 473 |
assert.NoError(t, err) |
| 474 |
} |
| 475 |
require.Len(t, output, len(tt.expected)) |
| 476 |
for i := range output { |
| 477 |
metricdatatest.AssertEqual(t, tt.expected[i], output[i], metricdatatest.IgnoreTimestamp()) |
| 478 |
} |
| 479 |
}) |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
func TestProduceForStartTime(t *testing.T) { |
| 484 |
testCases := []struct { |
| 485 |
name string |
| 486 |
testFn func(*prometheus.Registry) |
| 487 |
startTimeFn func(metricdata.Aggregation) []time.Time |
| 488 |
}{ |
| 489 |
{ |
| 490 |
name: "counter", |
| 491 |
testFn: func(reg *prometheus.Registry) { |
| 492 |
metric := prometheus.NewCounter(prometheus.CounterOpts{ |
| 493 |
Name: "test_counter_metric", |
| 494 |
Help: "A counter metric for testing", |
| 495 |
ConstLabels: prometheus.Labels(map[string]string{ |
| 496 |
"foo": "bar", |
| 497 |
}), |
| 498 |
}) |
| 499 |
reg.MustRegister(metric) |
| 500 |
metric.(prometheus.ExemplarAdder).AddWithExemplar( |
| 501 |
245.3, prometheus.Labels{ |
| 502 |
"trace_id": traceIDStr, |
| 503 |
"span_id": spanIDStr, |
| 504 |
"other_attribute": "abcd", |
| 505 |
}, |
| 506 |
) |
| 507 |
}, |
| 508 |
startTimeFn: func(aggr metricdata.Aggregation) []time.Time { |
| 509 |
dps := aggr.(metricdata.Sum[float64]).DataPoints |
| 510 |
sts := make([]time.Time, len(dps)) |
| 511 |
for i, dp := range dps { |
| 512 |
sts[i] = dp.StartTime |
| 513 |
} |
| 514 |
return sts |
| 515 |
}, |
| 516 |
}, |
| 517 |
{ |
| 518 |
name: "histogram", |
| 519 |
testFn: func(reg *prometheus.Registry) { |
| 520 |
metric := prometheus.NewHistogram(prometheus.HistogramOpts{ |
| 521 |
Name: "test_histogram_metric", |
| 522 |
Help: "A histogram metric for testing", |
| 523 |
ConstLabels: prometheus.Labels(map[string]string{ |
| 524 |
"foo": "bar", |
| 525 |
}), |
| 526 |
}) |
| 527 |
reg.MustRegister(metric) |
| 528 |
metric.(prometheus.ExemplarObserver).ObserveWithExemplar( |
| 529 |
578.3, prometheus.Labels{ |
| 530 |
"trace_id": traceIDStr, |
| 531 |
"span_id": spanIDStr, |
| 532 |
"other_attribute": "efgh", |
| 533 |
}, |
| 534 |
) |
| 535 |
}, |
| 536 |
startTimeFn: func(aggr metricdata.Aggregation) []time.Time { |
| 537 |
dps := aggr.(metricdata.Histogram[float64]).DataPoints |
| 538 |
sts := make([]time.Time, len(dps)) |
| 539 |
for i, dp := range dps { |
| 540 |
sts[i] = dp.StartTime |
| 541 |
} |
| 542 |
return sts |
| 543 |
}, |
| 544 |
}, |
| 545 |
{ |
| 546 |
name: "summary", |
| 547 |
testFn: func(reg *prometheus.Registry) { |
| 548 |
metric := prometheus.NewSummary(prometheus.SummaryOpts{ |
| 549 |
Name: "test_summary_metric", |
| 550 |
Help: "A summary metric for testing", |
| 551 |
ConstLabels: prometheus.Labels(map[string]string{ |
| 552 |
"foo": "bar", |
| 553 |
}), |
| 554 |
}) |
| 555 |
reg.MustRegister(metric) |
| 556 |
metric.Observe(78.3) |
| 557 |
}, |
| 558 |
startTimeFn: func(aggr metricdata.Aggregation) []time.Time { |
| 559 |
dps := aggr.(metricdata.Summary).DataPoints |
| 560 |
sts := make([]time.Time, len(dps)) |
| 561 |
for i, dp := range dps { |
| 562 |
sts[i] = dp.StartTime |
| 563 |
} |
| 564 |
return sts |
| 565 |
}, |
| 566 |
}, |
| 567 |
{ |
| 568 |
name: "exponential histogram", |
| 569 |
testFn: func(reg *prometheus.Registry) { |
| 570 |
metric := prometheus.NewHistogram(prometheus.HistogramOpts{ |
| 571 |
Name: "test_exponential_histogram_metric", |
| 572 |
Help: "An exponential histogram metric for testing", |
| 573 |
|
| 574 |
NativeHistogramBucketFactor: 1.5, |
| 575 |
ConstLabels: prometheus.Labels(map[string]string{ |
| 576 |
"foo": "bar", |
| 577 |
}), |
| 578 |
}) |
| 579 |
reg.MustRegister(metric) |
| 580 |
metric.Observe(78.3) |
| 581 |
}, |
| 582 |
startTimeFn: func(aggr metricdata.Aggregation) []time.Time { |
| 583 |
dps := aggr.(metricdata.ExponentialHistogram[float64]).DataPoints |
| 584 |
sts := make([]time.Time, len(dps)) |
| 585 |
for i, dp := range dps { |
| 586 |
sts[i] = dp.StartTime |
| 587 |
} |
| 588 |
return sts |
| 589 |
}, |
| 590 |
}, |
| 591 |
} |
| 592 |
for _, tt := range testCases { |
| 593 |
t.Run(tt.name, func(t *testing.T) { |
| 594 |
reg := prometheus.NewRegistry() |
| 595 |
tt.testFn(reg) |
| 596 |
p := NewMetricProducer(WithGatherer(reg)) |
| 597 |
output, err := p.Produce(t.Context()) |
| 598 |
assert.NoError(t, err) |
| 599 |
assert.NotEmpty(t, output) |
| 600 |
for _, sms := range output { |
| 601 |
assert.NotEmpty(t, sms.Metrics) |
| 602 |
for _, ms := range sms.Metrics { |
| 603 |
sts := tt.startTimeFn(ms.Data) |
| 604 |
assert.NotEmpty(t, sts) |
| 605 |
for _, st := range sts { |
| 606 |
assert.True(t, st.After(processStartTime)) |
| 607 |
} |
| 608 |
} |
| 609 |
} |
| 610 |
}) |
| 611 |
} |
| 612 |
} |