123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682 |
- // Copyright The OpenTelemetry Authors
- // SPDX-License-Identifier: Apache-2.0
- package internal
- import (
- "math"
- "testing"
- "time"
- "github.com/prometheus/prometheus/model/labels"
- "github.com/prometheus/prometheus/model/textparse"
- "github.com/prometheus/prometheus/model/value"
- "github.com/prometheus/prometheus/scrape"
- "github.com/stretchr/testify/require"
- "go.opentelemetry.io/collector/pdata/pcommon"
- "go.opentelemetry.io/collector/pdata/pmetric"
- "go.uber.org/zap"
- )
- type testMetadataStore map[string]scrape.MetricMetadata
- func (tmc testMetadataStore) GetMetadata(familyName string) (scrape.MetricMetadata, bool) {
- lookup, ok := tmc[familyName]
- return lookup, ok
- }
- func (tmc testMetadataStore) ListMetadata() []scrape.MetricMetadata { return nil }
- func (tmc testMetadataStore) SizeMetadata() int { return 0 }
- func (tmc testMetadataStore) LengthMetadata() int {
- return len(tmc)
- }
- var mc = testMetadataStore{
- "counter": scrape.MetricMetadata{
- Metric: "cr",
- Type: textparse.MetricTypeCounter,
- Help: "This is some help for a counter",
- Unit: "By",
- },
- "gauge": scrape.MetricMetadata{
- Metric: "ge",
- Type: textparse.MetricTypeGauge,
- Help: "This is some help for a gauge",
- Unit: "1",
- },
- "gaugehistogram": scrape.MetricMetadata{
- Metric: "gh",
- Type: textparse.MetricTypeGaugeHistogram,
- Help: "This is some help for a gauge histogram",
- Unit: "?",
- },
- "histogram": scrape.MetricMetadata{
- Metric: "hg",
- Type: textparse.MetricTypeHistogram,
- Help: "This is some help for a histogram",
- Unit: "ms",
- },
- "histogram_with_created": scrape.MetricMetadata{
- Metric: "hg",
- Type: textparse.MetricTypeHistogram,
- Help: "This is some help for a histogram",
- Unit: "ms",
- },
- "histogram_stale": scrape.MetricMetadata{
- Metric: "hg_stale",
- Type: textparse.MetricTypeHistogram,
- Help: "This is some help for a histogram",
- Unit: "ms",
- },
- "summary": scrape.MetricMetadata{
- Metric: "s",
- Type: textparse.MetricTypeSummary,
- Help: "This is some help for a summary",
- Unit: "ms",
- },
- "summary_with_created": scrape.MetricMetadata{
- Metric: "s",
- Type: textparse.MetricTypeSummary,
- Help: "This is some help for a summary",
- Unit: "ms",
- },
- "summary_stale": scrape.MetricMetadata{
- Metric: "s_stale",
- Type: textparse.MetricTypeSummary,
- Help: "This is some help for a summary",
- Unit: "ms",
- },
- "unknown": scrape.MetricMetadata{
- Metric: "u",
- Type: textparse.MetricTypeUnknown,
- Help: "This is some help for an unknown metric",
- Unit: "?",
- },
- }
- func TestMetricGroupData_toDistributionUnitTest(t *testing.T) {
- type scrape struct {
- at int64
- value float64
- metric string
- extraLabel labels.Label
- }
- tests := []struct {
- name string
- metricName string
- labels labels.Labels
- scrapes []*scrape
- want func() pmetric.HistogramDataPoint
- wantErr bool
- intervalStartTimeMs int64
- }{
- {
- name: "histogram with startTimestamp",
- metricName: "histogram",
- intervalStartTimeMs: 11,
- labels: labels.FromMap(map[string]string{"a": "A", "b": "B"}),
- scrapes: []*scrape{
- {at: 11, value: 66, metric: "histogram_count"},
- {at: 11, value: 1004.78, metric: "histogram_sum"},
- {at: 11, value: 33, metric: "histogram_bucket", extraLabel: labels.Label{Name: "le", Value: "0.75"}},
- {at: 11, value: 55, metric: "histogram_bucket", extraLabel: labels.Label{Name: "le", Value: "2.75"}},
- {at: 11, value: 66, metric: "histogram_bucket", extraLabel: labels.Label{Name: "le", Value: "+Inf"}},
- },
- want: func() pmetric.HistogramDataPoint {
- point := pmetric.NewHistogramDataPoint()
- point.SetCount(66)
- point.SetSum(1004.78)
- point.SetTimestamp(pcommon.Timestamp(11 * time.Millisecond)) // the time in milliseconds -> nanoseconds.
- point.ExplicitBounds().FromRaw([]float64{0.75, 2.75})
- point.BucketCounts().FromRaw([]uint64{33, 22, 11})
- point.SetStartTimestamp(pcommon.Timestamp(11 * time.Millisecond)) // the time in milliseconds -> nanoseconds.
- attributes := point.Attributes()
- attributes.PutStr("a", "A")
- attributes.PutStr("b", "B")
- return point
- },
- },
- {
- name: "histogram with startTimestamp from _created",
- metricName: "histogram_with_created",
- intervalStartTimeMs: 11,
- labels: labels.FromMap(map[string]string{"a": "A"}),
- scrapes: []*scrape{
- {at: 11, value: 66, metric: "histogram_with_created_count"},
- {at: 11, value: 1004.78, metric: "histogram_with_created_sum"},
- {at: 11, value: 600.78, metric: "histogram_with_created_created"},
- {
- at: 11,
- value: 33,
- metric: "histogram_with_created_bucket",
- extraLabel: labels.Label{Name: "le", Value: "0.75"},
- },
- {
- at: 11,
- value: 55,
- metric: "histogram_with_created_bucket",
- extraLabel: labels.Label{Name: "le", Value: "2.75"},
- },
- {
- at: 11,
- value: 66,
- metric: "histogram_with_created_bucket",
- extraLabel: labels.Label{Name: "le", Value: "+Inf"}},
- },
- want: func() pmetric.HistogramDataPoint {
- point := pmetric.NewHistogramDataPoint()
- point.SetCount(66)
- point.SetSum(1004.78)
- // the time in milliseconds -> nanoseconds.
- point.SetTimestamp(pcommon.Timestamp(11 * time.Millisecond))
- point.SetStartTimestamp(timestampFromFloat64(600.78))
- point.ExplicitBounds().FromRaw([]float64{0.75, 2.75})
- point.BucketCounts().FromRaw([]uint64{33, 22, 11})
- attributes := point.Attributes()
- attributes.PutStr("a", "A")
- return point
- },
- },
- {
- name: "histogram that is stale",
- metricName: "histogram_stale",
- intervalStartTimeMs: 11,
- labels: labels.FromMap(map[string]string{"a": "A", "b": "B"}),
- scrapes: []*scrape{
- {at: 11, value: math.Float64frombits(value.StaleNaN), metric: "histogram_stale_count"},
- {at: 11, value: math.Float64frombits(value.StaleNaN), metric: "histogram_stale_sum"},
- {at: 11, value: math.Float64frombits(value.StaleNaN), metric: "histogram_bucket", extraLabel: labels.Label{Name: "le", Value: "0.75"}},
- {at: 11, value: math.Float64frombits(value.StaleNaN), metric: "histogram_bucket", extraLabel: labels.Label{Name: "le", Value: "2.75"}},
- {at: 11, value: math.Float64frombits(value.StaleNaN), metric: "histogram_bucket", extraLabel: labels.Label{Name: "le", Value: "+Inf"}},
- },
- want: func() pmetric.HistogramDataPoint {
- point := pmetric.NewHistogramDataPoint()
- point.SetTimestamp(pcommon.Timestamp(11 * time.Millisecond)) // the time in milliseconds -> nanoseconds.
- point.SetFlags(pmetric.DefaultDataPointFlags.WithNoRecordedValue(true))
- point.ExplicitBounds().FromRaw([]float64{0.75, 2.75})
- point.BucketCounts().FromRaw([]uint64{0, 0, 0})
- point.SetStartTimestamp(pcommon.Timestamp(11 * time.Millisecond)) // the time in milliseconds -> nanoseconds.
- attributes := point.Attributes()
- attributes.PutStr("a", "A")
- attributes.PutStr("b", "B")
- return point
- },
- },
- {
- name: "histogram with inconsistent timestamps",
- metricName: "histogram_inconsistent_ts",
- intervalStartTimeMs: 11,
- labels: labels.FromMap(map[string]string{"a": "A", "le": "0.75", "b": "B"}),
- scrapes: []*scrape{
- {at: 11, value: math.Float64frombits(value.StaleNaN), metric: "histogram_stale_count"},
- {at: 12, value: math.Float64frombits(value.StaleNaN), metric: "histogram_stale_sum"},
- {at: 13, value: math.Float64frombits(value.StaleNaN), metric: "value"},
- },
- wantErr: true,
- },
- {
- name: "histogram without buckets",
- metricName: "histogram",
- intervalStartTimeMs: 11,
- labels: labels.FromMap(map[string]string{"a": "A", "b": "B"}),
- scrapes: []*scrape{
- {at: 11, value: 66, metric: "histogram_count"},
- {at: 11, value: 1004.78, metric: "histogram_sum"},
- },
- want: func() pmetric.HistogramDataPoint {
- point := pmetric.NewHistogramDataPoint()
- point.SetCount(66)
- point.SetSum(1004.78)
- point.SetTimestamp(pcommon.Timestamp(11 * time.Millisecond)) // the time in milliseconds -> nanoseconds.
- point.SetStartTimestamp(pcommon.Timestamp(11 * time.Millisecond)) // the time in milliseconds -> nanoseconds.
- point.BucketCounts().FromRaw([]uint64{66})
- attributes := point.Attributes()
- attributes.PutStr("a", "A")
- attributes.PutStr("b", "B")
- return point
- },
- },
- }
- for _, tt := range tests {
- tt := tt
- t.Run(tt.name, func(t *testing.T) {
- mp := newMetricFamily(tt.metricName, mc, zap.NewNop())
- for i, tv := range tt.scrapes {
- var lbls labels.Labels
- if tv.extraLabel.Name != "" {
- lbls = labels.NewBuilder(tt.labels).Set(tv.extraLabel.Name, tv.extraLabel.Value).Labels()
- } else {
- lbls = tt.labels.Copy()
- }
- sRef, _ := getSeriesRef(nil, lbls, mp.mtype)
- err := mp.addSeries(sRef, tv.metric, lbls, tv.at, tv.value)
- if tt.wantErr {
- if i != 0 {
- require.Error(t, err)
- }
- } else {
- require.NoError(t, err)
- }
- }
- if tt.wantErr {
- // Don't check the result if we got an error
- return
- }
- require.Len(t, mp.groups, 1)
- sl := pmetric.NewMetricSlice()
- mp.appendMetric(sl, false)
- require.Equal(t, 1, sl.Len(), "Exactly one metric expected")
- metric := sl.At(0)
- require.Equal(t, mc[tt.metricName].Help, metric.Description(), "Expected help metadata in metric description")
- require.Equal(t, mc[tt.metricName].Unit, metric.Unit(), "Expected unit metadata in metric")
- hdpL := metric.Histogram().DataPoints()
- require.Equal(t, 1, hdpL.Len(), "Exactly one point expected")
- got := hdpL.At(0)
- want := tt.want()
- require.Equal(t, want, got, "Expected the points to be equal")
- })
- }
- }
- func TestMetricGroupData_toSummaryUnitTest(t *testing.T) {
- type scrape struct {
- at int64
- value float64
- metric string
- }
- type labelsScrapes struct {
- labels labels.Labels
- scrapes []*scrape
- }
- tests := []struct {
- name string
- labelsScrapes []*labelsScrapes
- want func() pmetric.SummaryDataPoint
- wantErr bool
- }{
- {
- name: "summary",
- labelsScrapes: []*labelsScrapes{
- {
- labels: labels.FromMap(map[string]string{"a": "A", "b": "B"}),
- scrapes: []*scrape{
- {at: 14, value: 10, metric: "summary_count"},
- {at: 14, value: 15, metric: "summary_sum"},
- },
- },
- {
- labels: labels.FromMap(map[string]string{"a": "A", "quantile": "0.0", "b": "B"}),
- scrapes: []*scrape{
- {at: 14, value: 8, metric: "value"},
- },
- },
- {
- labels: labels.FromMap(map[string]string{"a": "A", "quantile": "0.75", "b": "B"}),
- scrapes: []*scrape{
- {at: 14, value: 33.7, metric: "value"},
- },
- },
- {
- labels: labels.FromMap(map[string]string{"a": "A", "quantile": "0.50", "b": "B"}),
- scrapes: []*scrape{
- {at: 14, value: 27, metric: "value"},
- },
- },
- {
- labels: labels.FromMap(map[string]string{"a": "A", "quantile": "0.90", "b": "B"}),
- scrapes: []*scrape{
- {at: 14, value: 56, metric: "value"},
- },
- },
- {
- labels: labels.FromMap(map[string]string{"a": "A", "quantile": "0.99", "b": "B"}),
- scrapes: []*scrape{
- {at: 14, value: 82, metric: "value"},
- },
- },
- },
- want: func() pmetric.SummaryDataPoint {
- point := pmetric.NewSummaryDataPoint()
- point.SetCount(10)
- point.SetSum(15)
- qtL := point.QuantileValues()
- qn0 := qtL.AppendEmpty()
- qn0.SetQuantile(0)
- qn0.SetValue(8)
- qn50 := qtL.AppendEmpty()
- qn50.SetQuantile(.5)
- qn50.SetValue(27)
- qn75 := qtL.AppendEmpty()
- qn75.SetQuantile(.75)
- qn75.SetValue(33.7)
- qn90 := qtL.AppendEmpty()
- qn90.SetQuantile(.9)
- qn90.SetValue(56)
- qn99 := qtL.AppendEmpty()
- qn99.SetQuantile(.99)
- qn99.SetValue(82)
- point.SetTimestamp(pcommon.Timestamp(14 * time.Millisecond)) // the time in milliseconds -> nanoseconds.
- point.SetStartTimestamp(pcommon.Timestamp(14 * time.Millisecond)) // the time in milliseconds -> nanoseconds
- attributes := point.Attributes()
- attributes.PutStr("a", "A")
- attributes.PutStr("b", "B")
- return point
- },
- },
- {
- name: "summary_with_created",
- labelsScrapes: []*labelsScrapes{
- {
- labels: labels.FromMap(map[string]string{"a": "A", "b": "B"}),
- scrapes: []*scrape{
- {at: 14, value: 10, metric: "summary_with_created_count"},
- {at: 14, value: 15, metric: "summary_with_created_sum"},
- {at: 14, value: 150, metric: "summary_with_created_created"},
- },
- },
- {
- labels: labels.FromMap(map[string]string{"a": "A", "quantile": "0.0", "b": "B"}),
- scrapes: []*scrape{
- {at: 14, value: 8, metric: "value"},
- },
- },
- {
- labels: labels.FromMap(map[string]string{"a": "A", "quantile": "0.75", "b": "B"}),
- scrapes: []*scrape{
- {at: 14, value: 33.7, metric: "value"},
- },
- },
- {
- labels: labels.FromMap(map[string]string{"a": "A", "quantile": "0.50", "b": "B"}),
- scrapes: []*scrape{
- {at: 14, value: 27, metric: "value"},
- },
- },
- {
- labels: labels.FromMap(map[string]string{"a": "A", "quantile": "0.90", "b": "B"}),
- scrapes: []*scrape{
- {at: 14, value: 56, metric: "value"},
- },
- },
- {
- labels: labels.FromMap(map[string]string{"a": "A", "quantile": "0.99", "b": "B"}),
- scrapes: []*scrape{
- {at: 14, value: 82, metric: "value"},
- },
- },
- },
- want: func() pmetric.SummaryDataPoint {
- point := pmetric.NewSummaryDataPoint()
- point.SetCount(10)
- point.SetSum(15)
- qtL := point.QuantileValues()
- qn0 := qtL.AppendEmpty()
- qn0.SetQuantile(0)
- qn0.SetValue(8)
- qn50 := qtL.AppendEmpty()
- qn50.SetQuantile(.5)
- qn50.SetValue(27)
- qn75 := qtL.AppendEmpty()
- qn75.SetQuantile(.75)
- qn75.SetValue(33.7)
- qn90 := qtL.AppendEmpty()
- qn90.SetQuantile(.9)
- qn90.SetValue(56)
- qn99 := qtL.AppendEmpty()
- qn99.SetQuantile(.99)
- qn99.SetValue(82)
- // the time in milliseconds -> nanoseconds.
- point.SetTimestamp(pcommon.Timestamp(14 * time.Millisecond))
- point.SetStartTimestamp(timestampFromFloat64(150))
- attributes := point.Attributes()
- attributes.PutStr("a", "A")
- attributes.PutStr("b", "B")
- return point
- },
- },
- {
- name: "summary_stale",
- labelsScrapes: []*labelsScrapes{
- {
- labels: labels.FromMap(map[string]string{"a": "A", "quantile": "0.0", "b": "B"}),
- scrapes: []*scrape{
- {at: 14, value: 10, metric: "summary_stale_count"},
- {at: 14, value: 12, metric: "summary_stale_sum"},
- {at: 14, value: 8, metric: "value"},
- },
- },
- {
- labels: labels.FromMap(map[string]string{"a": "A", "quantile": "0.75", "b": "B"}),
- scrapes: []*scrape{
- {at: 14, value: 10, metric: "summary_stale_count"},
- {at: 14, value: 1004.78, metric: "summary_stale_sum"},
- {at: 14, value: 33.7, metric: "value"},
- },
- },
- {
- labels: labels.FromMap(map[string]string{"a": "A", "quantile": "0.50", "b": "B"}),
- scrapes: []*scrape{
- {at: 14, value: 10, metric: "summary_stale_count"},
- {at: 14, value: 13, metric: "summary_stale_sum"},
- {at: 14, value: 27, metric: "value"},
- },
- },
- {
- labels: labels.FromMap(map[string]string{"a": "A", "quantile": "0.90", "b": "B"}),
- scrapes: []*scrape{
- {at: 14, value: 10, metric: "summary_stale_count"},
- {at: 14, value: 14, metric: "summary_stale_sum"},
- {at: 14, value: 56, metric: "value"},
- },
- },
- {
- labels: labels.FromMap(map[string]string{"a": "A", "quantile": "0.99", "b": "B"}),
- scrapes: []*scrape{
- {at: 14, value: math.Float64frombits(value.StaleNaN), metric: "summary_stale_count"},
- {at: 14, value: math.Float64frombits(value.StaleNaN), metric: "summary_stale_sum"},
- {at: 14, value: math.Float64frombits(value.StaleNaN), metric: "value"},
- },
- },
- },
- want: func() pmetric.SummaryDataPoint {
- point := pmetric.NewSummaryDataPoint()
- qtL := point.QuantileValues()
- qn0 := qtL.AppendEmpty()
- point.SetFlags(pmetric.DefaultDataPointFlags.WithNoRecordedValue(true))
- qn0.SetQuantile(0)
- qn0.SetValue(0)
- qn50 := qtL.AppendEmpty()
- qn50.SetQuantile(.5)
- qn50.SetValue(0)
- qn75 := qtL.AppendEmpty()
- qn75.SetQuantile(.75)
- qn75.SetValue(0)
- qn90 := qtL.AppendEmpty()
- qn90.SetQuantile(.9)
- qn90.SetValue(0)
- qn99 := qtL.AppendEmpty()
- qn99.SetQuantile(.99)
- qn99.SetValue(0)
- point.SetTimestamp(pcommon.Timestamp(14 * time.Millisecond)) // the time in milliseconds -> nanoseconds.
- point.SetStartTimestamp(pcommon.Timestamp(14 * time.Millisecond)) // the time in milliseconds -> nanoseconds
- attributes := point.Attributes()
- attributes.PutStr("a", "A")
- attributes.PutStr("b", "B")
- return point
- },
- },
- {
- name: "summary with inconsistent timestamps",
- labelsScrapes: []*labelsScrapes{
- {
- labels: labels.FromMap(map[string]string{"a": "A", "b": "B"}),
- scrapes: []*scrape{
- {at: 11, value: 10, metric: "summary_count"},
- {at: 14, value: 15, metric: "summary_sum"},
- },
- },
- },
- wantErr: true,
- },
- }
- for _, tt := range tests {
- tt := tt
- t.Run(tt.name, func(t *testing.T) {
- mp := newMetricFamily(tt.name, mc, zap.NewNop())
- for _, lbs := range tt.labelsScrapes {
- for i, scrape := range lbs.scrapes {
- lb := lbs.labels.Copy()
- sRef, _ := getSeriesRef(nil, lb, mp.mtype)
- err := mp.addSeries(sRef, scrape.metric, lb, scrape.at, scrape.value)
- if tt.wantErr {
- // The first scrape won't have an error
- if i != 0 {
- require.Error(t, err)
- }
- } else {
- require.NoError(t, err)
- }
- }
- }
- if tt.wantErr {
- // Don't check the result if we got an error
- return
- }
- require.Len(t, mp.groups, 1)
- sl := pmetric.NewMetricSlice()
- mp.appendMetric(sl, false)
- require.Equal(t, 1, sl.Len(), "Exactly one metric expected")
- metric := sl.At(0)
- require.Equal(t, mc[tt.name].Help, metric.Description(), "Expected help metadata in metric description")
- require.Equal(t, mc[tt.name].Unit, metric.Unit(), "Expected unit metadata in metric")
- sdpL := metric.Summary().DataPoints()
- require.Equal(t, 1, sdpL.Len(), "Exactly one point expected")
- got := sdpL.At(0)
- want := tt.want()
- require.Equal(t, want, got, "Expected the points to be equal")
- })
- }
- }
- func TestMetricGroupData_toNumberDataUnitTest(t *testing.T) {
- type scrape struct {
- at int64
- value float64
- metric string
- }
- tests := []struct {
- name string
- metricKind string
- labels labels.Labels
- scrapes []*scrape
- intervalStartTimestampMs int64
- want func() pmetric.NumberDataPoint
- }{
- {
- metricKind: "counter",
- name: "counter:: startTimestampMs from _created",
- intervalStartTimestampMs: 11,
- labels: labels.FromMap(map[string]string{"a": "A", "b": "B"}),
- scrapes: []*scrape{
- {at: 13, value: 33.7, metric: "value"},
- {at: 13, value: 150, metric: "value_created"},
- },
- want: func() pmetric.NumberDataPoint {
- point := pmetric.NewNumberDataPoint()
- point.SetDoubleValue(33.7)
- // the time in milliseconds -> nanoseconds.
- point.SetTimestamp(pcommon.Timestamp(13 * time.Millisecond))
- point.SetStartTimestamp(timestampFromFloat64(150))
- attributes := point.Attributes()
- attributes.PutStr("a", "A")
- attributes.PutStr("b", "B")
- return point
- },
- },
- {
- metricKind: "counter",
- name: "counter:: startTimestampMs of 11",
- intervalStartTimestampMs: 11,
- labels: labels.FromMap(map[string]string{"a": "A", "b": "B"}),
- scrapes: []*scrape{
- {at: 13, value: 33.7, metric: "value"},
- },
- want: func() pmetric.NumberDataPoint {
- point := pmetric.NewNumberDataPoint()
- point.SetDoubleValue(33.7)
- point.SetTimestamp(pcommon.Timestamp(13 * time.Millisecond)) // the time in milliseconds -> nanoseconds.
- point.SetStartTimestamp(pcommon.Timestamp(13 * time.Millisecond)) // the time in milliseconds -> nanoseconds.
- attributes := point.Attributes()
- attributes.PutStr("a", "A")
- attributes.PutStr("b", "B")
- return point
- },
- },
- {
- name: "counter:: startTimestampMs of 0",
- metricKind: "counter",
- intervalStartTimestampMs: 0,
- labels: labels.FromMap(map[string]string{"a": "A", "b": "B"}),
- scrapes: []*scrape{
- {at: 28, value: 99.9, metric: "value"},
- },
- want: func() pmetric.NumberDataPoint {
- point := pmetric.NewNumberDataPoint()
- point.SetDoubleValue(99.9)
- point.SetTimestamp(pcommon.Timestamp(28 * time.Millisecond)) // the time in milliseconds -> nanoseconds.
- point.SetStartTimestamp(pcommon.Timestamp(28 * time.Millisecond)) // the time in milliseconds -> nanoseconds.
- attributes := point.Attributes()
- attributes.PutStr("a", "A")
- attributes.PutStr("b", "B")
- return point
- },
- },
- }
- for _, tt := range tests {
- tt := tt
- t.Run(tt.name, func(t *testing.T) {
- mp := newMetricFamily(tt.metricKind, mc, zap.NewNop())
- for _, tv := range tt.scrapes {
- lb := tt.labels.Copy()
- sRef, _ := getSeriesRef(nil, lb, mp.mtype)
- require.NoError(t, mp.addSeries(sRef, tv.metric, lb, tv.at, tv.value))
- }
- require.Len(t, mp.groups, 1)
- sl := pmetric.NewMetricSlice()
- mp.appendMetric(sl, false)
- require.Equal(t, 1, sl.Len(), "Exactly one metric expected")
- metric := sl.At(0)
- require.Equal(t, mc[tt.metricKind].Help, metric.Description(), "Expected help metadata in metric description")
- require.Equal(t, mc[tt.metricKind].Unit, metric.Unit(), "Expected unit metadata in metric")
- ndpL := metric.Sum().DataPoints()
- require.Equal(t, 1, ndpL.Len(), "Exactly one point expected")
- got := ndpL.At(0)
- want := tt.want()
- require.Equal(t, want, got, "Expected the points to be equal")
- })
- }
- }
|