| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package pmetric |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
|
| 13 |
"go.opentelemetry.io/collector/internal/testutil" |
| 14 |
"go.opentelemetry.io/collector/pdata/internal" |
| 15 |
"go.opentelemetry.io/collector/pdata/pcommon" |
| 16 |
) |
| 17 |
|
| 18 |
const ( |
| 19 |
startTime = uint64(12578940000000012345) |
| 20 |
endTime = uint64(12578940000000054321) |
| 21 |
) |
| 22 |
|
| 23 |
func TestMetricCount(t *testing.T) { |
| 24 |
md := NewMetrics() |
| 25 |
assert.Equal(t, 0, md.MetricCount()) |
| 26 |
|
| 27 |
rms := md.ResourceMetrics() |
| 28 |
rms.EnsureCapacity(3) |
| 29 |
rm := rms.AppendEmpty() |
| 30 |
assert.Equal(t, 0, md.MetricCount()) |
| 31 |
|
| 32 |
ilm := rm.ScopeMetrics().AppendEmpty() |
| 33 |
assert.Equal(t, 0, md.MetricCount()) |
| 34 |
|
| 35 |
ilm.Metrics().AppendEmpty() |
| 36 |
assert.Equal(t, 1, md.MetricCount()) |
| 37 |
|
| 38 |
rms.AppendEmpty().ScopeMetrics().AppendEmpty() |
| 39 |
ilmm := rms.AppendEmpty().ScopeMetrics().AppendEmpty().Metrics() |
| 40 |
ilmm.EnsureCapacity(5) |
| 41 |
for range 5 { |
| 42 |
ilmm.AppendEmpty() |
| 43 |
} |
| 44 |
|
| 45 |
assert.Equal(t, 6, md.MetricCount()) |
| 46 |
} |
| 47 |
|
| 48 |
func TestMetricCountWithEmpty(t *testing.T) { |
| 49 |
assert.Equal(t, 0, generateMetricsEmptyResource().MetricCount()) |
| 50 |
assert.Equal(t, 0, generateMetricsEmptyInstrumentation().MetricCount()) |
| 51 |
assert.Equal(t, 1, generateMetricsEmptyMetrics().MetricCount()) |
| 52 |
} |
| 53 |
|
| 54 |
func TestMetricAndDataPointCount(t *testing.T) { |
| 55 |
md := NewMetrics() |
| 56 |
dps := md.DataPointCount() |
| 57 |
assert.Equal(t, 0, dps) |
| 58 |
|
| 59 |
rms := md.ResourceMetrics() |
| 60 |
rms.AppendEmpty() |
| 61 |
dps = md.DataPointCount() |
| 62 |
assert.Equal(t, 0, dps) |
| 63 |
|
| 64 |
ilms := md.ResourceMetrics().At(0).ScopeMetrics() |
| 65 |
ilms.AppendEmpty() |
| 66 |
dps = md.DataPointCount() |
| 67 |
assert.Equal(t, 0, dps) |
| 68 |
|
| 69 |
ilms.At(0).Metrics().AppendEmpty() |
| 70 |
dps = md.DataPointCount() |
| 71 |
assert.Equal(t, 0, dps) |
| 72 |
intSum := ilms.At(0).Metrics().At(0).SetEmptySum() |
| 73 |
intSum.DataPoints().AppendEmpty() |
| 74 |
intSum.DataPoints().AppendEmpty() |
| 75 |
intSum.DataPoints().AppendEmpty() |
| 76 |
assert.Equal(t, 3, md.DataPointCount()) |
| 77 |
|
| 78 |
md = NewMetrics() |
| 79 |
rms = md.ResourceMetrics() |
| 80 |
rms.EnsureCapacity(3) |
| 81 |
rms.AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty() |
| 82 |
rms.AppendEmpty().ScopeMetrics().AppendEmpty() |
| 83 |
rms.AppendEmpty().ScopeMetrics().AppendEmpty() |
| 84 |
ilms = rms.At(2).ScopeMetrics() |
| 85 |
ilm := ilms.At(0).Metrics() |
| 86 |
for range 5 { |
| 87 |
ilm.AppendEmpty() |
| 88 |
} |
| 89 |
assert.Equal(t, 0, md.DataPointCount()) |
| 90 |
|
| 91 |
ilm.At(0).SetEmptyGauge().DataPoints().AppendEmpty() |
| 92 |
assert.Equal(t, 1, md.DataPointCount()) |
| 93 |
|
| 94 |
ilm.At(1).SetEmptySum().DataPoints().AppendEmpty() |
| 95 |
assert.Equal(t, 2, md.DataPointCount()) |
| 96 |
|
| 97 |
ilm.At(2).SetEmptyHistogram().DataPoints().AppendEmpty() |
| 98 |
assert.Equal(t, 3, md.DataPointCount()) |
| 99 |
|
| 100 |
ilm.At(3).SetEmptyExponentialHistogram().DataPoints().AppendEmpty() |
| 101 |
assert.Equal(t, 4, md.DataPointCount()) |
| 102 |
|
| 103 |
ilm.At(4).SetEmptySummary().DataPoints().AppendEmpty() |
| 104 |
assert.Equal(t, 5, md.DataPointCount()) |
| 105 |
} |
| 106 |
|
| 107 |
func TestDataPointCountWithEmpty(t *testing.T) { |
| 108 |
assert.Equal(t, 0, generateMetricsEmptyResource().DataPointCount()) |
| 109 |
assert.Equal(t, 0, generateMetricsEmptyInstrumentation().DataPointCount()) |
| 110 |
assert.Equal(t, 0, generateMetricsEmptyMetrics().DataPointCount()) |
| 111 |
assert.Equal(t, 1, generateMetricsEmptyDataPoints().DataPointCount()) |
| 112 |
} |
| 113 |
|
| 114 |
func TestDataPointCountWithNilDataPoints(t *testing.T) { |
| 115 |
md := NewMetrics() |
| 116 |
ilm := md.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty() |
| 117 |
ilm.Metrics().AppendEmpty().SetEmptyGauge() |
| 118 |
ilm.Metrics().AppendEmpty().SetEmptySum() |
| 119 |
ilm.Metrics().AppendEmpty().SetEmptyHistogram() |
| 120 |
ilm.Metrics().AppendEmpty().SetEmptyExponentialHistogram() |
| 121 |
ilm.Metrics().AppendEmpty().SetEmptySummary() |
| 122 |
assert.Equal(t, 0, md.DataPointCount()) |
| 123 |
} |
| 124 |
|
| 125 |
func TestHistogramWithNilSum(t *testing.T) { |
| 126 |
md := NewMetrics() |
| 127 |
ilm := md.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty() |
| 128 |
histo := ilm.Metrics().AppendEmpty() |
| 129 |
histogramDataPoints := histo.SetEmptyHistogram().DataPoints() |
| 130 |
histogramDataPoints.AppendEmpty() |
| 131 |
dest := ilm.Metrics().AppendEmpty() |
| 132 |
histo.CopyTo(dest) |
| 133 |
assert.Equal(t, histo, dest) |
| 134 |
} |
| 135 |
|
| 136 |
func TestHistogramWithValidSum(t *testing.T) { |
| 137 |
md := NewMetrics() |
| 138 |
ilm := md.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty() |
| 139 |
histo := ilm.Metrics().AppendEmpty() |
| 140 |
histogramDataPoints := histo.SetEmptyHistogram().DataPoints() |
| 141 |
histogramDataPoints.AppendEmpty() |
| 142 |
histogramDataPoints.At(0).SetSum(10) |
| 143 |
dest := ilm.Metrics().AppendEmpty() |
| 144 |
histo.CopyTo(dest) |
| 145 |
assert.Equal(t, histo, dest) |
| 146 |
} |
| 147 |
|
| 148 |
func TestOtlpToInternalReadOnly(t *testing.T) { |
| 149 |
md := newMetrics(&internal.ExportMetricsServiceRequest{ |
| 150 |
ResourceMetrics: []*internal.ResourceMetrics{ |
| 151 |
{ |
| 152 |
Resource: generateTestProtoResource(), |
| 153 |
ScopeMetrics: []*internal.ScopeMetrics{ |
| 154 |
{ |
| 155 |
Scope: generateTestProtoInstrumentationScope(), |
| 156 |
Metrics: []*internal.Metric{generateTestProtoGaugeMetric(), generateTestProtoSumMetric(), generateTestProtoHistogramMetric()}, |
| 157 |
}, |
| 158 |
}, |
| 159 |
}, |
| 160 |
}, |
| 161 |
}, new(internal.State)) |
| 162 |
resourceMetrics := md.ResourceMetrics() |
| 163 |
assert.Equal(t, 1, resourceMetrics.Len()) |
| 164 |
|
| 165 |
resourceMetric := resourceMetrics.At(0) |
| 166 |
assert.Equal(t, map[string]any{ |
| 167 |
"string": "string-resource", |
| 168 |
}, resourceMetric.Resource().Attributes().AsRaw()) |
| 169 |
metrics := resourceMetric.ScopeMetrics().At(0).Metrics() |
| 170 |
assert.Equal(t, 3, metrics.Len()) |
| 171 |
|
| 172 |
|
| 173 |
metricInt := metrics.At(0) |
| 174 |
assert.Equal(t, "my_metric_int", metricInt.Name()) |
| 175 |
assert.Equal(t, "My metric", metricInt.Description()) |
| 176 |
assert.Equal(t, "ms", metricInt.Unit()) |
| 177 |
assert.Equal(t, MetricTypeGauge, metricInt.Type()) |
| 178 |
gaugeDataPoints := metricInt.Gauge().DataPoints() |
| 179 |
assert.Equal(t, 2, gaugeDataPoints.Len()) |
| 180 |
|
| 181 |
assert.EqualValues(t, startTime, gaugeDataPoints.At(0).StartTimestamp()) |
| 182 |
assert.EqualValues(t, endTime, gaugeDataPoints.At(0).Timestamp()) |
| 183 |
assert.InDelta(t, 123.1, gaugeDataPoints.At(0).DoubleValue(), 0.01) |
| 184 |
assert.Equal(t, map[string]any{"key0": "value0"}, gaugeDataPoints.At(0).Attributes().AsRaw()) |
| 185 |
|
| 186 |
assert.EqualValues(t, startTime, gaugeDataPoints.At(1).StartTimestamp()) |
| 187 |
assert.EqualValues(t, endTime, gaugeDataPoints.At(1).Timestamp()) |
| 188 |
assert.InDelta(t, 456.1, gaugeDataPoints.At(1).DoubleValue(), 0.01) |
| 189 |
assert.Equal(t, map[string]any{"key1": "value1"}, gaugeDataPoints.At(1).Attributes().AsRaw()) |
| 190 |
|
| 191 |
|
| 192 |
metricDouble := metrics.At(1) |
| 193 |
assert.Equal(t, "my_metric_double", metricDouble.Name()) |
| 194 |
assert.Equal(t, "My metric", metricDouble.Description()) |
| 195 |
assert.Equal(t, "ms", metricDouble.Unit()) |
| 196 |
assert.Equal(t, MetricTypeSum, metricDouble.Type()) |
| 197 |
dsd := metricDouble.Sum() |
| 198 |
assert.Equal(t, AggregationTemporalityCumulative, dsd.AggregationTemporality()) |
| 199 |
sumDataPoints := dsd.DataPoints() |
| 200 |
assert.Equal(t, 2, sumDataPoints.Len()) |
| 201 |
|
| 202 |
assert.EqualValues(t, startTime, sumDataPoints.At(0).StartTimestamp()) |
| 203 |
assert.EqualValues(t, endTime, sumDataPoints.At(0).Timestamp()) |
| 204 |
assert.InDelta(t, 123.1, sumDataPoints.At(0).DoubleValue(), 0.01) |
| 205 |
assert.Equal(t, map[string]any{"key0": "value0"}, sumDataPoints.At(0).Attributes().AsRaw()) |
| 206 |
|
| 207 |
assert.EqualValues(t, startTime, sumDataPoints.At(1).StartTimestamp()) |
| 208 |
assert.EqualValues(t, endTime, sumDataPoints.At(1).Timestamp()) |
| 209 |
assert.InDelta(t, 456.1, sumDataPoints.At(1).DoubleValue(), 0.01) |
| 210 |
assert.Equal(t, map[string]any{"key1": "value1"}, sumDataPoints.At(1).Attributes().AsRaw()) |
| 211 |
|
| 212 |
|
| 213 |
metricHistogram := metrics.At(2) |
| 214 |
assert.Equal(t, "my_metric_histogram", metricHistogram.Name()) |
| 215 |
assert.Equal(t, "My metric", metricHistogram.Description()) |
| 216 |
assert.Equal(t, "ms", metricHistogram.Unit()) |
| 217 |
assert.Equal(t, MetricTypeHistogram, metricHistogram.Type()) |
| 218 |
dhd := metricHistogram.Histogram() |
| 219 |
assert.Equal(t, AggregationTemporalityDelta, dhd.AggregationTemporality()) |
| 220 |
histogramDataPoints := dhd.DataPoints() |
| 221 |
assert.Equal(t, 2, histogramDataPoints.Len()) |
| 222 |
|
| 223 |
assert.EqualValues(t, startTime, histogramDataPoints.At(0).StartTimestamp()) |
| 224 |
assert.EqualValues(t, endTime, histogramDataPoints.At(0).Timestamp()) |
| 225 |
assert.Equal(t, []float64{1, 2}, histogramDataPoints.At(0).ExplicitBounds().AsRaw()) |
| 226 |
assert.Equal(t, map[string]any{"key0": "value0"}, histogramDataPoints.At(0).Attributes().AsRaw()) |
| 227 |
assert.Equal(t, []uint64{10, 15, 1}, histogramDataPoints.At(0).BucketCounts().AsRaw()) |
| 228 |
|
| 229 |
assert.EqualValues(t, startTime, histogramDataPoints.At(1).StartTimestamp()) |
| 230 |
assert.EqualValues(t, endTime, histogramDataPoints.At(1).Timestamp()) |
| 231 |
assert.Equal(t, []float64{1}, histogramDataPoints.At(1).ExplicitBounds().AsRaw()) |
| 232 |
assert.Equal(t, map[string]any{"key1": "value1"}, histogramDataPoints.At(1).Attributes().AsRaw()) |
| 233 |
assert.Equal(t, []uint64{10, 1}, histogramDataPoints.At(1).BucketCounts().AsRaw()) |
| 234 |
} |
| 235 |
|
| 236 |
func TestOtlpToFromInternalReadOnly(t *testing.T) { |
| 237 |
md := newMetrics(&internal.ExportMetricsServiceRequest{ |
| 238 |
ResourceMetrics: []*internal.ResourceMetrics{ |
| 239 |
{ |
| 240 |
Resource: generateTestProtoResource(), |
| 241 |
ScopeMetrics: []*internal.ScopeMetrics{ |
| 242 |
{ |
| 243 |
Scope: generateTestProtoInstrumentationScope(), |
| 244 |
Metrics: []*internal.Metric{generateTestProtoGaugeMetric(), generateTestProtoSumMetric(), generateTestProtoHistogramMetric()}, |
| 245 |
}, |
| 246 |
}, |
| 247 |
}, |
| 248 |
}, |
| 249 |
}, new(internal.State)) |
| 250 |
|
| 251 |
assert.EqualValues(t, &internal.MetricsData{ |
| 252 |
ResourceMetrics: []*internal.ResourceMetrics{ |
| 253 |
{ |
| 254 |
Resource: generateTestProtoResource(), |
| 255 |
ScopeMetrics: []*internal.ScopeMetrics{ |
| 256 |
{ |
| 257 |
Scope: generateTestProtoInstrumentationScope(), |
| 258 |
Metrics: []*internal.Metric{generateTestProtoGaugeMetric(), generateTestProtoSumMetric(), generateTestProtoHistogramMetric()}, |
| 259 |
}, |
| 260 |
}, |
| 261 |
}, |
| 262 |
}, |
| 263 |
}, md.getOrig()) |
| 264 |
} |
| 265 |
|
| 266 |
func TestOtlpToFromInternalGaugeMutating(t *testing.T) { |
| 267 |
newAttributes := map[string]any{"k": "v"} |
| 268 |
|
| 269 |
md := newMetrics(&internal.ExportMetricsServiceRequest{ |
| 270 |
ResourceMetrics: []*internal.ResourceMetrics{ |
| 271 |
{ |
| 272 |
Resource: generateTestProtoResource(), |
| 273 |
ScopeMetrics: []*internal.ScopeMetrics{ |
| 274 |
{ |
| 275 |
Scope: generateTestProtoInstrumentationScope(), |
| 276 |
Metrics: []*internal.Metric{generateTestProtoGaugeMetric()}, |
| 277 |
}, |
| 278 |
}, |
| 279 |
}, |
| 280 |
}, |
| 281 |
}, new(internal.State)) |
| 282 |
resourceMetrics := md.ResourceMetrics() |
| 283 |
metric := resourceMetrics.At(0).ScopeMetrics().At(0).Metrics().At(0) |
| 284 |
|
| 285 |
metric.SetName("new_my_metric_int") |
| 286 |
assert.Equal(t, "new_my_metric_int", metric.Name()) |
| 287 |
metric.SetDescription("My new metric") |
| 288 |
assert.Equal(t, "My new metric", metric.Description()) |
| 289 |
metric.SetUnit("1") |
| 290 |
assert.Equal(t, "1", metric.Unit()) |
| 291 |
|
| 292 |
igd := metric.Gauge() |
| 293 |
assert.Equal(t, 2, igd.DataPoints().Len()) |
| 294 |
gaugeDataPoints := metric.SetEmptyGauge().DataPoints() |
| 295 |
gaugeDataPoints.AppendEmpty() |
| 296 |
assert.Equal(t, 1, gaugeDataPoints.Len()) |
| 297 |
gaugeDataPoints.At(0).SetStartTimestamp(pcommon.Timestamp(startTime + 1)) |
| 298 |
assert.EqualValues(t, startTime+1, gaugeDataPoints.At(0).StartTimestamp()) |
| 299 |
gaugeDataPoints.At(0).SetTimestamp(pcommon.Timestamp(endTime + 1)) |
| 300 |
assert.EqualValues(t, endTime+1, gaugeDataPoints.At(0).Timestamp()) |
| 301 |
gaugeDataPoints.At(0).SetDoubleValue(124.1) |
| 302 |
assert.InDelta(t, 124.1, gaugeDataPoints.At(0).DoubleValue(), 0.01) |
| 303 |
gaugeDataPoints.At(0).Attributes().Remove("key0") |
| 304 |
gaugeDataPoints.At(0).Attributes().PutStr("k", "v") |
| 305 |
assert.Equal(t, newAttributes, gaugeDataPoints.At(0).Attributes().AsRaw()) |
| 306 |
|
| 307 |
|
| 308 |
assert.EqualValues(t, &internal.MetricsData{ |
| 309 |
ResourceMetrics: []*internal.ResourceMetrics{ |
| 310 |
{ |
| 311 |
Resource: generateTestProtoResource(), |
| 312 |
ScopeMetrics: []*internal.ScopeMetrics{ |
| 313 |
{ |
| 314 |
Scope: generateTestProtoInstrumentationScope(), |
| 315 |
Metrics: []*internal.Metric{ |
| 316 |
{ |
| 317 |
Name: "new_my_metric_int", |
| 318 |
Description: "My new metric", |
| 319 |
Unit: "1", |
| 320 |
Data: &internal.Metric_Gauge{ |
| 321 |
Gauge: &internal.Gauge{ |
| 322 |
DataPoints: []*internal.NumberDataPoint{ |
| 323 |
{ |
| 324 |
Attributes: []internal.KeyValue{ |
| 325 |
{ |
| 326 |
Key: "k", |
| 327 |
Value: internal.AnyValue{Value: &internal.AnyValue_StringValue{StringValue: "v"}}, |
| 328 |
}, |
| 329 |
}, |
| 330 |
StartTimeUnixNano: startTime + 1, |
| 331 |
TimeUnixNano: endTime + 1, |
| 332 |
Value: &internal.NumberDataPoint_AsDouble{ |
| 333 |
AsDouble: 124.1, |
| 334 |
}, |
| 335 |
}, |
| 336 |
}, |
| 337 |
}, |
| 338 |
}, |
| 339 |
}, |
| 340 |
}, |
| 341 |
}, |
| 342 |
}, |
| 343 |
}, |
| 344 |
}, |
| 345 |
}, md.getOrig()) |
| 346 |
} |
| 347 |
|
| 348 |
func TestOtlpToFromInternalSumMutating(t *testing.T) { |
| 349 |
newAttributes := map[string]any{"k": "v"} |
| 350 |
|
| 351 |
md := newMetrics(&internal.ExportMetricsServiceRequest{ |
| 352 |
ResourceMetrics: []*internal.ResourceMetrics{ |
| 353 |
{ |
| 354 |
Resource: generateTestProtoResource(), |
| 355 |
ScopeMetrics: []*internal.ScopeMetrics{ |
| 356 |
{ |
| 357 |
Scope: generateTestProtoInstrumentationScope(), |
| 358 |
Metrics: []*internal.Metric{generateTestProtoSumMetric()}, |
| 359 |
}, |
| 360 |
}, |
| 361 |
}, |
| 362 |
}, |
| 363 |
}, new(internal.State)) |
| 364 |
resourceMetrics := md.ResourceMetrics() |
| 365 |
metric := resourceMetrics.At(0).ScopeMetrics().At(0).Metrics().At(0) |
| 366 |
|
| 367 |
metric.SetName("new_my_metric_double") |
| 368 |
assert.Equal(t, "new_my_metric_double", metric.Name()) |
| 369 |
metric.SetDescription("My new metric") |
| 370 |
assert.Equal(t, "My new metric", metric.Description()) |
| 371 |
metric.SetUnit("1") |
| 372 |
assert.Equal(t, "1", metric.Unit()) |
| 373 |
|
| 374 |
dsd := metric.Sum() |
| 375 |
assert.Equal(t, 2, dsd.DataPoints().Len()) |
| 376 |
metric.SetEmptySum().SetAggregationTemporality(AggregationTemporalityCumulative) |
| 377 |
doubleDataPoints := metric.Sum().DataPoints() |
| 378 |
doubleDataPoints.AppendEmpty() |
| 379 |
assert.Equal(t, 1, doubleDataPoints.Len()) |
| 380 |
doubleDataPoints.At(0).SetStartTimestamp(pcommon.Timestamp(startTime + 1)) |
| 381 |
assert.EqualValues(t, startTime+1, doubleDataPoints.At(0).StartTimestamp()) |
| 382 |
doubleDataPoints.At(0).SetTimestamp(pcommon.Timestamp(endTime + 1)) |
| 383 |
assert.EqualValues(t, endTime+1, doubleDataPoints.At(0).Timestamp()) |
| 384 |
doubleDataPoints.At(0).SetDoubleValue(124.1) |
| 385 |
assert.InDelta(t, 124.1, doubleDataPoints.At(0).DoubleValue(), 0.01) |
| 386 |
doubleDataPoints.At(0).Attributes().Remove("key0") |
| 387 |
doubleDataPoints.At(0).Attributes().PutStr("k", "v") |
| 388 |
assert.Equal(t, newAttributes, doubleDataPoints.At(0).Attributes().AsRaw()) |
| 389 |
|
| 390 |
|
| 391 |
assert.EqualValues(t, &internal.MetricsData{ |
| 392 |
ResourceMetrics: []*internal.ResourceMetrics{ |
| 393 |
{ |
| 394 |
Resource: generateTestProtoResource(), |
| 395 |
ScopeMetrics: []*internal.ScopeMetrics{ |
| 396 |
{ |
| 397 |
Scope: generateTestProtoInstrumentationScope(), |
| 398 |
Metrics: []*internal.Metric{ |
| 399 |
{ |
| 400 |
Name: "new_my_metric_double", |
| 401 |
Description: "My new metric", |
| 402 |
Unit: "1", |
| 403 |
Data: &internal.Metric_Sum{ |
| 404 |
Sum: &internal.Sum{ |
| 405 |
AggregationTemporality: internal.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE, |
| 406 |
DataPoints: []*internal.NumberDataPoint{ |
| 407 |
{ |
| 408 |
Attributes: []internal.KeyValue{ |
| 409 |
{ |
| 410 |
Key: "k", |
| 411 |
Value: internal.AnyValue{Value: &internal.AnyValue_StringValue{StringValue: "v"}}, |
| 412 |
}, |
| 413 |
}, |
| 414 |
StartTimeUnixNano: startTime + 1, |
| 415 |
TimeUnixNano: endTime + 1, |
| 416 |
Value: &internal.NumberDataPoint_AsDouble{ |
| 417 |
AsDouble: 124.1, |
| 418 |
}, |
| 419 |
}, |
| 420 |
}, |
| 421 |
}, |
| 422 |
}, |
| 423 |
}, |
| 424 |
}, |
| 425 |
}, |
| 426 |
}, |
| 427 |
}, |
| 428 |
}, |
| 429 |
}, md.getOrig()) |
| 430 |
} |
| 431 |
|
| 432 |
func TestOtlpToFromInternalHistogramMutating(t *testing.T) { |
| 433 |
newAttributes := map[string]any{"k": "v"} |
| 434 |
|
| 435 |
md := newMetrics(&internal.ExportMetricsServiceRequest{ |
| 436 |
ResourceMetrics: []*internal.ResourceMetrics{ |
| 437 |
{ |
| 438 |
Resource: generateTestProtoResource(), |
| 439 |
ScopeMetrics: []*internal.ScopeMetrics{ |
| 440 |
{ |
| 441 |
Scope: generateTestProtoInstrumentationScope(), |
| 442 |
Metrics: []*internal.Metric{generateTestProtoHistogramMetric()}, |
| 443 |
}, |
| 444 |
}, |
| 445 |
}, |
| 446 |
}, |
| 447 |
}, new(internal.State)) |
| 448 |
resourceMetrics := md.ResourceMetrics() |
| 449 |
metric := resourceMetrics.At(0).ScopeMetrics().At(0).Metrics().At(0) |
| 450 |
|
| 451 |
metric.SetName("new_my_metric_histogram") |
| 452 |
assert.Equal(t, "new_my_metric_histogram", metric.Name()) |
| 453 |
metric.SetDescription("My new metric") |
| 454 |
assert.Equal(t, "My new metric", metric.Description()) |
| 455 |
metric.SetUnit("1") |
| 456 |
assert.Equal(t, "1", metric.Unit()) |
| 457 |
|
| 458 |
dhd := metric.Histogram() |
| 459 |
assert.Equal(t, 2, dhd.DataPoints().Len()) |
| 460 |
metric.SetEmptyHistogram().SetAggregationTemporality(AggregationTemporalityDelta) |
| 461 |
histogramDataPoints := metric.Histogram().DataPoints() |
| 462 |
histogramDataPoints.AppendEmpty() |
| 463 |
assert.Equal(t, 1, histogramDataPoints.Len()) |
| 464 |
histogramDataPoints.At(0).SetStartTimestamp(pcommon.Timestamp(startTime + 1)) |
| 465 |
assert.EqualValues(t, startTime+1, histogramDataPoints.At(0).StartTimestamp()) |
| 466 |
histogramDataPoints.At(0).SetTimestamp(pcommon.Timestamp(endTime + 1)) |
| 467 |
assert.EqualValues(t, endTime+1, histogramDataPoints.At(0).Timestamp()) |
| 468 |
histogramDataPoints.At(0).Attributes().Remove("key0") |
| 469 |
histogramDataPoints.At(0).Attributes().PutStr("k", "v") |
| 470 |
assert.Equal(t, newAttributes, histogramDataPoints.At(0).Attributes().AsRaw()) |
| 471 |
histogramDataPoints.At(0).ExplicitBounds().FromRaw([]float64{1}) |
| 472 |
assert.Equal(t, []float64{1}, histogramDataPoints.At(0).ExplicitBounds().AsRaw()) |
| 473 |
histogramDataPoints.At(0).BucketCounts().FromRaw([]uint64{21, 32}) |
| 474 |
|
| 475 |
assert.EqualValues(t, &internal.MetricsData{ |
| 476 |
ResourceMetrics: []*internal.ResourceMetrics{ |
| 477 |
{ |
| 478 |
Resource: generateTestProtoResource(), |
| 479 |
ScopeMetrics: []*internal.ScopeMetrics{ |
| 480 |
{ |
| 481 |
Scope: generateTestProtoInstrumentationScope(), |
| 482 |
Metrics: []*internal.Metric{ |
| 483 |
{ |
| 484 |
Name: "new_my_metric_histogram", |
| 485 |
Description: "My new metric", |
| 486 |
Unit: "1", |
| 487 |
Data: &internal.Metric_Histogram{ |
| 488 |
Histogram: &internal.Histogram{ |
| 489 |
AggregationTemporality: internal.AggregationTemporality_AGGREGATION_TEMPORALITY_DELTA, |
| 490 |
DataPoints: []*internal.HistogramDataPoint{ |
| 491 |
{ |
| 492 |
Attributes: []internal.KeyValue{ |
| 493 |
{ |
| 494 |
Key: "k", |
| 495 |
Value: internal.AnyValue{Value: &internal.AnyValue_StringValue{StringValue: "v"}}, |
| 496 |
}, |
| 497 |
}, |
| 498 |
StartTimeUnixNano: startTime + 1, |
| 499 |
TimeUnixNano: endTime + 1, |
| 500 |
BucketCounts: []uint64{21, 32}, |
| 501 |
ExplicitBounds: []float64{1}, |
| 502 |
}, |
| 503 |
}, |
| 504 |
}, |
| 505 |
}, |
| 506 |
}, |
| 507 |
}, |
| 508 |
}, |
| 509 |
}, |
| 510 |
}, |
| 511 |
}, |
| 512 |
}, md.getOrig()) |
| 513 |
} |
| 514 |
|
| 515 |
func TestOtlpToFromInternalExponentialHistogramMutating(t *testing.T) { |
| 516 |
newAttributes := map[string]any{"k": "v"} |
| 517 |
|
| 518 |
md := newMetrics(&internal.ExportMetricsServiceRequest{ |
| 519 |
ResourceMetrics: []*internal.ResourceMetrics{ |
| 520 |
{ |
| 521 |
Resource: generateTestProtoResource(), |
| 522 |
ScopeMetrics: []*internal.ScopeMetrics{ |
| 523 |
{ |
| 524 |
Scope: generateTestProtoInstrumentationScope(), |
| 525 |
Metrics: []*internal.Metric{generateTestProtoHistogramMetric()}, |
| 526 |
}, |
| 527 |
}, |
| 528 |
}, |
| 529 |
}, |
| 530 |
}, new(internal.State)) |
| 531 |
resourceMetrics := md.ResourceMetrics() |
| 532 |
metric := resourceMetrics.At(0).ScopeMetrics().At(0).Metrics().At(0) |
| 533 |
|
| 534 |
metric.SetName("new_my_metric_exponential_histogram") |
| 535 |
assert.Equal(t, "new_my_metric_exponential_histogram", metric.Name()) |
| 536 |
metric.SetDescription("My new metric") |
| 537 |
assert.Equal(t, "My new metric", metric.Description()) |
| 538 |
metric.SetUnit("1") |
| 539 |
assert.Equal(t, "1", metric.Unit()) |
| 540 |
|
| 541 |
dhd := metric.Histogram() |
| 542 |
assert.Equal(t, 2, dhd.DataPoints().Len()) |
| 543 |
metric.SetEmptyExponentialHistogram().SetAggregationTemporality(AggregationTemporalityDelta) |
| 544 |
histogramDataPoints := metric.ExponentialHistogram().DataPoints() |
| 545 |
histogramDataPoints.AppendEmpty() |
| 546 |
assert.Equal(t, 1, histogramDataPoints.Len()) |
| 547 |
histogramDataPoints.At(0).SetStartTimestamp(pcommon.Timestamp(startTime + 1)) |
| 548 |
assert.EqualValues(t, startTime+1, histogramDataPoints.At(0).StartTimestamp()) |
| 549 |
histogramDataPoints.At(0).SetTimestamp(pcommon.Timestamp(endTime + 1)) |
| 550 |
assert.EqualValues(t, endTime+1, histogramDataPoints.At(0).Timestamp()) |
| 551 |
histogramDataPoints.At(0).Attributes().Remove("key0") |
| 552 |
histogramDataPoints.At(0).Attributes().PutStr("k", "v") |
| 553 |
assert.Equal(t, newAttributes, histogramDataPoints.At(0).Attributes().AsRaw()) |
| 554 |
|
| 555 |
assert.EqualValues(t, &internal.MetricsData{ |
| 556 |
ResourceMetrics: []*internal.ResourceMetrics{ |
| 557 |
{ |
| 558 |
Resource: generateTestProtoResource(), |
| 559 |
ScopeMetrics: []*internal.ScopeMetrics{ |
| 560 |
{ |
| 561 |
Scope: generateTestProtoInstrumentationScope(), |
| 562 |
Metrics: []*internal.Metric{ |
| 563 |
{ |
| 564 |
Name: "new_my_metric_exponential_histogram", |
| 565 |
Description: "My new metric", |
| 566 |
Unit: "1", |
| 567 |
Data: &internal.Metric_ExponentialHistogram{ |
| 568 |
ExponentialHistogram: &internal.ExponentialHistogram{ |
| 569 |
AggregationTemporality: internal.AggregationTemporality_AGGREGATION_TEMPORALITY_DELTA, |
| 570 |
DataPoints: []*internal.ExponentialHistogramDataPoint{ |
| 571 |
{ |
| 572 |
Attributes: []internal.KeyValue{ |
| 573 |
{ |
| 574 |
Key: "k", |
| 575 |
Value: internal.AnyValue{Value: &internal.AnyValue_StringValue{StringValue: "v"}}, |
| 576 |
}, |
| 577 |
}, |
| 578 |
StartTimeUnixNano: startTime + 1, |
| 579 |
TimeUnixNano: endTime + 1, |
| 580 |
}, |
| 581 |
}, |
| 582 |
}, |
| 583 |
}, |
| 584 |
}, |
| 585 |
}, |
| 586 |
}, |
| 587 |
}, |
| 588 |
}, |
| 589 |
}, |
| 590 |
}, md.getOrig()) |
| 591 |
} |
| 592 |
|
| 593 |
func TestMetricsCopyTo(t *testing.T) { |
| 594 |
md := generateTestMetrics() |
| 595 |
metricsCopy := NewMetrics() |
| 596 |
md.CopyTo(metricsCopy) |
| 597 |
assert.Equal(t, md, metricsCopy) |
| 598 |
} |
| 599 |
|
| 600 |
func TestReadOnlyMetricsInvalidUsage(t *testing.T) { |
| 601 |
metrics := NewMetrics() |
| 602 |
assert.False(t, metrics.IsReadOnly()) |
| 603 |
res := metrics.ResourceMetrics().AppendEmpty().Resource() |
| 604 |
res.Attributes().PutStr("k1", "v1") |
| 605 |
metrics.MarkReadOnly() |
| 606 |
assert.True(t, metrics.IsReadOnly()) |
| 607 |
assert.Panics(t, func() { res.Attributes().PutStr("k2", "v2") }) |
| 608 |
} |
| 609 |
|
| 610 |
func BenchmarkOtlpToFromInternal_PassThrough(b *testing.B) { |
| 611 |
testutil.SkipMemoryBench(b) |
| 612 |
req := &internal.ExportMetricsServiceRequest{ |
| 613 |
ResourceMetrics: []*internal.ResourceMetrics{ |
| 614 |
{ |
| 615 |
Resource: generateTestProtoResource(), |
| 616 |
ScopeMetrics: []*internal.ScopeMetrics{ |
| 617 |
{ |
| 618 |
Scope: generateTestProtoInstrumentationScope(), |
| 619 |
Metrics: []*internal.Metric{generateTestProtoGaugeMetric(), generateTestProtoSumMetric(), generateTestProtoHistogramMetric()}, |
| 620 |
}, |
| 621 |
}, |
| 622 |
}, |
| 623 |
}, |
| 624 |
} |
| 625 |
var state internal.State |
| 626 |
|
| 627 |
for b.Loop() { |
| 628 |
md := newMetrics(req, &state) |
| 629 |
newReq := md.getOrig() |
| 630 |
if len(req.ResourceMetrics) != len(newReq.ResourceMetrics) { |
| 631 |
b.Fail() |
| 632 |
} |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
func BenchmarkOtlpToFromInternal_Gauge_MutateOneLabel(b *testing.B) { |
| 637 |
testutil.SkipMemoryBench(b) |
| 638 |
req := &internal.ExportMetricsServiceRequest{ |
| 639 |
ResourceMetrics: []*internal.ResourceMetrics{ |
| 640 |
{ |
| 641 |
Resource: generateTestProtoResource(), |
| 642 |
ScopeMetrics: []*internal.ScopeMetrics{ |
| 643 |
{ |
| 644 |
Scope: generateTestProtoInstrumentationScope(), |
| 645 |
Metrics: []*internal.Metric{generateTestProtoGaugeMetric()}, |
| 646 |
}, |
| 647 |
}, |
| 648 |
}, |
| 649 |
}, |
| 650 |
} |
| 651 |
var state internal.State |
| 652 |
|
| 653 |
for b.Loop() { |
| 654 |
md := newMetrics(req, &state) |
| 655 |
md.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Gauge().DataPoints().At(0).Attributes(). |
| 656 |
PutStr("key0", "value2") |
| 657 |
newReq := md.getOrig() |
| 658 |
if len(req.ResourceMetrics) != len(newReq.ResourceMetrics) { |
| 659 |
b.Fail() |
| 660 |
} |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
func BenchmarkOtlpToFromInternal_Sum_MutateOneLabel(b *testing.B) { |
| 665 |
testutil.SkipMemoryBench(b) |
| 666 |
req := &internal.ExportMetricsServiceRequest{ |
| 667 |
ResourceMetrics: []*internal.ResourceMetrics{ |
| 668 |
{ |
| 669 |
Resource: generateTestProtoResource(), |
| 670 |
ScopeMetrics: []*internal.ScopeMetrics{ |
| 671 |
{ |
| 672 |
Scope: generateTestProtoInstrumentationScope(), |
| 673 |
Metrics: []*internal.Metric{generateTestProtoSumMetric()}, |
| 674 |
}, |
| 675 |
}, |
| 676 |
}, |
| 677 |
}, |
| 678 |
} |
| 679 |
var state internal.State |
| 680 |
|
| 681 |
for b.Loop() { |
| 682 |
md := newMetrics(req, &state) |
| 683 |
md.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Sum().DataPoints().At(0).Attributes(). |
| 684 |
PutStr("key0", "value2") |
| 685 |
newReq := md.getOrig() |
| 686 |
if len(req.ResourceMetrics) != len(newReq.ResourceMetrics) { |
| 687 |
b.Fail() |
| 688 |
} |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
func BenchmarkOtlpToFromInternal_HistogramPoints_MutateOneLabel(b *testing.B) { |
| 693 |
testutil.SkipMemoryBench(b) |
| 694 |
req := &internal.ExportMetricsServiceRequest{ |
| 695 |
ResourceMetrics: []*internal.ResourceMetrics{ |
| 696 |
{ |
| 697 |
Resource: generateTestProtoResource(), |
| 698 |
ScopeMetrics: []*internal.ScopeMetrics{ |
| 699 |
{ |
| 700 |
Scope: generateTestProtoInstrumentationScope(), |
| 701 |
Metrics: []*internal.Metric{generateTestProtoHistogramMetric()}, |
| 702 |
}, |
| 703 |
}, |
| 704 |
}, |
| 705 |
}, |
| 706 |
} |
| 707 |
var state internal.State |
| 708 |
|
| 709 |
for b.Loop() { |
| 710 |
md := newMetrics(req, &state) |
| 711 |
md.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Histogram().DataPoints().At(0).Attributes(). |
| 712 |
PutStr("key0", "value2") |
| 713 |
newReq := md.getOrig() |
| 714 |
if len(req.ResourceMetrics) != len(newReq.ResourceMetrics) { |
| 715 |
b.Fail() |
| 716 |
} |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
func generateTestProtoResource() internal.Resource { |
| 721 |
return internal.Resource{ |
| 722 |
Attributes: []internal.KeyValue{ |
| 723 |
{ |
| 724 |
Key: "string", |
| 725 |
Value: internal.AnyValue{Value: &internal.AnyValue_StringValue{StringValue: "string-resource"}}, |
| 726 |
}, |
| 727 |
}, |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
func generateTestProtoInstrumentationScope() internal.InstrumentationScope { |
| 732 |
return internal.InstrumentationScope{ |
| 733 |
Name: "test", |
| 734 |
Version: "", |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
func generateTestProtoGaugeMetric() *internal.Metric { |
| 739 |
return &internal.Metric{ |
| 740 |
Name: "my_metric_int", |
| 741 |
Description: "My metric", |
| 742 |
Unit: "ms", |
| 743 |
Data: &internal.Metric_Gauge{ |
| 744 |
Gauge: &internal.Gauge{ |
| 745 |
DataPoints: []*internal.NumberDataPoint{ |
| 746 |
{ |
| 747 |
Attributes: []internal.KeyValue{ |
| 748 |
{ |
| 749 |
Key: "key0", |
| 750 |
Value: internal.AnyValue{Value: &internal.AnyValue_StringValue{StringValue: "value0"}}, |
| 751 |
}, |
| 752 |
}, |
| 753 |
StartTimeUnixNano: startTime, |
| 754 |
TimeUnixNano: endTime, |
| 755 |
Value: &internal.NumberDataPoint_AsDouble{ |
| 756 |
AsDouble: 123.1, |
| 757 |
}, |
| 758 |
}, |
| 759 |
{ |
| 760 |
Attributes: []internal.KeyValue{ |
| 761 |
{ |
| 762 |
Key: "key1", |
| 763 |
Value: internal.AnyValue{Value: &internal.AnyValue_StringValue{StringValue: "value1"}}, |
| 764 |
}, |
| 765 |
}, |
| 766 |
StartTimeUnixNano: startTime, |
| 767 |
TimeUnixNano: endTime, |
| 768 |
Value: &internal.NumberDataPoint_AsDouble{ |
| 769 |
AsDouble: 456.1, |
| 770 |
}, |
| 771 |
}, |
| 772 |
}, |
| 773 |
}, |
| 774 |
}, |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
func generateTestProtoSumMetric() *internal.Metric { |
| 779 |
return &internal.Metric{ |
| 780 |
Name: "my_metric_double", |
| 781 |
Description: "My metric", |
| 782 |
Unit: "ms", |
| 783 |
Data: &internal.Metric_Sum{ |
| 784 |
Sum: &internal.Sum{ |
| 785 |
AggregationTemporality: internal.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE, |
| 786 |
DataPoints: []*internal.NumberDataPoint{ |
| 787 |
{ |
| 788 |
Attributes: []internal.KeyValue{ |
| 789 |
{ |
| 790 |
Key: "key0", |
| 791 |
Value: internal.AnyValue{Value: &internal.AnyValue_StringValue{StringValue: "value0"}}, |
| 792 |
}, |
| 793 |
}, |
| 794 |
StartTimeUnixNano: startTime, |
| 795 |
TimeUnixNano: endTime, |
| 796 |
Value: &internal.NumberDataPoint_AsDouble{ |
| 797 |
AsDouble: 123.1, |
| 798 |
}, |
| 799 |
}, |
| 800 |
{ |
| 801 |
Attributes: []internal.KeyValue{ |
| 802 |
{ |
| 803 |
Key: "key1", |
| 804 |
Value: internal.AnyValue{Value: &internal.AnyValue_StringValue{StringValue: "value1"}}, |
| 805 |
}, |
| 806 |
}, |
| 807 |
StartTimeUnixNano: startTime, |
| 808 |
TimeUnixNano: endTime, |
| 809 |
Value: &internal.NumberDataPoint_AsDouble{ |
| 810 |
AsDouble: 456.1, |
| 811 |
}, |
| 812 |
}, |
| 813 |
}, |
| 814 |
}, |
| 815 |
}, |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
func generateTestProtoHistogramMetric() *internal.Metric { |
| 820 |
return &internal.Metric{ |
| 821 |
Name: "my_metric_histogram", |
| 822 |
Description: "My metric", |
| 823 |
Unit: "ms", |
| 824 |
Data: &internal.Metric_Histogram{ |
| 825 |
Histogram: &internal.Histogram{ |
| 826 |
AggregationTemporality: internal.AggregationTemporality_AGGREGATION_TEMPORALITY_DELTA, |
| 827 |
DataPoints: []*internal.HistogramDataPoint{ |
| 828 |
{ |
| 829 |
Attributes: []internal.KeyValue{ |
| 830 |
{ |
| 831 |
Key: "key0", |
| 832 |
Value: internal.AnyValue{Value: &internal.AnyValue_StringValue{StringValue: "value0"}}, |
| 833 |
}, |
| 834 |
}, |
| 835 |
StartTimeUnixNano: startTime, |
| 836 |
TimeUnixNano: endTime, |
| 837 |
BucketCounts: []uint64{10, 15, 1}, |
| 838 |
ExplicitBounds: []float64{1, 2}, |
| 839 |
}, |
| 840 |
{ |
| 841 |
Attributes: []internal.KeyValue{ |
| 842 |
{ |
| 843 |
Key: "key1", |
| 844 |
Value: internal.AnyValue{Value: &internal.AnyValue_StringValue{StringValue: "value1"}}, |
| 845 |
}, |
| 846 |
}, |
| 847 |
StartTimeUnixNano: startTime, |
| 848 |
TimeUnixNano: endTime, |
| 849 |
BucketCounts: []uint64{10, 1}, |
| 850 |
ExplicitBounds: []float64{1}, |
| 851 |
}, |
| 852 |
}, |
| 853 |
}, |
| 854 |
}, |
| 855 |
} |
| 856 |
} |
| 857 |
|
| 858 |
func generateMetricsEmptyResource() Metrics { |
| 859 |
return newMetrics(&internal.ExportMetricsServiceRequest{ |
| 860 |
ResourceMetrics: []*internal.ResourceMetrics{{}}, |
| 861 |
}, new(internal.State)) |
| 862 |
} |
| 863 |
|
| 864 |
func generateMetricsEmptyInstrumentation() Metrics { |
| 865 |
return newMetrics(&internal.ExportMetricsServiceRequest{ |
| 866 |
ResourceMetrics: []*internal.ResourceMetrics{ |
| 867 |
{ |
| 868 |
ScopeMetrics: []*internal.ScopeMetrics{{}}, |
| 869 |
}, |
| 870 |
}, |
| 871 |
}, new(internal.State)) |
| 872 |
} |
| 873 |
|
| 874 |
func generateMetricsEmptyMetrics() Metrics { |
| 875 |
return newMetrics(&internal.ExportMetricsServiceRequest{ |
| 876 |
ResourceMetrics: []*internal.ResourceMetrics{ |
| 877 |
{ |
| 878 |
ScopeMetrics: []*internal.ScopeMetrics{ |
| 879 |
{ |
| 880 |
Metrics: []*internal.Metric{{}}, |
| 881 |
}, |
| 882 |
}, |
| 883 |
}, |
| 884 |
}, |
| 885 |
}, new(internal.State)) |
| 886 |
} |
| 887 |
|
| 888 |
func generateMetricsEmptyDataPoints() Metrics { |
| 889 |
return newMetrics(&internal.ExportMetricsServiceRequest{ |
| 890 |
ResourceMetrics: []*internal.ResourceMetrics{ |
| 891 |
{ |
| 892 |
ScopeMetrics: []*internal.ScopeMetrics{ |
| 893 |
{ |
| 894 |
Metrics: []*internal.Metric{ |
| 895 |
{ |
| 896 |
Data: &internal.Metric_Gauge{ |
| 897 |
Gauge: &internal.Gauge{ |
| 898 |
DataPoints: []*internal.NumberDataPoint{ |
| 899 |
{}, |
| 900 |
}, |
| 901 |
}, |
| 902 |
}, |
| 903 |
}, |
| 904 |
}, |
| 905 |
}, |
| 906 |
}, |
| 907 |
}, |
| 908 |
}, |
| 909 |
}, new(internal.State)) |
| 910 |
} |
| 911 |
|
| 912 |
func BenchmarkMetricsUsage(b *testing.B) { |
| 913 |
md := generateTestMetrics() |
| 914 |
|
| 915 |
ts := pcommon.NewTimestampFromTime(time.Now()) |
| 916 |
|
| 917 |
b.ReportAllocs() |
| 918 |
|
| 919 |
for b.Loop() { |
| 920 |
for i := 0; i < md.ResourceMetrics().Len(); i++ { |
| 921 |
rm := md.ResourceMetrics().At(i) |
| 922 |
res := rm.Resource() |
| 923 |
res.Attributes().PutStr("foo", "bar") |
| 924 |
v, ok := res.Attributes().Get("foo") |
| 925 |
assert.True(b, ok) |
| 926 |
assert.Equal(b, "bar", v.Str()) |
| 927 |
v.SetStr("new-bar") |
| 928 |
assert.Equal(b, "new-bar", v.Str()) |
| 929 |
res.Attributes().Remove("foo") |
| 930 |
for j := 0; j < rm.ScopeMetrics().Len(); j++ { |
| 931 |
sm := rm.ScopeMetrics().At(j) |
| 932 |
for k := 0; k < sm.Metrics().Len(); k++ { |
| 933 |
m := sm.Metrics().At(k) |
| 934 |
m.SetName("new_metric_name") |
| 935 |
assert.Equal(b, "new_metric_name", m.Name()) |
| 936 |
|
| 937 |
if m.Type() == MetricTypeSum { |
| 938 |
assert.Equal(b, MetricTypeSum, m.Type()) |
| 939 |
m.Sum().SetAggregationTemporality(AggregationTemporalityCumulative) |
| 940 |
assert.Equal(b, AggregationTemporalityCumulative, m.Sum().AggregationTemporality()) |
| 941 |
m.Sum().SetIsMonotonic(true) |
| 942 |
assert.True(b, m.Sum().IsMonotonic()) |
| 943 |
for l := 0; l < m.Sum().DataPoints().Len(); l++ { |
| 944 |
dp := m.Sum().DataPoints().At(l) |
| 945 |
dp.SetIntValue(123) |
| 946 |
assert.Equal(b, int64(123), dp.IntValue()) |
| 947 |
assert.Equal(b, NumberDataPointValueTypeInt, dp.ValueType()) |
| 948 |
dp.SetStartTimestamp(ts) |
| 949 |
assert.Equal(b, ts, dp.StartTimestamp()) |
| 950 |
} |
| 951 |
dp := m.Sum().DataPoints().AppendEmpty() |
| 952 |
dp.Attributes().PutStr("foo", "bar") |
| 953 |
dp.SetDoubleValue(123) |
| 954 |
dp.SetStartTimestamp(ts) |
| 955 |
dp.SetTimestamp(ts) |
| 956 |
m.Sum().DataPoints().RemoveIf(func(dp NumberDataPoint) bool { |
| 957 |
_, ok := dp.Attributes().Get("foo") |
| 958 |
return ok |
| 959 |
}) |
| 960 |
} |
| 961 |
} |
| 962 |
} |
| 963 |
} |
| 964 |
} |
| 965 |
} |
| 966 |
|
| 967 |
func BenchmarkMetricsMarshalJSON(b *testing.B) { |
| 968 |
md := generateTestMetrics() |
| 969 |
encoder := &JSONMarshaler{} |
| 970 |
|
| 971 |
b.ReportAllocs() |
| 972 |
|
| 973 |
for b.Loop() { |
| 974 |
jsonBuf, err := encoder.MarshalMetrics(md) |
| 975 |
require.NoError(b, err) |
| 976 |
require.NotNil(b, jsonBuf) |
| 977 |
} |
| 978 |
} |