Skip to content

Commit 6d08186

Browse files
author
Tristan Sloughter
authored
Merge pull request #613 from albertored/temporality
Fix temporality handling
2 parents efc87f3 + 1837af9 commit 6d08186

File tree

10 files changed

+60
-51
lines changed

10 files changed

+60
-51
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3232
### Fixes
3333

3434
- [Correctly record histogram values greater than last boundary](https://github.com/open-telemetry/opentelemetry-erlang/pull/614)
35+
- [Readers should use a default cumulative temporality if not specified](https://github.com/open-telemetry/opentelemetry-erlang/pull/613)
3536

3637
## SDK 1.3.1 - 2023-08-15
3738

apps/opentelemetry_api_experimental/include/otel_metrics.hrl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
-define(TEMPORALITY_DELTA, temporality_delta).
1212
-define(TEMPORALITY_CUMULATIVE, temporality_cumulative).
13-
-define(TEMPORALITY_UNSPECIFIED, temporality_unspecified).
1413

1514
-define(KIND_COUNTER, counter).
1615
-define(KIND_OBSERVABLE_COUNTER, observable_counter).

apps/opentelemetry_api_experimental/src/otel_instrument.erl

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
-export([new/6,
2121
new/8,
22-
is_monotonic/1]).
22+
is_monotonic/1,
23+
temporality/1]).
2324

2425
-include("otel_metrics.hrl").
2526

@@ -35,9 +36,7 @@
3536
[named_observations()].
3637
-type callback() :: fun((callback_args()) -> callback_result()).
3738

38-
-type temporality() :: ?TEMPORALITY_UNSPECIFIED |
39-
?TEMPORALITY_DELTA |
40-
?TEMPORALITY_CUMULATIVE.
39+
-type temporality() :: ?TEMPORALITY_DELTA | ?TEMPORALITY_CUMULATIVE.
4140

4241
-type t() :: #instrument{}.
4342

