Skip to content

Commit bfa5a52

Browse files
author
Tristan Sloughter
authored
Merge pull request #623 from albertored/metrics-query-performance
Improve ets queries performance
2 parents 46e690c + 0a4f0f2 commit bfa5a52

File tree

5 files changed

+52
-49
lines changed

5 files changed

+52
-49
lines changed

apps/opentelemetry_experimental/include/otel_metrics.hrl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
-record(sum_aggregation,
2727
{
2828
%% TODO: attributes should be a tuple of just the values, sorted by attribute name
29-
key :: otel_aggregation:key() | match_spec(otel_aggregation:key()) | {element, 2, '$_'},
29+
key :: otel_aggregation:key() | otel_aggregation:key_match_spec() | {element, 2, '$_'},
3030
start_time_unix_nano :: integer() | match_spec(integer()),
3131
last_start_time_unix_nano :: integer() | match_spec(integer()),
3232
checkpoint :: number() | match_spec(number()) | {'+', '$2', '$3'} | {'+', '$3', '$4'},
@@ -38,7 +38,7 @@
3838
-record(last_value_aggregation,
3939
{
4040
%% TODO: attributes should be a tuple of just the values, sorted by attribute name
41-
key :: otel_aggregation:key() | match_spec(otel_aggregation:key()),
41+
key :: otel_aggregation:key() | otel_aggregation:key_match_spec(),
4242
checkpoint :: number() | match_spec(number()),
4343
value :: number() | match_spec(number()),
4444
start_time_unix_nano :: integer() | match_spec(integer()),
@@ -58,7 +58,7 @@
5858
-record(explicit_histogram_aggregation,
5959
{
6060
%% TODO: attributes should be a tuple of just the values, sorted by attribute name
61-
key :: otel_aggregation:key() | match_spec(otel_aggregation:key()),
61+
key :: otel_aggregation:key() | otel_aggregation:key_match_spec(),
6262
start_time_unix_nano :: integer() | {const, eqwalizer:dynamic()} | '$9' | '$2' | undefined,
6363
%% instrument_temporality :: otel_aggregation:temporality(),
6464
%% default: [0.0, 5.0, 10.0, 25.0, 50.0, 75.0, 100.0, 250.0, 500.0, 1000.0]

apps/opentelemetry_experimental/src/otel_aggregation.erl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
otel_aggregation_last_value:t() | otel_aggregation_histogram_explicit:t().
1313

1414
-type key() :: {atom(), opentelemetry:attributes_map(), reference()}.
15+
-type key_match_spec() :: match_spec(otel_aggregation:key()) | {match_spec(atom()), match_spec(opentelemetry:attributes_map()), match_spec(reference())}.
1516

1617
-type options() :: map().
1718

1819
-export_type([t/0,
1920
key/0,
21+
key_match_spec/0,
2022
options/0]).
2123

2224
%% Returns the aggregation's record as it is seen and updated by

apps/opentelemetry_experimental/src/otel_aggregation_histogram_explicit.erl

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ aggregate(Table, #view_aggregation{name=Name,
179179
checkpoint(Tab, #view_aggregation{name=Name,
180180
reader=ReaderId,
181181
temporality=?TEMPORALITY_DELTA}, CollectionStartNano) ->
182-
MS = [{#explicit_histogram_aggregation{key='$1',
182+
MS = [{#explicit_histogram_aggregation{key={Name, '$1', ReaderId},
183183
start_time_unix_nano='$9',
184184
explicit_bucket_boundaries='$2',
185185
record_min_max='$3',
@@ -189,9 +189,8 @@ checkpoint(Tab, #view_aggregation{name=Name,
189189
max='$7',
190190
sum='$8'
191191
},
192-
[{'=:=', {element, 1, '$1'}, {const, Name}},
193-
{'=:=', {element, 3, '$1'}, {const, ReaderId}}],
194-
[{#explicit_histogram_aggregation{key='$1',
192+
[],
193+
[{#explicit_histogram_aggregation{key={{{const, Name}, '$1', {const, ReaderId}}},
195194
start_time_unix_nano={const, CollectionStartNano},
196195
explicit_bucket_boundaries='$2',
197196
record_min_max='$3',
@@ -215,12 +214,17 @@ checkpoint(_Tab, _, _CollectionStartNano) ->
215214
ok.
216215

217216
collect(Tab, #view_aggregation{name=Name,
218-
reader=ReaderPid,
217+
reader=ReaderId,
219218
temporality=Temporality}, CollectionStartTime) ->
220-
Select = [{'$1',
221-
[{'==', Name, {element, 1, {element, 2, '$1'}}},
222-
{'==', ReaderPid, {element, 3, {element, 2, '$1'}}}],
223-
['$1']}],
219+
Select = [{#explicit_histogram_aggregation{key={Name, '$1', ReaderId},
220+
start_time_unix_nano='$2',
221+
explicit_bucket_boundaries='$3',
222+
record_min_max='$4',
223+
checkpoint='$5',
224+
bucket_counts='$6',
225+
min='$7',
226+
max='$8',
227+
sum='$9'}, [], ['$_']}],
224228
AttributesAggregation = ets:select(Tab, Select),
225229
#histogram{datapoints=[datapoint(CollectionStartTime, SumAgg) || SumAgg <- AttributesAggregation],
226230
aggregation_temporality=Temporality}.

apps/opentelemetry_experimental/src/otel_aggregation_last_value.erl

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,13 @@ aggregate(Tab, ViewAggregation=#view_aggregation{name=Name,
5757
checkpoint(Tab, #view_aggregation{name=Name,
5858
reader=ReaderId,
5959
temporality=?TEMPORALITY_DELTA}, CollectionStartNano) ->
60-
MS = [{#last_value_aggregation{key='$1',
60+
MS = [{#last_value_aggregation{key={Name, '$1', ReaderId},
6161
start_time_unix_nano='$3',
6262
last_start_time_unix_nano='_',
6363
checkpoint='_',
6464
value='$2'},
65-
[{'=:=', {element, 1, '$1'}, {const, Name}},
66-
{'=:=', {element, 3, '$1'}, {const, ReaderId}}],
67-
[{#last_value_aggregation{key='$1',
65+
[],
66+
[{#last_value_aggregation{key={{Name, '$1', {const, ReaderId}}},
6867
start_time_unix_nano={const, CollectionStartNano},
6968
last_start_time_unix_nano='$3',
7069
checkpoint='$2',
@@ -74,14 +73,13 @@ checkpoint(Tab, #view_aggregation{name=Name,
7473
ok;
7574
checkpoint(Tab, #view_aggregation{name=Name,
7675
reader=ReaderId}, _CollectionStartNano) ->
77-
MS = [{#last_value_aggregation{key='$1',
76+
MS = [{#last_value_aggregation{key={Name, '$1', ReaderId},
7877
start_time_unix_nano='$3',
7978
last_start_time_unix_nano='_',
8079
checkpoint='_',
8180
value='$2'},
82-
[{'=:=', {element, 1, '$1'}, {const, Name}},
83-
{'=:=', {element, 3, '$1'}, {const, ReaderId}}],
84-
[{#last_value_aggregation{key='$1',
81+
[],
82+
[{#last_value_aggregation{key={{{const, Name}, '$1', {const, ReaderId}}},
8583
start_time_unix_nano='$3',
8684
last_start_time_unix_nano='$3',
8785
checkpoint='$2',
@@ -92,11 +90,12 @@ checkpoint(Tab, #view_aggregation{name=Name,
9290

9391

9492
collect(Tab, #view_aggregation{name=Name,
95-
reader=ReaderPid}, CollectionStartTime) ->
96-
Select = [{'$1',
97-
[{'=:=', Name, {element, 1, {element, 2, '$1'}}},
98-
{'=:=', ReaderPid, {element, 3, {element, 2, '$1'}}}],
99-
['$1']}],
93+
reader=ReaderId}, CollectionStartTime) ->
94+
Select = [{#last_value_aggregation{key={Name, '$1', ReaderId},
95+
checkpoint='$2',
96+
value='$3',
97+
start_time_unix_nano='$4',
98+
last_start_time_unix_nano='$5'}, [], ['$_']}],
10099
AttributesAggregation = ets:select(Tab, Select),
101100
#gauge{datapoints=[datapoint(CollectionStartTime, LastValueAgg) ||
102101
LastValueAgg <- AttributesAggregation]}.

apps/opentelemetry_experimental/src/otel_aggregation_sum.erl

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -91,35 +91,32 @@ aggregate(_Tab, #view_aggregation{name=_Name,
9191

9292
-dialyzer({nowarn_function, checkpoint/3}).
9393
checkpoint(Tab, #view_aggregation{name=Name,
94-
reader=ReaderPid,
94+
reader=ReaderId,
9595
temporality=?TEMPORALITY_DELTA}, CollectionStartNano) ->
96-
MS = [{#sum_aggregation{key='$1',
96+
MS = [{#sum_aggregation{key={Name, '$1', ReaderId},
9797
start_time_unix_nano='$4',
9898
last_start_time_unix_nano='_',
9999
checkpoint='$5',
100100
previous_checkpoint='_',
101101
int_value='$2',
102102
float_value='$3'},
103-
[{'=:=', {element, 1, '$1'}, {const, Name}},
104-
{'=:=', {element, 3, '$1'}, {const, ReaderPid}},
105-
{'=:=', '$3', {const, 0.0}}],
106-
[{#sum_aggregation{key='$1',
103+
[{'=:=', '$3', {const, 0.0}}],
104+
[{#sum_aggregation{key={{Name, '$1', {const, ReaderId}}},
107105
start_time_unix_nano={const, CollectionStartNano},
108106
last_start_time_unix_nano='$4',
109107
checkpoint='$2',
110108
previous_checkpoint='$5',
111109
int_value=0,
112110
float_value=0.0}}]},
113-
{#sum_aggregation{key='$1',
111+
{#sum_aggregation{key={Name, '$1', ReaderId},
114112
start_time_unix_nano='$4',
115113
last_start_time_unix_nano='_',
116114
checkpoint='$5',
117115
previous_checkpoint='_',
118116
int_value='$2',
119117
float_value='$3'},
120-
[{'=:=', {element, 1, '$1'}, {const, Name}},
121-
{'=:=', {element, 3, '$1'}, {const, ReaderPid}}],
122-
[{#sum_aggregation{key='$1',
118+
[],
119+
[{#sum_aggregation{key={{Name, '$1', {const, ReaderId}}},
123120
start_time_unix_nano={const, CollectionStartNano},
124121
last_start_time_unix_nano='$4',
125122
checkpoint={'+', '$2', '$3'},
@@ -129,35 +126,32 @@ checkpoint(Tab, #view_aggregation{name=Name,
129126
_ = ets:select_replace(Tab, MS),
130127
ok;
131128
checkpoint(Tab, #view_aggregation{name=Name,
132-
reader=ReaderPid,
129+
reader=ReaderId,
133130
temporality=?TEMPORALITY_CUMULATIVE}, _CollectionStartNano) ->
134-
MS = [{#sum_aggregation{key='$1',
131+
MS = [{#sum_aggregation{key={Name, '$1', ReaderId},
135132
start_time_unix_nano='$2',
136133
last_start_time_unix_nano='_',
137134
checkpoint='$5',
138135
previous_checkpoint='$6',
139136
int_value='$3',
140137
float_value='$4'},
141-
[{'=:=', {element, 1, '$1'}, {const, Name}},
142-
{'=:=', {element, 3, '$1'}, {const, ReaderPid}},
143-
{'=:=', '$4', {const, 0.0}}],
144-
[{#sum_aggregation{key='$1',
138+
[{'=:=', '$4', {const, 0.0}}],
139+
[{#sum_aggregation{key={{Name, '$1', {const, ReaderId}}},
145140
start_time_unix_nano='$2',
146141
last_start_time_unix_nano='$2',
147142
checkpoint='$3',
148143
previous_checkpoint={'+', '$5', '$6'},
149144
int_value=0,
150145
float_value=0.0}}]},
151-
{#sum_aggregation{key='$1',
146+
{#sum_aggregation{key={Name, '$1', ReaderId},
152147
start_time_unix_nano='$2',
153148
last_start_time_unix_nano='_',
154149
checkpoint='$5',
155150
previous_checkpoint='$6',
156151
int_value='$3',
157152
float_value='$4'},
158-
[{'=:=', {element, 1, '$1'}, {const, Name}},
159-
{'=:=', {element, 3, '$1'}, {const, ReaderPid}}],
160-
[{#sum_aggregation{key='$1',
153+
[],
154+
[{#sum_aggregation{key={{Name, '$1', {const, ReaderId}}},
161155
start_time_unix_nano='$2',
162156
last_start_time_unix_nano='$2',
163157
checkpoint={'+', '$3', '$4'},
@@ -172,10 +166,14 @@ collect(Tab, #view_aggregation{name=Name,
172166
instrument=#instrument{temporality=InstrumentTemporality},
173167
temporality=Temporality,
174168
is_monotonic=IsMonotonic}, CollectionStartTime) ->
175-
Select = [{'$1',
176-
[{'=:=', Name, {element, 1, {element, 2, '$1'}}},
177-
{'=:=', ReaderId, {element, 3, {element, 2, '$1'}}}],
178-
['$1']}],
169+
170+
Select = [{#sum_aggregation{key={Name, '$1', ReaderId},
171+
start_time_unix_nano='$2',
172+
last_start_time_unix_nano='$3',
173+
checkpoint='$4',
174+
previous_checkpoint='$5',
175+
int_value='$6',
176+
float_value='$7'}, [], ['$_']}],
179177
AttributesAggregation = ets:select(Tab, Select),
180178
#sum{aggregation_temporality=Temporality,
181179
is_monotonic=IsMonotonic,

0 commit comments

Comments
 (0)