1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
4 #ifdef ENABLE_METRICS_PREVIEW
5 # include <gtest/gtest.h>
6 # include "opentelemetry/_metrics/instrument.h"
7 # include "opentelemetry/_metrics/noop.h"
8 # include "opentelemetry/_metrics/observer_result.h"
9 # include "opentelemetry/_metrics/sync_instruments.h"
14 OPENTELEMETRY_BEGIN_NAMESPACE
16 using opentelemetry::metrics::Meter
;
17 using opentelemetry::metrics::NoopMeter
;
19 void Callback(opentelemetry::metrics::ObserverResult
<int> result
)
21 std::map
<std::string
, std::string
> labels
= {{"key", "value"}};
22 auto labelkv
= common::KeyValueIterableView
<decltype(labels
)>{labels
};
23 result
.observe(1, labelkv
);
26 TEST(NoopTest
, CreateInstruments
)
28 auto m
= std::unique_ptr
<Meter
>(new NoopMeter
{});
30 // Test instrument constructors
31 m
->NewIntCounter("Test counter", "For testing", "Unitless", true);
32 m
->NewIntUpDownCounter("Test ud counter", "For testing", "Unitless", true);
33 m
->NewIntValueRecorder("Test recorder", "For testing", "Unitless", true);
35 m
->NewIntSumObserver("Test sum obs", "For testing", "Unitless", true, &Callback
);
36 m
->NewIntUpDownSumObserver("Test udsum obs", "For testing", "Unitless", true, &Callback
);
37 m
->NewIntValueObserver("Test val obs", "For testing", "Unitless", true, &Callback
);
40 TEST(NoopMeter
, RecordBatch
)
42 // Test BatchRecord with all supported types
43 // Create Counter and call RecordBatch for all four supported types: short, int, float, and double
45 std::unique_ptr
<Meter
> m
{std::unique_ptr
<Meter
>(new NoopMeter
{})};
47 std::map
<std::string
, std::string
> labels
= {{"Key", "Value"}};
48 auto labelkv
= opentelemetry::common::KeyValueIterableView
<decltype(labels
)>{labels
};
50 auto s
= m
->NewShortCounter("Test short counter", "For testing", "Unitless", true);
52 std::array
<metrics::SynchronousInstrument
<short> *, 1> siarr
{s
.get()};
53 std::array
<short, 1> svarr
{1};
54 nostd::span
<metrics::SynchronousInstrument
<short> *> ssp
{siarr
};
55 nostd::span
<short> sval
{svarr
};
56 m
->RecordShortBatch(labelkv
, ssp
, sval
);
58 auto i
= m
->NewIntCounter("Test int counter", "For testing", "Unitless", true);
60 std::array
<metrics::SynchronousInstrument
<int> *, 1> iiarr
{i
.get()};
61 std::array
<int, 1> ivarr
{1};
62 nostd::span
<metrics::SynchronousInstrument
<int> *> isp
{iiarr
};
63 nostd::span
<int> ival
{ivarr
};
64 m
->RecordIntBatch(labelkv
, isp
, ival
);
66 auto f
= m
->NewFloatCounter("Test int counter", "For testing", "Unitless", true);
68 std::array
<metrics::SynchronousInstrument
<float> *, 1> fiarr
{f
.get()};
69 std::array
<float, 1> fvarr
{1.0f
};
70 nostd::span
<metrics::SynchronousInstrument
<float> *> fsp
{fiarr
};
71 nostd::span
<float> fval
{fvarr
};
72 m
->RecordFloatBatch(labelkv
, fsp
, fval
);
74 auto d
= m
->NewDoubleCounter("Test int counter", "For testing", "Unitless", true);
76 std::array
<metrics::SynchronousInstrument
<double> *, 1> diarr
{d
.get()};
77 std::array
<double, 1> dvarr
{1.0f
};
78 nostd::span
<metrics::SynchronousInstrument
<double> *> dsp
{diarr
};
79 nostd::span
<double> dval
{dvarr
};
80 m
->RecordDoubleBatch(labelkv
, dsp
, dval
);
82 OPENTELEMETRY_END_NAMESPACE