@@ -81,3 +80,16 @@ is_monotonic(#instrument{kind=?KIND_HISTOGRAM}) ->
8180
true;
8281
is_monotonic(_) ->
8382
false.
83+
84+
temporality(#instrument{kind=?KIND_COUNTER}) ->
85+
?TEMPORALITY_DELTA;
86+
temporality(#instrument{kind=?KIND_OBSERVABLE_COUNTER}) ->
87+
?TEMPORALITY_CUMULATIVE;
88+
temporality(#instrument{kind=?KIND_UPDOWN_COUNTER}) ->
89+
?TEMPORALITY_DELTA;
90+
temporality(#instrument{kind=?KIND_OBSERVABLE_UPDOWNCOUNTER}) ->
91+
?TEMPORALITY_CUMULATIVE;
92+
temporality(#instrument{kind=?KIND_HISTOGRAM}) ->
93+
?TEMPORALITY_DELTA;
94+
temporality(#instrument{kind=?KIND_OBSERVABLE_GAUGE}) ->
95+
?TEMPORALITY_CUMULATIVE.

apps/opentelemetry_experimental/src/otel_aggregation.erl

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
-module(otel_aggregation).
22

33
-export([maybe_init_aggregate/4,
4-
default_mapping/0,
5-
temporality_mapping/0,
6-
instrument_temporality/1]).
4+
default_mapping/0]).
75

86
-include_lib("opentelemetry_api_experimental/include/otel_metrics.hrl").
97
-include("otel_metrics.hrl").
@@ -71,25 +69,4 @@ default_mapping() ->
7169
?KIND_HISTOGRAM => otel_aggregation_histogram_explicit,
7270
?KIND_OBSERVABLE_GAUGE => otel_aggregation_last_value,
7371
?KIND_UPDOWN_COUNTER => otel_aggregation_sum,
74-
?KIND_OBSERVABLE_UPDOWNCOUNTER => otel_aggregation_sum}.
75-
76-
temporality_mapping() ->
77-
#{?KIND_COUNTER =>?TEMPORALITY_DELTA,
78-
?KIND_OBSERVABLE_COUNTER => ?TEMPORALITY_CUMULATIVE,
79-
?KIND_UPDOWN_COUNTER => ?TEMPORALITY_DELTA,
80-
?KIND_OBSERVABLE_UPDOWNCOUNTER => ?TEMPORALITY_CUMULATIVE,
81-
?KIND_HISTOGRAM => ?TEMPORALITY_UNSPECIFIED,
82-
?KIND_OBSERVABLE_GAUGE => ?TEMPORALITY_UNSPECIFIED}.
83-
84-
instrument_temporality(#instrument{kind=?KIND_COUNTER}) ->
85-
?TEMPORALITY_DELTA;
86-
instrument_temporality(#instrument{kind=?KIND_OBSERVABLE_COUNTER}) ->
87-
?TEMPORALITY_CUMULATIVE;
88-
instrument_temporality(#instrument{kind=?KIND_UPDOWN_COUNTER}) ->
89-
?TEMPORALITY_DELTA;
90-
instrument_temporality(#instrument{kind=?KIND_OBSERVABLE_UPDOWNCOUNTER}) ->
91-
?TEMPORALITY_CUMULATIVE;
92-
instrument_temporality(#instrument{kind=?KIND_HISTOGRAM}) ->
93-
?TEMPORALITY_UNSPECIFIED;
94-
instrument_temporality(#instrument{kind=?KIND_OBSERVABLE_GAUGE}) ->
95-
?TEMPORALITY_UNSPECIFIED.
72+
?KIND_OBSERVABLE_UPDOWNCOUNTER => otel_aggregation_sum}.

apps/opentelemetry_experimental/src/otel_meter_server.erl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,14 +342,12 @@ metric_reader(ReaderId, ReaderPid, DefaultAggregationMapping, Temporality) ->
342342

343343
ReaderAggregationMapping = maps:merge(otel_aggregation:default_mapping(),
344344
DefaultAggregationMapping),
345-
ReaderTemporalityMapping = maps:merge(otel_aggregation:temporality_mapping(),
346-
Temporality),
347345

348346
#reader{id=ReaderId,
349347
pid=ReaderPid,
350348
monitor_ref=Ref,
351349
default_aggregation_mapping=ReaderAggregationMapping,
352-
default_temporality_mapping=ReaderTemporalityMapping}.
350+
default_temporality_mapping=Temporality}.
353351

354352

355353
%% a Measurement's Instrument is matched against Views
@@ -388,7 +386,7 @@ view_aggregation_for_reader(Instrument=#instrument{kind=Kind}, ViewAggregation,
388386
Reader=#reader{id=Id,
389387
default_temporality_mapping=ReaderTemporalityMapping}) ->
390388
AggregationModule = aggregation_module(Instrument, View, Reader),
391-
Temporality = maps:get(Kind, ReaderTemporalityMapping, ?TEMPORALITY_UNSPECIFIED),
389+
Temporality = maps:get(Kind, ReaderTemporalityMapping, ?TEMPORALITY_CUMULATIVE),
392390

393391
ViewAggregation#view_aggregation{
394392
reader=Id,
@@ -400,7 +398,7 @@ view_aggregation_for_reader(Instrument=#instrument{kind=Kind}, ViewAggregation,
400398
Reader=#reader{id=Id,
401399
default_temporality_mapping=ReaderTemporalityMapping}) ->
402400
AggregationModule = aggregation_module(Instrument, View, Reader),
403-
Temporality = maps:get(Kind, ReaderTemporalityMapping, ?TEMPORALITY_UNSPECIFIED),
401+
Temporality = maps:get(Kind, ReaderTemporalityMapping, ?TEMPORALITY_CUMULATIVE),
404402

405403
ViewAggregation#view_aggregation{
406404
reader=Id,

apps/opentelemetry_experimental/src/otel_otlp_metrics.erl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
-include_lib("kernel/include/logger.hrl").
2323
-include_lib("opentelemetry_api/include/opentelemetry.hrl").
24+
-include_lib("opentelemetry_api_experimental/include/otel_metrics.hrl").
2425
-include_lib("opentelemetry/include/otel_span.hrl").
2526
-include("otel_metrics.hrl").
2627

@@ -125,9 +126,7 @@ to_datapoint_value(Value) when is_integer(Value) ->
125126
to_datapoint_value(Value) when is_float(Value) ->
126127
{as_double, Value}.
127128

128-
to_otlp_temporality(temporality_delta) ->
129+
to_otlp_temporality(?TEMPORALITY_DELTA) ->
129130
'AGGREGATION_TEMPORALITY_DELTA';
130-
to_otlp_temporality(temporality_cumulative) ->
131-
'AGGREGATION_TEMPORALITY_CUMULATIVE';
132-
to_otlp_temporality(_) ->
133-
'AGGREGATION_TEMPORALITY_UNSPECIFIED'.
131+
to_otlp_temporality(?TEMPORALITY_CUMULATIVE) ->
132+
'AGGREGATION_TEMPORALITY_CUMULATIVE'.

apps/opentelemetry_experimental/src/otel_view.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ match_instrument_to_views(Instrument=#instrument{name=InstrumentName,
8080
meter=Meter,
8181
description=Description}, Views) ->
8282
IsMonotonic = otel_instrument:is_monotonic(Instrument),
83-
Temporality = otel_aggregation:instrument_temporality(Instrument),
83+
Temporality = otel_instrument:temporality(Instrument),
8484
Scope = otel_meter:scope(Meter),
8585
case lists:filtermap(fun(View=#view{name=ViewName,
8686
description=ViewDescription,

apps/opentelemetry_experimental/test/otel_metrics_SUITE.erl

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ init_per_testcase(provider_test, Config) ->
9797
application:load(opentelemetry_experimental),
9898
ok = application:set_env(opentelemetry_experimental, readers,
9999
[#{module => otel_metric_reader,
100-
config => #{exporter => {otel_metric_exporter_pid, self()}}}]),
100+
config => #{exporter => {otel_metric_exporter_pid, self()},
101+
default_temporality_mapping => default_temporality_mapping()}}]),
101102
ok = application:set_env(opentelemetry_experimental, views,
102103
[#{selector => #{instrument_name => a_counter},
103104
aggregation_module => otel_aggregation_sum},
@@ -117,9 +118,11 @@ init_per_testcase(multiple_readers, Config) ->
117118
application:load(opentelemetry_experimental),
118119
ok = application:set_env(opentelemetry_experimental, readers,
119120
[#{module => otel_metric_reader,
120-
config => #{exporter => {otel_metric_exporter_pid, self()}}},
121+
config => #{exporter => {otel_metric_exporter_pid, self()},
122+
default_temporality_mapping => default_temporality_mapping()}},
121123
#{module => otel_metric_reader,
122124
config => #{exporter => {otel_metric_exporter_pid, self()},
125+
default_temporality_mapping => default_temporality_mapping(),
123126
default_aggregation_mapping =>
124127
#{?KIND_COUNTER => otel_aggregation_drop}}}]),
125128

@@ -132,13 +135,14 @@ init_per_testcase(delta_counter, Config) ->
132135
%% delta is the default for a counter with sum aggregation
133136
%% so no need to set any temporality mapping in the reader
134137
ok = application:set_env(opentelemetry_experimental, readers, [#{module => otel_metric_reader,
135-
config => #{exporter => {otel_metric_exporter_pid, self()}}}]),
138+
config => #{exporter => {otel_metric_exporter_pid, self()},
139+
default_temporality_mapping => default_temporality_mapping()}}]),
136140

137141
{ok, _} = application:ensure_all_started(opentelemetry_experimental),
138142

139143
Config;
140144
init_per_testcase(cumulative_counter, Config) ->
141-
CumulativeCounterTemporality = #{?KIND_COUNTER =>?TEMPORALITY_CUMULATIVE},
145+
CumulativeCounterTemporality = maps:put(?KIND_COUNTER, ?TEMPORALITY_CUMULATIVE, default_temporality_mapping()),
142146
application:load(opentelemetry_experimental),
143147
ok = application:set_env(opentelemetry_experimental, readers, [#{module => otel_metric_reader,
144148
config => #{exporter => {otel_metric_exporter_pid, self()},
@@ -149,7 +153,7 @@ init_per_testcase(cumulative_counter, Config) ->
149153

150154
Config;
151155
init_per_testcase(delta_explicit_histograms, Config) ->
152-
DeltaHistogramTemporality = #{?KIND_HISTOGRAM =>?TEMPORALITY_DELTA},
156+
DeltaHistogramTemporality = maps:put(?KIND_HISTOGRAM, ?TEMPORALITY_DELTA, default_temporality_mapping()),
153157
application:load(opentelemetry_experimental),
154158
ok = application:set_env(opentelemetry_experimental, readers, [#{module => otel_metric_reader,
155159
config => #{exporter => {otel_metric_exporter_pid, self()},
@@ -160,7 +164,7 @@ init_per_testcase(delta_explicit_histograms, Config) ->
160164

161165
Config;
162166
init_per_testcase(delta_observable_counter, Config) ->
163-
DeltaObservableCounterTemporality = #{?KIND_OBSERVABLE_COUNTER =>?TEMPORALITY_DELTA},
167+
DeltaObservableCounterTemporality = maps:put(?KIND_OBSERVABLE_COUNTER, ?TEMPORALITY_DELTA, default_temporality_mapping()),
164168
application:load(opentelemetry_experimental),
165169
ok = application:set_env(opentelemetry_experimental, readers, [#{module => otel_metric_reader,
166170
config => #{exporter => {otel_metric_exporter_pid, self()},
@@ -173,7 +177,8 @@ init_per_testcase(delta_observable_counter, Config) ->
173177
init_per_testcase(_, Config) ->
174178
application:load(opentelemetry_experimental),
175179
ok = application:set_env(opentelemetry_experimental, readers, [#{module => otel_metric_reader,
176-
config => #{exporter => {otel_metric_exporter_pid, self()}}}]),
180+
config => #{exporter => {otel_metric_exporter_pid, self()},
181+
default_temporality_mapping => default_temporality_mapping()}}]),
177182

178183
{ok, _} = application:ensure_all_started(opentelemetry_experimental),
179184

@@ -193,6 +198,16 @@ default_resource(_Config) ->
193198

194199
ok.
195200

201+
default_temporality_mapping() ->
202+
#{
203+
?KIND_COUNTER => ?TEMPORALITY_DELTA,
204+
?KIND_OBSERVABLE_COUNTER => ?TEMPORALITY_CUMULATIVE,
205+
?KIND_UPDOWN_COUNTER => ?TEMPORALITY_DELTA,
206+
?KIND_OBSERVABLE_UPDOWNCOUNTER => ?TEMPORALITY_CUMULATIVE,
207+
?KIND_HISTOGRAM => ?TEMPORALITY_DELTA,
208+
?KIND_OBSERVABLE_GAUGE => ?TEMPORALITY_CUMULATIVE
209+
}.
210+
196211
using_macros(_Config) ->
197212
DefaultMeter = otel_meter_default,
198213

apps/opentelemetry_exporter/test/opentelemetry_exporter_SUITE.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ verify_metrics_export(Config) ->
7676
name = <<"sum name">>,
7777
description = <<"some sum description">>,
7878
unit = kb,
79-
data = #sum{aggregation_temporality = 'AGGREGATION_TEMPORALITY_CUMULATIVE',
79+
data = #sum{aggregation_temporality = temporality_cumulative,
8080
is_monotonic=true,
8181
datapoints=[#datapoint{
8282
attributes=otel_attributes:new(#{<<"key-1">> => <<"value-1">>},
@@ -126,7 +126,7 @@ verify_metrics_export(Config) ->
126126
name = <<"histogram name">>,
127127
description = <<"some histogram description">>,
128128
unit = kb,
129-
data = #histogram{aggregation_temporality = 'AGGREGATION_TEMPORALITY_CUMULATIVE',
129+
data = #histogram{aggregation_temporality = temporality_cumulative,
130130
datapoints=[#histogram_datapoint{
131131
attributes=otel_attributes:new(#{<<"key-1">> => <<"value-1">>},
132132
128, 128),

test/otel_metric_tests.exs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,15 @@ defmodule OtelMetricTests do
2828
%{
2929
module: :otel_metric_reader,
3030
config: %{
31-
exporter: {:otel_metric_exporter_pid, {:metric, self()}}
31+
exporter: {:otel_metric_exporter_pid, {:metric, self()}},
32+
default_temporality_mapping: %{
33+
counter: :temporality_delta,
34+
observable_counter: :temporality_cumulative,
35+
updown_counter: :temporality_delta,
36+
observable_updowncounter: :temporality_cumulative,
37+
histogram: :temporality_delta,
38+
observable_gauge: :temporality_cumulative
39+
}
3240
}
3341
}
3442
])

0 commit comments

Comments
 (0)