123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602 |
- // Copyright The OpenTelemetry Authors
- // SPDX-License-Identifier: Apache-2.0
- package internal
- import (
- "context"
- "errors"
- "testing"
- "time"
- "github.com/prometheus/common/model"
- "github.com/prometheus/prometheus/model/exemplar"
- "github.com/prometheus/prometheus/model/labels"
- "github.com/prometheus/prometheus/model/metadata"
- "github.com/prometheus/prometheus/scrape"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "go.opentelemetry.io/collector/component"
- "go.opentelemetry.io/collector/consumer/consumertest"
- "go.opentelemetry.io/collector/pdata/pcommon"
- "go.opentelemetry.io/collector/pdata/pmetric"
- "go.opentelemetry.io/collector/receiver/receiverhelper"
- "go.opentelemetry.io/collector/receiver/receivertest"
- "go.uber.org/zap"
- "go.uber.org/zap/zaptest/observer"
- )
- const (
- startTimestamp = pcommon.Timestamp(1555366608340000000)
- ts = int64(1555366610000)
- interval = int64(15 * 1000)
- tsNanos = pcommon.Timestamp(ts * 1e6)
- tsPlusIntervalNanos = pcommon.Timestamp((ts + interval) * 1e6)
- )
- var (
- target = scrape.NewTarget(
- // processedLabels contain label values after processing (e.g. relabeling)
- labels.FromMap(map[string]string{
- model.InstanceLabel: "localhost:8080",
- }),
- // discoveredLabels contain labels prior to any processing
- labels.FromMap(map[string]string{
- model.AddressLabel: "address:8080",
- model.SchemeLabel: "http",
- }),
- nil)
- scrapeCtx = scrape.ContextWithMetricMetadataStore(
- scrape.ContextWithTarget(context.Background(), target),
- testMetadataStore(testMetadata))
- )
- func TestTransactionCommitWithoutAdding(t *testing.T) {
- tr := newTransaction(scrapeCtx, &startTimeAdjuster{startTime: startTimestamp}, consumertest.NewNop(), nil, receivertest.NewNopCreateSettings(), nopObsRecv(t), false)
- assert.NoError(t, tr.Commit())
- }
- func TestTransactionRollbackDoesNothing(t *testing.T) {
- tr := newTransaction(scrapeCtx, &startTimeAdjuster{startTime: startTimestamp}, consumertest.NewNop(), nil, receivertest.NewNopCreateSettings(), nopObsRecv(t), false)
- assert.NoError(t, tr.Rollback())
- }
- func TestTransactionUpdateMetadataDoesNothing(t *testing.T) {
- tr := newTransaction(scrapeCtx, &startTimeAdjuster{startTime: startTimestamp}, consumertest.NewNop(), nil, receivertest.NewNopCreateSettings(), nopObsRecv(t), false)
- _, err := tr.UpdateMetadata(0, labels.New(), metadata.Metadata{})
- assert.NoError(t, err)
- }
- func TestTransactionAppendNoTarget(t *testing.T) {
- badLabels := labels.FromStrings(model.MetricNameLabel, "counter_test")
- tr := newTransaction(scrapeCtx, &startTimeAdjuster{startTime: startTimestamp}, consumertest.NewNop(), nil, receivertest.NewNopCreateSettings(), nopObsRecv(t), false)
- _, err := tr.Append(0, badLabels, time.Now().Unix()*1000, 1.0)
- assert.Error(t, err)
- }
- func TestTransactionAppendNoMetricName(t *testing.T) {
- jobNotFoundLb := labels.FromMap(map[string]string{
- model.InstanceLabel: "localhost:8080",
- model.JobLabel: "test2",
- })
- tr := newTransaction(scrapeCtx, &startTimeAdjuster{startTime: startTimestamp}, consumertest.NewNop(), nil, receivertest.NewNopCreateSettings(), nopObsRecv(t), false)
- _, err := tr.Append(0, jobNotFoundLb, time.Now().Unix()*1000, 1.0)
- assert.ErrorIs(t, err, errMetricNameNotFound)
- assert.ErrorIs(t, tr.Commit(), errNoDataToBuild)
- }
- func TestTransactionAppendEmptyMetricName(t *testing.T) {
- tr := newTransaction(scrapeCtx, &startTimeAdjuster{startTime: startTimestamp}, consumertest.NewNop(), nil, receivertest.NewNopCreateSettings(), nopObsRecv(t), false)
- _, err := tr.Append(0, labels.FromMap(map[string]string{
- model.InstanceLabel: "localhost:8080",
- model.JobLabel: "test2",
- model.MetricNameLabel: "",
- }), time.Now().Unix()*1000, 1.0)
- assert.ErrorIs(t, err, errMetricNameNotFound)
- }
- func TestTransactionAppendResource(t *testing.T) {
- sink := new(consumertest.MetricsSink)
- tr := newTransaction(scrapeCtx, &startTimeAdjuster{startTime: startTimestamp}, sink, nil, receivertest.NewNopCreateSettings(), nopObsRecv(t), false)
- _, err := tr.Append(0, labels.FromMap(map[string]string{
- model.InstanceLabel: "localhost:8080",
- model.JobLabel: "test",
- model.MetricNameLabel: "counter_test",
- }), time.Now().Unix()*1000, 1.0)
- assert.NoError(t, err)
- _, err = tr.Append(0, labels.FromMap(map[string]string{
- model.InstanceLabel: "localhost:8080",
- model.JobLabel: "test",
- model.MetricNameLabel: startTimeMetricName,
- }), time.Now().UnixMilli(), 1.0)
- assert.NoError(t, err)
- assert.NoError(t, tr.Commit())
- expectedResource := CreateResource("test", "localhost:8080", labels.FromStrings(model.SchemeLabel, "http"))
- mds := sink.AllMetrics()
- require.Len(t, mds, 1)
- gotResource := mds[0].ResourceMetrics().At(0).Resource()
- require.Equal(t, expectedResource, gotResource)
- }
- func TestReceiverVersionAndNameAreAttached(t *testing.T) {
- sink := new(consumertest.MetricsSink)
- tr := newTransaction(scrapeCtx, &startTimeAdjuster{startTime: startTimestamp}, sink, nil, receivertest.NewNopCreateSettings(), nopObsRecv(t), false)
- _, err := tr.Append(0, labels.FromMap(map[string]string{
- model.InstanceLabel: "localhost:8080",
- model.JobLabel: "test",
- model.MetricNameLabel: "counter_test",
- }), time.Now().Unix()*1000, 1.0)
- assert.NoError(t, err)
- assert.NoError(t, tr.Commit())
- expectedResource := CreateResource("test", "localhost:8080", labels.FromStrings(model.SchemeLabel, "http"))
- mds := sink.AllMetrics()
- require.Len(t, mds, 1)
- gotResource := mds[0].ResourceMetrics().At(0).Resource()
- require.Equal(t, expectedResource, gotResource)
- gotScope := mds[0].ResourceMetrics().At(0).ScopeMetrics().At(0).Scope()
- require.Equal(t, receiverName, gotScope.Name())
- require.Equal(t, component.NewDefaultBuildInfo().Version, gotScope.Version())
- }
- func TestTransactionCommitErrorWhenAdjusterError(t *testing.T) {
- goodLabels := labels.FromMap(map[string]string{
- model.InstanceLabel: "localhost:8080",
- model.JobLabel: "test",
- model.MetricNameLabel: "counter_test",
- })
- sink := new(consumertest.MetricsSink)
- adjusterErr := errors.New("adjuster error")
- tr := newTransaction(scrapeCtx, &errorAdjuster{err: adjusterErr}, sink, nil, receivertest.NewNopCreateSettings(), nopObsRecv(t), false)
- _, err := tr.Append(0, goodLabels, time.Now().Unix()*1000, 1.0)
- assert.NoError(t, err)
- assert.ErrorIs(t, tr.Commit(), adjusterErr)
- }
- // Ensure that we reject duplicate label keys. See https://github.com/open-telemetry/wg-prometheus/issues/44.
- func TestTransactionAppendDuplicateLabels(t *testing.T) {
- sink := new(consumertest.MetricsSink)
- tr := newTransaction(scrapeCtx, &startTimeAdjuster{startTime: startTimestamp}, sink, nil, receivertest.NewNopCreateSettings(), nopObsRecv(t), false)
- dupLabels := labels.FromStrings(
- model.InstanceLabel, "0.0.0.0:8855",
- model.JobLabel, "test",
- model.MetricNameLabel, "counter_test",
- "a", "1",
- "a", "6",
- "z", "9",
- )
- _, err := tr.Append(0, dupLabels, 1917, 1.0)
- require.Error(t, err)
- assert.Contains(t, err.Error(), `invalid sample: non-unique label names: "a"`)
- }
- func TestTransactionAppendHistogramNoLe(t *testing.T) {
- sink := new(consumertest.MetricsSink)
- receiverSettings := receivertest.NewNopCreateSettings()
- core, observedLogs := observer.New(zap.InfoLevel)
- receiverSettings.Logger = zap.New(core)
- tr := newTransaction(
- scrapeCtx,
- &startTimeAdjuster{startTime: startTimestamp},
- sink,
- nil,
- receiverSettings,
- nopObsRecv(t),
- false,
- )
- goodLabels := labels.FromStrings(
- model.InstanceLabel, "0.0.0.0:8855",
- model.JobLabel, "test",
- model.MetricNameLabel, "hist_test_bucket",
- )
- _, err := tr.Append(0, goodLabels, 1917, 1.0)
- require.NoError(t, err)
- assert.Equal(t, 1, observedLogs.Len())
- assert.Equal(t, 1, observedLogs.FilterMessage("failed to add datapoint").Len())
- assert.NoError(t, tr.Commit())
- assert.Len(t, sink.AllMetrics(), 0)
- }
- func TestTransactionAppendSummaryNoQuantile(t *testing.T) {
- sink := new(consumertest.MetricsSink)
- receiverSettings := receivertest.NewNopCreateSettings()
- core, observedLogs := observer.New(zap.InfoLevel)
- receiverSettings.Logger = zap.New(core)
- tr := newTransaction(
- scrapeCtx,
- &startTimeAdjuster{startTime: startTimestamp},
- sink,
- nil,
- receiverSettings,
- nopObsRecv(t),
- false,
- )
- goodLabels := labels.FromStrings(
- model.InstanceLabel, "0.0.0.0:8855",
- model.JobLabel, "test",
- model.MetricNameLabel, "summary_test",
- )
- _, err := tr.Append(0, goodLabels, 1917, 1.0)
- require.NoError(t, err)
- assert.Equal(t, 1, observedLogs.Len())
- assert.Equal(t, 1, observedLogs.FilterMessage("failed to add datapoint").Len())
- assert.NoError(t, tr.Commit())
- assert.Len(t, sink.AllMetrics(), 0)
- }
- func TestTransactionAppendValidAndInvalid(t *testing.T) {
- sink := new(consumertest.MetricsSink)
- receiverSettings := receivertest.NewNopCreateSettings()
- core, observedLogs := observer.New(zap.InfoLevel)
- receiverSettings.Logger = zap.New(core)
- tr := newTransaction(
- scrapeCtx,
- &startTimeAdjuster{startTime: startTimestamp},
- sink,
- nil,
- receiverSettings,
- nopObsRecv(t),
- false,
- )
- // a valid counter
- _, err := tr.Append(0, labels.FromMap(map[string]string{
- model.InstanceLabel: "localhost:8080",
- model.JobLabel: "test",
- model.MetricNameLabel: "counter_test",
- }), time.Now().Unix()*1000, 1.0)
- assert.NoError(t, err)
- // summary without quantiles, should be ignored
- summarylabels := labels.FromStrings(
- model.InstanceLabel, "0.0.0.0:8855",
- model.JobLabel, "test",
- model.MetricNameLabel, "summary_test",
- )
- _, err = tr.Append(0, summarylabels, 1917, 1.0)
- require.NoError(t, err)
- assert.Equal(t, 1, observedLogs.Len())
- assert.Equal(t, 1, observedLogs.FilterMessage("failed to add datapoint").Len())
- assert.NoError(t, tr.Commit())
- expectedResource := CreateResource("test", "localhost:8080", labels.FromStrings(model.SchemeLabel, "http"))
- mds := sink.AllMetrics()
- require.Len(t, mds, 1)
- gotResource := mds[0].ResourceMetrics().At(0).Resource()
- require.Equal(t, expectedResource, gotResource)
- require.Equal(t, 1, mds[0].MetricCount())
- }
- func TestAppendExemplarWithNoMetricName(t *testing.T) {
- sink := new(consumertest.MetricsSink)
- tr := newTransaction(scrapeCtx, &startTimeAdjuster{startTime: startTimestamp}, sink, nil, receivertest.NewNopCreateSettings(), nopObsRecv(t), false)
- labels := labels.FromStrings(
- model.InstanceLabel, "0.0.0.0:8855",
- model.JobLabel, "test",
- )
- _, err := tr.AppendExemplar(0, labels, exemplar.Exemplar{Value: 0})
- assert.Equal(t, errMetricNameNotFound, err)
- }
- func TestAppendExemplarWithEmptyMetricName(t *testing.T) {
- sink := new(consumertest.MetricsSink)
- tr := newTransaction(scrapeCtx, &startTimeAdjuster{startTime: startTimestamp}, sink, nil, receivertest.NewNopCreateSettings(), nopObsRecv(t), false)
- labels := labels.FromStrings(
- model.InstanceLabel, "0.0.0.0:8855",
- model.JobLabel, "test",
- model.MetricNameLabel, "",
- )
- _, err := tr.AppendExemplar(0, labels, exemplar.Exemplar{Value: 0})
- assert.Equal(t, errMetricNameNotFound, err)
- }
- func TestAppendExemplarWithDuplicateLabels(t *testing.T) {
- sink := new(consumertest.MetricsSink)
- tr := newTransaction(scrapeCtx, &startTimeAdjuster{startTime: startTimestamp}, sink, nil, receivertest.NewNopCreateSettings(), nopObsRecv(t), false)
- labels := labels.FromStrings(
- model.InstanceLabel, "0.0.0.0:8855",
- model.JobLabel, "test",
- model.MetricNameLabel, "",
- "a", "b",
- "a", "c",
- )
- _, err := tr.AppendExemplar(0, labels, exemplar.Exemplar{Value: 0})
- require.Error(t, err)
- assert.Contains(t, err.Error(), `invalid sample: non-unique label names: "a"`)
- }
- func TestAppendExemplarWithoutAddingMetric(t *testing.T) {
- sink := new(consumertest.MetricsSink)
- tr := newTransaction(scrapeCtx, &startTimeAdjuster{startTime: startTimestamp}, sink, nil, receivertest.NewNopCreateSettings(), nopObsRecv(t), false)
- labels := labels.FromStrings(
- model.InstanceLabel, "0.0.0.0:8855",
- model.JobLabel, "test",
- model.MetricNameLabel, "counter_test",
- "a", "b",
- )
- _, err := tr.AppendExemplar(0, labels, exemplar.Exemplar{Value: 0})
- assert.NoError(t, err)
- }
- func TestAppendExemplarWithNoLabels(t *testing.T) {
- sink := new(consumertest.MetricsSink)
- tr := newTransaction(scrapeCtx, &startTimeAdjuster{startTime: startTimestamp}, sink, nil, receivertest.NewNopCreateSettings(), nopObsRecv(t), false)
- _, err := tr.AppendExemplar(0, nil, exemplar.Exemplar{Value: 0})
- assert.Equal(t, errNoJobInstance, err)
- }
- func TestAppendExemplarWithEmptyLabelArray(t *testing.T) {
- sink := new(consumertest.MetricsSink)
- tr := newTransaction(scrapeCtx, &startTimeAdjuster{startTime: startTimestamp}, sink, nil, receivertest.NewNopCreateSettings(), nopObsRecv(t), false)
- _, err := tr.AppendExemplar(0, []labels.Label{}, exemplar.Exemplar{Value: 0})
- assert.Equal(t, errNoJobInstance, err)
- }
- func nopObsRecv(t *testing.T) *receiverhelper.ObsReport {
- obsrecv, err := receiverhelper.NewObsReport(receiverhelper.ObsReportSettings{
- ReceiverID: component.NewID("prometheus"),
- Transport: transport,
- ReceiverCreateSettings: receivertest.NewNopCreateSettings(),
- })
- require.NoError(t, err)
- return obsrecv
- }
- func TestMetricBuilderCounters(t *testing.T) {
- tests := []buildTestData{
- {
- name: "single-item",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint("counter_test", 100, nil, "foo", "bar"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- md0 := pmetric.NewMetrics()
- mL0 := md0.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m0 := mL0.AppendEmpty()
- m0.SetName("counter_test")
- sum := m0.SetEmptySum()
- sum.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
- sum.SetIsMonotonic(true)
- pt0 := sum.DataPoints().AppendEmpty()
- pt0.SetDoubleValue(100.0)
- pt0.SetStartTimestamp(startTimestamp)
- pt0.SetTimestamp(tsNanos)
- pt0.Attributes().PutStr("foo", "bar")
- return []pmetric.Metrics{md0}
- },
- },
- {
- name: "single-item-with-exemplars",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint(
- "counter_test",
- 100,
- []exemplar.Exemplar{
- {
- Value: 1,
- Ts: 1663113420863,
- Labels: []labels.Label{{Name: model.MetricNameLabel, Value: "counter_test"}, {Name: model.JobLabel, Value: "job"}, {Name: model.InstanceLabel, Value: "instance"}, {Name: "foo", Value: "bar"}},
- },
- {
- Value: 1,
- Ts: 1663113420863,
- Labels: []labels.Label{{Name: "foo", Value: "bar"}, {Name: "trace_id", Value: ""}, {Name: "span_id", Value: ""}},
- },
- {
- Value: 1,
- Ts: 1663113420863,
- Labels: []labels.Label{{Name: "foo", Value: "bar"}, {Name: "trace_id", Value: "10a47365b8aa04e08291fab9deca84db6170"}, {Name: "span_id", Value: "719cee4a669fd7d109ff"}},
- },
- {
- Value: 1,
- Ts: 1663113420863,
- Labels: []labels.Label{{Name: "foo", Value: "bar"}, {Name: "trace_id", Value: "174137cab66dc880"}, {Name: "span_id", Value: "dfa4597a9d"}},
- },
- },
- "foo", "bar"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- md0 := pmetric.NewMetrics()
- mL0 := md0.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m0 := mL0.AppendEmpty()
- m0.SetName("counter_test")
- sum := m0.SetEmptySum()
- sum.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
- sum.SetIsMonotonic(true)
- pt0 := sum.DataPoints().AppendEmpty()
- pt0.SetDoubleValue(100.0)
- pt0.SetStartTimestamp(startTimestamp)
- pt0.SetTimestamp(tsNanos)
- pt0.Attributes().PutStr("foo", "bar")
- e0 := pt0.Exemplars().AppendEmpty()
- e0.SetTimestamp(timestampFromMs(1663113420863))
- e0.SetDoubleValue(1)
- e0.FilteredAttributes().PutStr(model.MetricNameLabel, "counter_test")
- e0.FilteredAttributes().PutStr(model.JobLabel, "job")
- e0.FilteredAttributes().PutStr(model.InstanceLabel, "instance")
- e0.FilteredAttributes().PutStr("foo", "bar")
- e1 := pt0.Exemplars().AppendEmpty()
- e1.SetTimestamp(timestampFromMs(1663113420863))
- e1.SetDoubleValue(1)
- e1.FilteredAttributes().PutStr("foo", "bar")
- e2 := pt0.Exemplars().AppendEmpty()
- e2.SetTimestamp(timestampFromMs(1663113420863))
- e2.SetDoubleValue(1)
- e2.FilteredAttributes().PutStr("foo", "bar")
- e2.SetTraceID([16]byte{0x10, 0xa4, 0x73, 0x65, 0xb8, 0xaa, 0x04, 0xe0, 0x82, 0x91, 0xfa, 0xb9, 0xde, 0xca, 0x84, 0xdb})
- e2.SetSpanID([8]byte{0x71, 0x9c, 0xee, 0x4a, 0x66, 0x9f, 0xd7, 0xd1})
- e3 := pt0.Exemplars().AppendEmpty()
- e3.SetTimestamp(timestampFromMs(1663113420863))
- e3.SetDoubleValue(1)
- e3.FilteredAttributes().PutStr("foo", "bar")
- e3.SetTraceID([16]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x41, 0x37, 0xca, 0xb6, 0x6d, 0xc8, 0x80})
- e3.SetSpanID([8]byte{0x00, 0x00, 0x00, 0xdf, 0xa4, 0x59, 0x7a, 0x9d})
- return []pmetric.Metrics{md0}
- },
- },
- {
- name: "two-items",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint("counter_test", 150, nil, "foo", "bar"),
- createDataPoint("counter_test", 25, nil, "foo", "other"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- md0 := pmetric.NewMetrics()
- mL0 := md0.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m0 := mL0.AppendEmpty()
- m0.SetName("counter_test")
- sum := m0.SetEmptySum()
- sum.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
- sum.SetIsMonotonic(true)
- pt0 := sum.DataPoints().AppendEmpty()
- pt0.SetDoubleValue(150.0)
- pt0.SetStartTimestamp(startTimestamp)
- pt0.SetTimestamp(tsNanos)
- pt0.Attributes().PutStr("foo", "bar")
- pt1 := sum.DataPoints().AppendEmpty()
- pt1.SetDoubleValue(25.0)
- pt1.SetStartTimestamp(startTimestamp)
- pt1.SetTimestamp(tsNanos)
- pt1.Attributes().PutStr("foo", "other")
- return []pmetric.Metrics{md0}
- },
- },
- {
- name: "two-metrics",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint("counter_test", 150, nil, "foo", "bar"),
- createDataPoint("counter_test", 25, nil, "foo", "other"),
- createDataPoint("counter_test2", 100, nil, "foo", "bar"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- md0 := pmetric.NewMetrics()
- mL0 := md0.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m0 := mL0.AppendEmpty()
- m0.SetName("counter_test")
- sum0 := m0.SetEmptySum()
- sum0.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
- sum0.SetIsMonotonic(true)
- pt0 := sum0.DataPoints().AppendEmpty()
- pt0.SetDoubleValue(150.0)
- pt0.SetStartTimestamp(startTimestamp)
- pt0.SetTimestamp(tsNanos)
- pt0.Attributes().PutStr("foo", "bar")
- pt1 := sum0.DataPoints().AppendEmpty()
- pt1.SetDoubleValue(25.0)
- pt1.SetStartTimestamp(startTimestamp)
- pt1.SetTimestamp(tsNanos)
- pt1.Attributes().PutStr("foo", "other")
- m1 := mL0.AppendEmpty()
- m1.SetName("counter_test2")
- sum1 := m1.SetEmptySum()
- sum1.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
- sum1.SetIsMonotonic(true)
- pt2 := sum1.DataPoints().AppendEmpty()
- pt2.SetDoubleValue(100.0)
- pt2.SetStartTimestamp(startTimestamp)
- pt2.SetTimestamp(tsNanos)
- pt2.Attributes().PutStr("foo", "bar")
- return []pmetric.Metrics{md0}
- },
- },
- {
- name: "metrics-with-poor-names",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint("poor_name_count", 100, nil, "foo", "bar"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- md0 := pmetric.NewMetrics()
- mL0 := md0.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m0 := mL0.AppendEmpty()
- m0.SetName("poor_name_count")
- sum := m0.SetEmptySum()
- sum.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
- sum.SetIsMonotonic(true)
- pt0 := sum.DataPoints().AppendEmpty()
- pt0.SetDoubleValue(100.0)
- pt0.SetStartTimestamp(startTimestamp)
- pt0.SetTimestamp(tsNanos)
- pt0.Attributes().PutStr("foo", "bar")
- return []pmetric.Metrics{md0}
- },
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- tt.run(t)
- })
- }
- }
- func TestMetricBuilderGauges(t *testing.T) {
- tests := []buildTestData{
- {
- name: "one-gauge",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint("gauge_test", 100, nil, "foo", "bar"),
- },
- },
- {
- pts: []*testDataPoint{
- createDataPoint("gauge_test", 90, nil, "foo", "bar"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- md0 := pmetric.NewMetrics()
- mL0 := md0.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m0 := mL0.AppendEmpty()
- m0.SetName("gauge_test")
- gauge0 := m0.SetEmptyGauge()
- pt0 := gauge0.DataPoints().AppendEmpty()
- pt0.SetDoubleValue(100.0)
- pt0.SetStartTimestamp(0)
- pt0.SetTimestamp(tsNanos)
- pt0.Attributes().PutStr("foo", "bar")
- md1 := pmetric.NewMetrics()
- mL1 := md1.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m1 := mL1.AppendEmpty()
- m1.SetName("gauge_test")
- gauge1 := m1.SetEmptyGauge()
- pt1 := gauge1.DataPoints().AppendEmpty()
- pt1.SetDoubleValue(90.0)
- pt1.SetStartTimestamp(0)
- pt1.SetTimestamp(tsPlusIntervalNanos)
- pt1.Attributes().PutStr("foo", "bar")
- return []pmetric.Metrics{md0, md1}
- },
- },
- {
- name: "one-gauge-with-exemplars",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint(
- "gauge_test",
- 100,
- []exemplar.Exemplar{
- {
- Value: 2,
- Ts: 1663350815890,
- Labels: []labels.Label{{Name: model.MetricNameLabel, Value: "counter_test"}, {Name: model.JobLabel, Value: "job"}, {Name: model.InstanceLabel, Value: "instance"}, {Name: "foo", Value: "bar"}},
- },
- {
- Value: 2,
- Ts: 1663350815890,
- Labels: []labels.Label{{Name: "foo", Value: "bar"}, {Name: "trace_id", Value: ""}, {Name: "span_id", Value: ""}},
- },
- {
- Value: 2,
- Ts: 1663350815890,
- Labels: []labels.Label{{Name: "foo", Value: "bar"}, {Name: "trace_id", Value: "10a47365b8aa04e08291fab9deca84db6170"}, {Name: "span_id", Value: "719cee4a669fd7d109ff"}},
- },
- {
- Value: 2,
- Ts: 1663350815890,
- Labels: []labels.Label{{Name: "foo", Value: "bar"}, {Name: "trace_id", Value: "174137cab66dc880"}, {Name: "span_id", Value: "dfa4597a9d"}},
- },
- },
- "foo", "bar"),
- },
- },
- {
- pts: []*testDataPoint{
- createDataPoint("gauge_test", 90, nil, "foo", "bar"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- md0 := pmetric.NewMetrics()
- mL0 := md0.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m0 := mL0.AppendEmpty()
- m0.SetName("gauge_test")
- gauge0 := m0.SetEmptyGauge()
- pt0 := gauge0.DataPoints().AppendEmpty()
- pt0.SetDoubleValue(100.0)
- pt0.SetStartTimestamp(0)
- pt0.SetTimestamp(tsNanos)
- pt0.Attributes().PutStr("foo", "bar")
- e0 := pt0.Exemplars().AppendEmpty()
- e0.SetTimestamp(timestampFromMs(1663350815890))
- e0.SetDoubleValue(2)
- e0.FilteredAttributes().PutStr(model.MetricNameLabel, "counter_test")
- e0.FilteredAttributes().PutStr(model.JobLabel, "job")
- e0.FilteredAttributes().PutStr(model.InstanceLabel, "instance")
- e0.FilteredAttributes().PutStr("foo", "bar")
- e1 := pt0.Exemplars().AppendEmpty()
- e1.SetTimestamp(timestampFromMs(1663350815890))
- e1.SetDoubleValue(2)
- e1.FilteredAttributes().PutStr("foo", "bar")
- e2 := pt0.Exemplars().AppendEmpty()
- e2.SetTimestamp(timestampFromMs(1663350815890))
- e2.SetDoubleValue(2)
- e2.FilteredAttributes().PutStr("foo", "bar")
- e2.SetTraceID([16]byte{0x10, 0xa4, 0x73, 0x65, 0xb8, 0xaa, 0x04, 0xe0, 0x82, 0x91, 0xfa, 0xb9, 0xde, 0xca, 0x84, 0xdb})
- e2.SetSpanID([8]byte{0x71, 0x9c, 0xee, 0x4a, 0x66, 0x9f, 0xd7, 0xd1})
- e3 := pt0.Exemplars().AppendEmpty()
- e3.SetTimestamp(timestampFromMs(1663350815890))
- e3.SetDoubleValue(2)
- e3.FilteredAttributes().PutStr("foo", "bar")
- e3.SetTraceID([16]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x41, 0x37, 0xca, 0xb6, 0x6d, 0xc8, 0x80})
- e3.SetSpanID([8]byte{0x00, 0x00, 0x00, 0xdf, 0xa4, 0x59, 0x7a, 0x9d})
- md1 := pmetric.NewMetrics()
- mL1 := md1.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m1 := mL1.AppendEmpty()
- m1.SetName("gauge_test")
- gauge1 := m1.SetEmptyGauge()
- pt1 := gauge1.DataPoints().AppendEmpty()
- pt1.SetDoubleValue(90.0)
- pt1.SetStartTimestamp(0)
- pt1.SetTimestamp(tsPlusIntervalNanos)
- pt1.Attributes().PutStr("foo", "bar")
- return []pmetric.Metrics{md0, md1}
- },
- },
- {
- name: "gauge-with-different-tags",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint("gauge_test", 100, nil, "foo", "bar"),
- createDataPoint("gauge_test", 200, nil, "bar", "foo"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- md0 := pmetric.NewMetrics()
- mL0 := md0.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m0 := mL0.AppendEmpty()
- m0.SetName("gauge_test")
- gauge0 := m0.SetEmptyGauge()
- pt0 := gauge0.DataPoints().AppendEmpty()
- pt0.SetDoubleValue(100.0)
- pt0.SetStartTimestamp(0)
- pt0.SetTimestamp(tsNanos)
- pt0.Attributes().PutStr("foo", "bar")
- pt1 := gauge0.DataPoints().AppendEmpty()
- pt1.SetDoubleValue(200.0)
- pt1.SetStartTimestamp(0)
- pt1.SetTimestamp(tsNanos)
- pt1.Attributes().PutStr("bar", "foo")
- return []pmetric.Metrics{md0}
- },
- },
- {
- // TODO: A decision need to be made. If we want to have the behavior which can generate different tag key
- // sets because metrics come and go
- name: "gauge-comes-and-go-with-different-tagset",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint("gauge_test", 100, nil, "foo", "bar"),
- createDataPoint("gauge_test", 200, nil, "bar", "foo"),
- },
- },
- {
- pts: []*testDataPoint{
- createDataPoint("gauge_test", 20, nil, "foo", "bar"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- md0 := pmetric.NewMetrics()
- mL0 := md0.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m0 := mL0.AppendEmpty()
- m0.SetName("gauge_test")
- gauge0 := m0.SetEmptyGauge()
- pt0 := gauge0.DataPoints().AppendEmpty()
- pt0.SetDoubleValue(100.0)
- pt0.SetStartTimestamp(0)
- pt0.SetTimestamp(tsNanos)
- pt0.Attributes().PutStr("foo", "bar")
- pt1 := gauge0.DataPoints().AppendEmpty()
- pt1.SetDoubleValue(200.0)
- pt1.SetStartTimestamp(0)
- pt1.SetTimestamp(tsNanos)
- pt1.Attributes().PutStr("bar", "foo")
- md1 := pmetric.NewMetrics()
- mL1 := md1.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m1 := mL1.AppendEmpty()
- m1.SetName("gauge_test")
- gauge1 := m1.SetEmptyGauge()
- pt2 := gauge1.DataPoints().AppendEmpty()
- pt2.SetDoubleValue(20.0)
- pt2.SetStartTimestamp(0)
- pt2.SetTimestamp(tsPlusIntervalNanos)
- pt2.Attributes().PutStr("foo", "bar")
- return []pmetric.Metrics{md0, md1}
- },
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- tt.run(t)
- })
- }
- }
- func TestMetricBuilderUntyped(t *testing.T) {
- tests := []buildTestData{
- {
- name: "one-unknown",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint("unknown_test", 100, nil, "foo", "bar"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- md0 := pmetric.NewMetrics()
- mL0 := md0.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m0 := mL0.AppendEmpty()
- m0.SetName("unknown_test")
- gauge0 := m0.SetEmptyGauge()
- pt0 := gauge0.DataPoints().AppendEmpty()
- pt0.SetDoubleValue(100.0)
- pt0.SetStartTimestamp(0)
- pt0.SetTimestamp(tsNanos)
- pt0.Attributes().PutStr("foo", "bar")
- return []pmetric.Metrics{md0}
- },
- },
- {
- name: "no-type-hint",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint("something_not_exists", 100, nil, "foo", "bar"),
- createDataPoint("theother_not_exists", 200, nil, "foo", "bar"),
- createDataPoint("theother_not_exists", 300, nil, "bar", "foo"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- md0 := pmetric.NewMetrics()
- mL0 := md0.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m0 := mL0.AppendEmpty()
- m0.SetName("something_not_exists")
- gauge0 := m0.SetEmptyGauge()
- pt0 := gauge0.DataPoints().AppendEmpty()
- pt0.SetDoubleValue(100.0)
- pt0.SetTimestamp(tsNanos)
- pt0.Attributes().PutStr("foo", "bar")
- m1 := mL0.AppendEmpty()
- m1.SetName("theother_not_exists")
- gauge1 := m1.SetEmptyGauge()
- pt1 := gauge1.DataPoints().AppendEmpty()
- pt1.SetDoubleValue(200.0)
- pt1.SetTimestamp(tsNanos)
- pt1.Attributes().PutStr("foo", "bar")
- pt2 := gauge1.DataPoints().AppendEmpty()
- pt2.SetDoubleValue(300.0)
- pt2.SetTimestamp(tsNanos)
- pt2.Attributes().PutStr("bar", "foo")
- return []pmetric.Metrics{md0}
- },
- },
- {
- name: "untype-metric-poor-names",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint("some_count", 100, nil, "foo", "bar"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- md0 := pmetric.NewMetrics()
- mL0 := md0.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m0 := mL0.AppendEmpty()
- m0.SetName("some_count")
- gauge0 := m0.SetEmptyGauge()
- pt0 := gauge0.DataPoints().AppendEmpty()
- pt0.SetDoubleValue(100.0)
- pt0.SetTimestamp(tsNanos)
- pt0.Attributes().PutStr("foo", "bar")
- return []pmetric.Metrics{md0}
- },
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- tt.run(t)
- })
- }
- }
- func TestMetricBuilderHistogram(t *testing.T) {
- tests := []buildTestData{
- {
- name: "single item",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint("hist_test_bucket", 1, nil, "foo", "bar", "le", "10"),
- createDataPoint("hist_test_bucket", 2, nil, "foo", "bar", "le", "20"),
- createDataPoint("hist_test_bucket", 10, nil, "foo", "bar", "le", "+inf"),
- createDataPoint("hist_test_sum", 99, nil, "foo", "bar"),
- createDataPoint("hist_test_count", 10, nil, "foo", "bar"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- md0 := pmetric.NewMetrics()
- mL0 := md0.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m0 := mL0.AppendEmpty()
- m0.SetName("hist_test")
- hist0 := m0.SetEmptyHistogram()
- hist0.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
- pt0 := hist0.DataPoints().AppendEmpty()
- pt0.SetCount(10)
- pt0.SetSum(99)
- pt0.ExplicitBounds().FromRaw([]float64{10, 20})
- pt0.BucketCounts().FromRaw([]uint64{1, 1, 8})
- pt0.SetTimestamp(tsNanos)
- pt0.SetStartTimestamp(startTimestamp)
- pt0.Attributes().PutStr("foo", "bar")
- return []pmetric.Metrics{md0}
- },
- },
- {
- name: "single item with exemplars",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint(
- "hist_test_bucket",
- 1,
- []exemplar.Exemplar{
- {
- Value: 1,
- Ts: 1663113420863,
- Labels: []labels.Label{{Name: model.MetricNameLabel, Value: "counter_test"}, {Name: model.JobLabel, Value: "job"}, {Name: model.InstanceLabel, Value: "instance"}, {Name: "foo", Value: "bar"}},
- },
- {
- Value: 1,
- Ts: 1663113420863,
- Labels: []labels.Label{{Name: "foo", Value: "bar"}, {Name: "trace_id", Value: ""}, {Name: "span_id", Value: ""}, {Name: "le", Value: "20"}},
- },
- {
- Value: 1,
- Ts: 1663113420863,
- Labels: []labels.Label{{Name: "foo", Value: "bar"}, {Name: "trace_id", Value: "10a47365b8aa04e08291fab9deca84db6170"}, {Name: "traceid", Value: "e3688e1aa2961786"}, {Name: "span_id", Value: "719cee4a669fd7d109ff"}},
- },
- {
- Value: 1,
- Ts: 1663113420863,
- Labels: []labels.Label{{Name: "foo", Value: "bar"}, {Name: "trace_id", Value: "174137cab66dc880"}, {Name: "span_id", Value: "dfa4597a9d"}},
- },
- {
- Value: 1,
- Ts: 1663113420863,
- Labels: []labels.Label{{Name: "foo", Value: "bar"}, {Name: "trace_id", Value: "174137cab66dc88"}, {Name: "span_id", Value: "dfa4597a9"}},
- },
- },
- "foo", "bar", "le", "10"),
- createDataPoint("hist_test_bucket", 2, nil, "foo", "bar", "le", "20"),
- createDataPoint("hist_test_bucket", 10, nil, "foo", "bar", "le", "+inf"),
- createDataPoint("hist_test_sum", 99, nil, "foo", "bar"),
- createDataPoint("hist_test_count", 10, nil, "foo", "bar"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- md0 := pmetric.NewMetrics()
- mL0 := md0.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m0 := mL0.AppendEmpty()
- m0.SetName("hist_test")
- hist0 := m0.SetEmptyHistogram()
- hist0.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
- pt0 := hist0.DataPoints().AppendEmpty()
- pt0.SetCount(10)
- pt0.SetSum(99)
- pt0.ExplicitBounds().FromRaw([]float64{10, 20})
- pt0.BucketCounts().FromRaw([]uint64{1, 1, 8})
- pt0.SetTimestamp(tsNanos)
- pt0.SetStartTimestamp(startTimestamp)
- pt0.Attributes().PutStr("foo", "bar")
- e0 := pt0.Exemplars().AppendEmpty()
- e0.SetTimestamp(timestampFromMs(1663113420863))
- e0.SetDoubleValue(1)
- e0.FilteredAttributes().PutStr(model.MetricNameLabel, "counter_test")
- e0.FilteredAttributes().PutStr(model.JobLabel, "job")
- e0.FilteredAttributes().PutStr(model.InstanceLabel, "instance")
- e0.FilteredAttributes().PutStr("foo", "bar")
- e1 := pt0.Exemplars().AppendEmpty()
- e1.SetTimestamp(timestampFromMs(1663113420863))
- e1.SetDoubleValue(1)
- e1.FilteredAttributes().PutStr("foo", "bar")
- e1.FilteredAttributes().PutStr("le", "20")
- e2 := pt0.Exemplars().AppendEmpty()
- e2.SetTimestamp(timestampFromMs(1663113420863))
- e2.SetDoubleValue(1)
- e2.FilteredAttributes().PutStr("foo", "bar")
- e2.FilteredAttributes().PutStr("traceid", "e3688e1aa2961786")
- e2.SetTraceID([16]byte{0x10, 0xa4, 0x73, 0x65, 0xb8, 0xaa, 0x04, 0xe0, 0x82, 0x91, 0xfa, 0xb9, 0xde, 0xca, 0x84, 0xdb})
- e2.SetSpanID([8]byte{0x71, 0x9c, 0xee, 0x4a, 0x66, 0x9f, 0xd7, 0xd1})
- e3 := pt0.Exemplars().AppendEmpty()
- e3.SetTimestamp(timestampFromMs(1663113420863))
- e3.SetDoubleValue(1)
- e3.FilteredAttributes().PutStr("foo", "bar")
- e3.SetTraceID([16]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x41, 0x37, 0xca, 0xb6, 0x6d, 0xc8, 0x80})
- e3.SetSpanID([8]byte{0x00, 0x00, 0x00, 0xdf, 0xa4, 0x59, 0x7a, 0x9d})
- e4 := pt0.Exemplars().AppendEmpty()
- e4.SetTimestamp(timestampFromMs(1663113420863))
- e4.SetDoubleValue(1)
- e4.FilteredAttributes().PutStr("foo", "bar")
- e4.FilteredAttributes().PutStr("trace_id", "174137cab66dc88")
- e4.FilteredAttributes().PutStr("span_id", "dfa4597a9")
- return []pmetric.Metrics{md0}
- },
- },
- {
- name: "multi-groups",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint("hist_test_bucket", 1, nil, "foo", "bar", "le", "10"),
- createDataPoint("hist_test_bucket", 2, nil, "foo", "bar", "le", "20"),
- createDataPoint("hist_test_bucket", 10, nil, "foo", "bar", "le", "+inf"),
- createDataPoint("hist_test_sum", 99, nil, "foo", "bar"),
- createDataPoint("hist_test_count", 10, nil, "foo", "bar"),
- createDataPoint("hist_test_bucket", 1, nil, "key2", "v2", "le", "10"),
- createDataPoint("hist_test_bucket", 2, nil, "key2", "v2", "le", "20"),
- createDataPoint("hist_test_bucket", 3, nil, "key2", "v2", "le", "+inf"),
- createDataPoint("hist_test_sum", 50, nil, "key2", "v2"),
- createDataPoint("hist_test_count", 3, nil, "key2", "v2"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- md0 := pmetric.NewMetrics()
- mL0 := md0.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m0 := mL0.AppendEmpty()
- m0.SetName("hist_test")
- hist0 := m0.SetEmptyHistogram()
- hist0.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
- pt0 := hist0.DataPoints().AppendEmpty()
- pt0.SetCount(10)
- pt0.SetSum(99)
- pt0.ExplicitBounds().FromRaw([]float64{10, 20})
- pt0.BucketCounts().FromRaw([]uint64{1, 1, 8})
- pt0.SetTimestamp(tsNanos)
- pt0.SetStartTimestamp(startTimestamp)
- pt0.Attributes().PutStr("foo", "bar")
- pt1 := hist0.DataPoints().AppendEmpty()
- pt1.SetCount(3)
- pt1.SetSum(50)
- pt1.ExplicitBounds().FromRaw([]float64{10, 20})
- pt1.BucketCounts().FromRaw([]uint64{1, 1, 1})
- pt1.SetTimestamp(tsNanos)
- pt1.SetStartTimestamp(startTimestamp)
- pt1.Attributes().PutStr("key2", "v2")
- return []pmetric.Metrics{md0}
- },
- },
- {
- name: "multi-groups-and-families",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint("hist_test_bucket", 1, nil, "foo", "bar", "le", "10"),
- createDataPoint("hist_test_bucket", 2, nil, "foo", "bar", "le", "20"),
- createDataPoint("hist_test_bucket", 10, nil, "foo", "bar", "le", "+inf"),
- createDataPoint("hist_test_sum", 99, nil, "foo", "bar"),
- createDataPoint("hist_test_count", 10, nil, "foo", "bar"),
- createDataPoint("hist_test_bucket", 1, nil, "key2", "v2", "le", "10"),
- createDataPoint("hist_test_bucket", 2, nil, "key2", "v2", "le", "20"),
- createDataPoint("hist_test_bucket", 3, nil, "key2", "v2", "le", "+inf"),
- createDataPoint("hist_test_sum", 50, nil, "key2", "v2"),
- createDataPoint("hist_test_count", 3, nil, "key2", "v2"),
- createDataPoint("hist_test2_bucket", 1, nil, "foo", "bar", "le", "10"),
- createDataPoint("hist_test2_bucket", 2, nil, "foo", "bar", "le", "20"),
- createDataPoint("hist_test2_bucket", 3, nil, "foo", "bar", "le", "+inf"),
- createDataPoint("hist_test2_sum", 50, nil, "foo", "bar"),
- createDataPoint("hist_test2_count", 3, nil, "foo", "bar"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- md0 := pmetric.NewMetrics()
- mL0 := md0.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m0 := mL0.AppendEmpty()
- m0.SetName("hist_test")
- hist0 := m0.SetEmptyHistogram()
- hist0.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
- pt0 := hist0.DataPoints().AppendEmpty()
- pt0.SetCount(10)
- pt0.SetSum(99)
- pt0.ExplicitBounds().FromRaw([]float64{10, 20})
- pt0.BucketCounts().FromRaw([]uint64{1, 1, 8})
- pt0.SetTimestamp(tsNanos)
- pt0.SetStartTimestamp(startTimestamp)
- pt0.Attributes().PutStr("foo", "bar")
- pt1 := hist0.DataPoints().AppendEmpty()
- pt1.SetCount(3)
- pt1.SetSum(50)
- pt1.ExplicitBounds().FromRaw([]float64{10, 20})
- pt1.BucketCounts().FromRaw([]uint64{1, 1, 1})
- pt1.SetTimestamp(tsNanos)
- pt1.SetStartTimestamp(startTimestamp)
- pt1.Attributes().PutStr("key2", "v2")
- m1 := mL0.AppendEmpty()
- m1.SetName("hist_test2")
- hist1 := m1.SetEmptyHistogram()
- hist1.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
- pt2 := hist1.DataPoints().AppendEmpty()
- pt2.SetCount(3)
- pt2.SetSum(50)
- pt2.ExplicitBounds().FromRaw([]float64{10, 20})
- pt2.BucketCounts().FromRaw([]uint64{1, 1, 1})
- pt2.SetTimestamp(tsNanos)
- pt2.SetStartTimestamp(startTimestamp)
- pt2.Attributes().PutStr("foo", "bar")
- return []pmetric.Metrics{md0}
- },
- },
- {
- name: "unordered-buckets",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint("hist_test_bucket", 10, nil, "foo", "bar", "le", "+inf"),
- createDataPoint("hist_test_bucket", 1, nil, "foo", "bar", "le", "10"),
- createDataPoint("hist_test_bucket", 2, nil, "foo", "bar", "le", "20"),
- createDataPoint("hist_test_sum", 99, nil, "foo", "bar"),
- createDataPoint("hist_test_count", 10, nil, "foo", "bar"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- md0 := pmetric.NewMetrics()
- mL0 := md0.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m0 := mL0.AppendEmpty()
- m0.SetName("hist_test")
- hist0 := m0.SetEmptyHistogram()
- hist0.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
- pt0 := hist0.DataPoints().AppendEmpty()
- pt0.SetCount(10)
- pt0.SetSum(99)
- pt0.ExplicitBounds().FromRaw([]float64{10, 20})
- pt0.BucketCounts().FromRaw([]uint64{1, 1, 8})
- pt0.SetTimestamp(tsNanos)
- pt0.SetStartTimestamp(startTimestamp)
- pt0.Attributes().PutStr("foo", "bar")
- return []pmetric.Metrics{md0}
- },
- },
- {
- // this won't likely happen in real env, as prometheus wont generate histogram with less than 3 buckets
- name: "only-one-bucket",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint("hist_test_bucket", 3, nil, "foo", "bar", "le", "+inf"),
- createDataPoint("hist_test_count", 3, nil, "foo", "bar"),
- createDataPoint("hist_test_sum", 100, nil, "foo", "bar"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- md0 := pmetric.NewMetrics()
- mL0 := md0.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m0 := mL0.AppendEmpty()
- m0.SetName("hist_test")
- hist0 := m0.SetEmptyHistogram()
- hist0.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
- pt0 := hist0.DataPoints().AppendEmpty()
- pt0.SetCount(3)
- pt0.SetSum(100)
- pt0.BucketCounts().FromRaw([]uint64{3})
- pt0.SetTimestamp(tsNanos)
- pt0.SetStartTimestamp(startTimestamp)
- pt0.Attributes().PutStr("foo", "bar")
- return []pmetric.Metrics{md0}
- },
- },
- {
- // this won't likely happen in real env, as prometheus wont generate histogram with less than 3 buckets
- name: "only-one-bucket-noninf",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint("hist_test_bucket", 3, nil, "foo", "bar", "le", "20"),
- createDataPoint("hist_test_count", 3, nil, "foo", "bar"),
- createDataPoint("hist_test_sum", 100, nil, "foo", "bar"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- md0 := pmetric.NewMetrics()
- mL0 := md0.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m0 := mL0.AppendEmpty()
- m0.SetName("hist_test")
- hist0 := m0.SetEmptyHistogram()
- hist0.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
- pt0 := hist0.DataPoints().AppendEmpty()
- pt0.SetCount(3)
- pt0.SetSum(100)
- pt0.BucketCounts().FromRaw([]uint64{3, 0})
- pt0.ExplicitBounds().FromRaw([]float64{20})
- pt0.SetTimestamp(tsNanos)
- pt0.SetStartTimestamp(startTimestamp)
- pt0.Attributes().PutStr("foo", "bar")
- return []pmetric.Metrics{md0}
- },
- },
- {
- name: "no-sum",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint("hist_test_bucket", 1, nil, "foo", "bar", "le", "10"),
- createDataPoint("hist_test_bucket", 2, nil, "foo", "bar", "le", "20"),
- createDataPoint("hist_test_bucket", 3, nil, "foo", "bar", "le", "+inf"),
- createDataPoint("hist_test_count", 3, nil, "foo", "bar"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- md0 := pmetric.NewMetrics()
- mL0 := md0.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m0 := mL0.AppendEmpty()
- m0.SetName("hist_test")
- hist0 := m0.SetEmptyHistogram()
- hist0.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
- pt0 := hist0.DataPoints().AppendEmpty()
- pt0.SetCount(3)
- pt0.ExplicitBounds().FromRaw([]float64{10, 20})
- pt0.BucketCounts().FromRaw([]uint64{1, 1, 1})
- pt0.SetTimestamp(tsNanos)
- pt0.SetStartTimestamp(startTimestamp)
- pt0.Attributes().PutStr("foo", "bar")
- return []pmetric.Metrics{md0}
- },
- },
- {
- name: "corrupted-no-buckets",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint("hist_test_sum", 99, nil, "foo", "bar"),
- createDataPoint("hist_test_count", 10, nil, "foo", "bar"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- md0 := pmetric.NewMetrics()
- mL0 := md0.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m0 := mL0.AppendEmpty()
- m0.SetName("hist_test")
- hist0 := m0.SetEmptyHistogram()
- hist0.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
- pt0 := hist0.DataPoints().AppendEmpty()
- pt0.SetCount(10)
- pt0.SetSum(99)
- pt0.BucketCounts().FromRaw([]uint64{10})
- pt0.SetTimestamp(tsNanos)
- pt0.SetStartTimestamp(startTimestamp)
- pt0.Attributes().PutStr("foo", "bar")
- return []pmetric.Metrics{md0}
- },
- },
- {
- name: "corrupted-no-count",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint("hist_test_bucket", 1, nil, "foo", "bar", "le", "10"),
- createDataPoint("hist_test_bucket", 2, nil, "foo", "bar", "le", "20"),
- createDataPoint("hist_test_bucket", 3, nil, "foo", "bar", "le", "+inf"),
- createDataPoint("hist_test_sum", 99, nil, "foo", "bar"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- return []pmetric.Metrics{pmetric.NewMetrics()}
- },
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- tt.run(t)
- })
- }
- }
- func TestMetricBuilderSummary(t *testing.T) {
- tests := []buildTestData{
- {
- name: "no-sum-and-count",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint("summary_test", 5, nil, "foo", "bar", "quantile", "1"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- return []pmetric.Metrics{pmetric.NewMetrics()}
- },
- },
- {
- name: "no-count",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint("summary_test", 1, nil, "foo", "bar", "quantile", "0.5"),
- createDataPoint("summary_test", 2, nil, "foo", "bar", "quantile", "0.75"),
- createDataPoint("summary_test", 5, nil, "foo", "bar", "quantile", "1"),
- createDataPoint("summary_test_sum", 500, nil, "foo", "bar"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- return []pmetric.Metrics{pmetric.NewMetrics()}
- },
- },
- {
- name: "no-sum",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint("summary_test", 1, nil, "foo", "bar", "quantile", "0.5"),
- createDataPoint("summary_test", 2, nil, "foo", "bar", "quantile", "0.75"),
- createDataPoint("summary_test", 5, nil, "foo", "bar", "quantile", "1"),
- createDataPoint("summary_test_count", 500, nil, "foo", "bar"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- md0 := pmetric.NewMetrics()
- mL0 := md0.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m0 := mL0.AppendEmpty()
- m0.SetName("summary_test")
- sum0 := m0.SetEmptySummary()
- pt0 := sum0.DataPoints().AppendEmpty()
- pt0.SetTimestamp(tsNanos)
- pt0.SetStartTimestamp(startTimestamp)
- pt0.SetCount(500)
- pt0.SetSum(0.0)
- pt0.Attributes().PutStr("foo", "bar")
- qvL := pt0.QuantileValues()
- q50 := qvL.AppendEmpty()
- q50.SetQuantile(.50)
- q50.SetValue(1.0)
- q75 := qvL.AppendEmpty()
- q75.SetQuantile(.75)
- q75.SetValue(2.0)
- q100 := qvL.AppendEmpty()
- q100.SetQuantile(1)
- q100.SetValue(5.0)
- return []pmetric.Metrics{md0}
- },
- },
- {
- name: "empty-quantiles",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint("summary_test_sum", 100, nil, "foo", "bar"),
- createDataPoint("summary_test_count", 500, nil, "foo", "bar"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- md0 := pmetric.NewMetrics()
- mL0 := md0.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m0 := mL0.AppendEmpty()
- m0.SetName("summary_test")
- sum0 := m0.SetEmptySummary()
- pt0 := sum0.DataPoints().AppendEmpty()
- pt0.SetStartTimestamp(startTimestamp)
- pt0.SetTimestamp(tsNanos)
- pt0.SetCount(500)
- pt0.SetSum(100.0)
- pt0.Attributes().PutStr("foo", "bar")
- return []pmetric.Metrics{md0}
- },
- },
- {
- name: "regular-summary",
- inputs: []*testScrapedPage{
- {
- pts: []*testDataPoint{
- createDataPoint("summary_test", 1, nil, "foo", "bar", "quantile", "0.5"),
- createDataPoint("summary_test", 2, nil, "foo", "bar", "quantile", "0.75"),
- createDataPoint("summary_test", 5, nil, "foo", "bar", "quantile", "1"),
- createDataPoint("summary_test_sum", 100, nil, "foo", "bar"),
- createDataPoint("summary_test_count", 500, nil, "foo", "bar"),
- },
- },
- },
- wants: func() []pmetric.Metrics {
- md0 := pmetric.NewMetrics()
- mL0 := md0.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics()
- m0 := mL0.AppendEmpty()
- m0.SetName("summary_test")
- sum0 := m0.SetEmptySummary()
- pt0 := sum0.DataPoints().AppendEmpty()
- pt0.SetStartTimestamp(startTimestamp)
- pt0.SetTimestamp(tsNanos)
- pt0.SetCount(500)
- pt0.SetSum(100.0)
- pt0.Attributes().PutStr("foo", "bar")
- qvL := pt0.QuantileValues()
- q50 := qvL.AppendEmpty()
- q50.SetQuantile(.50)
- q50.SetValue(1.0)
- q75 := qvL.AppendEmpty()
- q75.SetQuantile(.75)
- q75.SetValue(2.0)
- q100 := qvL.AppendEmpty()
- q100.SetQuantile(1)
- q100.SetValue(5.0)
- return []pmetric.Metrics{md0}
- },
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- tt.run(t)
- })
- }
- }
- type buildTestData struct {
- name string
- inputs []*testScrapedPage
- wants func() []pmetric.Metrics
- }
- func (tt buildTestData) run(t *testing.T) {
- wants := tt.wants()
- assert.EqualValues(t, len(wants), len(tt.inputs))
- st := ts
- for i, page := range tt.inputs {
- sink := new(consumertest.MetricsSink)
- tr := newTransaction(scrapeCtx, &startTimeAdjuster{startTime: startTimestamp}, sink, nil, receivertest.NewNopCreateSettings(), nopObsRecv(t), false)
- for _, pt := range page.pts {
- // set ts for testing
- pt.t = st
- _, err := tr.Append(0, pt.lb, pt.t, pt.v)
- assert.NoError(t, err)
- for _, e := range pt.exemplars {
- _, err := tr.AppendExemplar(0, pt.lb, e)
- assert.NoError(t, err)
- }
- }
- assert.NoError(t, tr.Commit())
- mds := sink.AllMetrics()
- if wants[i].ResourceMetrics().Len() == 0 {
- // Receiver does not emit empty metrics, so will not have anything in the sink.
- require.Len(t, mds, 0)
- st += interval
- continue
- }
- require.Len(t, mds, 1)
- assertEquivalentMetrics(t, wants[i], mds[0])
- st += interval
- }
- }
- type errorAdjuster struct {
- err error
- }
- func (ea *errorAdjuster) AdjustMetrics(pmetric.Metrics) error {
- return ea.err
- }
- type startTimeAdjuster struct {
- startTime pcommon.Timestamp
- }
- func (s *startTimeAdjuster) AdjustMetrics(metrics pmetric.Metrics) error {
- for i := 0; i < metrics.ResourceMetrics().Len(); i++ {
- rm := metrics.ResourceMetrics().At(i)
- for j := 0; j < rm.ScopeMetrics().Len(); j++ {
- ilm := rm.ScopeMetrics().At(j)
- for k := 0; k < ilm.Metrics().Len(); k++ {
- metric := ilm.Metrics().At(k)
- switch metric.Type() {
- case pmetric.MetricTypeSum:
- dps := metric.Sum().DataPoints()
- for l := 0; l < dps.Len(); l++ {
- dps.At(l).SetStartTimestamp(s.startTime)
- }
- case pmetric.MetricTypeSummary:
- dps := metric.Summary().DataPoints()
- for l := 0; l < dps.Len(); l++ {
- dps.At(l).SetStartTimestamp(s.startTime)
- }
- case pmetric.MetricTypeHistogram:
- dps := metric.Histogram().DataPoints()
- for l := 0; l < dps.Len(); l++ {
- dps.At(l).SetStartTimestamp(s.startTime)
- }
- case pmetric.MetricTypeEmpty, pmetric.MetricTypeGauge, pmetric.MetricTypeExponentialHistogram:
- }
- }
- }
- }
- return nil
- }
- type testDataPoint struct {
- lb labels.Labels
- t int64
- v float64
- exemplars []exemplar.Exemplar
- }
- type testScrapedPage struct {
- pts []*testDataPoint
- }
- func createDataPoint(mname string, value float64, es []exemplar.Exemplar, tagPairs ...string) *testDataPoint {
- var lbls []string
- lbls = append(lbls, tagPairs...)
- lbls = append(lbls, model.MetricNameLabel, mname)
- lbls = append(lbls, model.JobLabel, "job")
- lbls = append(lbls, model.InstanceLabel, "instance")
- return &testDataPoint{
- lb: labels.FromStrings(lbls...),
- t: ts,
- v: value,
- exemplars: es,
- }
- }
- func assertEquivalentMetrics(t *testing.T, want, got pmetric.Metrics) {
- require.Equal(t, want.ResourceMetrics().Len(), got.ResourceMetrics().Len())
- if want.ResourceMetrics().Len() == 0 {
- return
- }
- for i := 0; i < want.ResourceMetrics().Len(); i++ {
- wantSm := want.ResourceMetrics().At(i).ScopeMetrics()
- gotSm := got.ResourceMetrics().At(i).ScopeMetrics()
- require.Equal(t, wantSm.Len(), gotSm.Len())
- if wantSm.Len() == 0 {
- return
- }
- for j := 0; j < wantSm.Len(); j++ {
- wantMs := wantSm.At(j).Metrics()
- gotMs := gotSm.At(j).Metrics()
- require.Equal(t, wantMs.Len(), gotMs.Len())
- wmap := map[string]pmetric.Metric{}
- gmap := map[string]pmetric.Metric{}
- for k := 0; k < wantMs.Len(); k++ {
- wi := wantMs.At(k)
- wmap[wi.Name()] = wi
- gi := gotMs.At(k)
- gmap[gi.Name()] = gi
- }
- assert.EqualValues(t, wmap, gmap)
- }
- }
- }
|