Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metrics: get the Resource from the Provider/server when registering a Reader #581

Merged
merged 7 commits into from
May 9, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
%% macros for metrics
%% Meters for applications are automatically created on boot

-define(current_meter, opentelemetry_experimental:get_meter(?MODULE)).
-define(current_meter, opentelemetry_experimental:get_meter(
opentelemetry:get_application_scope(?MODULE))).

-define(create_counter(Name, Opts),
otel_meter:create_counter(?current_meter, Name, Opts)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

-include_lib("kernel/include/logger.hrl").
-include("otel_meter.hrl").
-include_lib("opentelemetry_api/include/opentelemetry.hrl").

-export_type([meter/0]).

Expand Down Expand Up @@ -59,6 +60,10 @@ get_meter_(MeterProvider) ->
Meter:: meter().
get_meter('$__default_meter') ->
get_meter();
get_meter(#instrumentation_scope{name=Name,
version=Vsn,
schema_url=SchemaUrl}) ->
get_meter(Name, Vsn, SchemaUrl);
get_meter({Name, Vsn, SchemaUrl}) ->
get_meter(Name, Vsn, SchemaUrl);
get_meter(Name) ->
Expand Down
11 changes: 6 additions & 5 deletions apps/opentelemetry_experimental/src/otel_meter_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ handle_call({add_metric_reader, ReaderId, ReaderPid, DefaultAggregationMapping,
instruments_tab=InstrumentsTab,
callbacks_tab=CallbacksTab,
view_aggregations_tab=ViewAggregationsTab,
metrics_tab=MetricsTab}) ->
metrics_tab=MetricsTab,
resource=Resource}) ->
Reader = metric_reader(ReaderId,
ReaderPid,
DefaultAggregationMapping,
Expand All @@ -207,7 +208,7 @@ handle_call({add_metric_reader, ReaderId, ReaderPid, DefaultAggregationMapping,
%% matches for the new Reader
_ = update_view_aggregations(InstrumentsTab, CallbacksTab, ViewAggregationsTab, Views, Readers1),

{reply, {CallbacksTab, ViewAggregationsTab, MetricsTab}, State#state{readers=Readers1}};
{reply, {CallbacksTab, ViewAggregationsTab, MetricsTab, Resource}, State#state{readers=Readers1}};
handle_call(resource, _From, State=#state{resource=Resource}) ->
{reply, Resource, State};
handle_call({add_instrument, Instrument}, _From, State=#state{readers=Readers,
Expand Down Expand Up @@ -372,15 +373,15 @@ handle_measurement(#measurement{instrument=#instrument{meter=Meter,
value=Value,
attributes=Attributes},
ViewAggregationsTab, MetricsTab) ->
Matches = ets:lookup_element(ViewAggregationsTab, {Meter, Name}, 2),
Matches = ets:match(ViewAggregationsTab, {{Meter, Name}, '$1'}),
update_aggregations(Value, Attributes, Matches, MetricsTab).

handle_measurement(Meter, Name, Number, Attributes, ViewAggregationsTab, MetricsTab) ->
Matches = ets:lookup_element(ViewAggregationsTab, {Meter, Name}, 2),
Matches = ets:match(ViewAggregationsTab, {{Meter, Name}, '$1'}),
update_aggregations(Number, Attributes, Matches, MetricsTab).

update_aggregations(Value, Attributes, ViewAggregations, MetricsTab) ->
lists:foreach(fun(ViewAggregation=#view_aggregation{}) ->
lists:foreach(fun([ViewAggregation=#view_aggregation{}]) ->
tsloughter marked this conversation as resolved.
Show resolved Hide resolved
otel_aggregation:maybe_init_aggregate(MetricsTab,
ViewAggregation,
Value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
-module(otel_metric_exporter_console).

-export([init/1,
export/2,
export/4,
force_flush/0,
shutdown/0]).

Expand All @@ -30,7 +30,7 @@
init(_) ->
{ok, []}.

export(Metrics, _) ->
export(metrics, Metrics, _Resource, _) ->
io:format("** METRICS FOR DEBUG **~n"),
lists:map(fun(#metric{name=Name,
data=Data}) ->
Expand Down
17 changes: 9 additions & 8 deletions apps/opentelemetry_experimental/src/otel_metric_reader.erl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
callbacks_tab :: ets:table(),
view_aggregation_tab :: ets:table(),
metrics_tab :: ets:table(),
config :: #{}
config :: #{},
resource :: otel_resource:t() | undefined
}).

%% -spec start_link(atom(), map()) -> {ok, pid()} | ignore | {error, term()}.
Expand Down Expand Up @@ -99,13 +100,14 @@ handle_continue(register_with_server, State=#state{provider_sup=ProviderSup,
default_aggregation_mapping=DefaultAggregationMapping,
temporality_mapping=Temporality}) ->
ServerPid = otel_meter_server_sup:provider_pid(ProviderSup),
{CallbacksTab, ViewAggregationTab, MetricsTab} =
{CallbacksTab, ViewAggregationTab, MetricsTab, Resource} =
otel_meter_server:add_metric_reader(ServerPid, ReaderId, self(),
DefaultAggregationMapping,
Temporality),
{noreply, State#state{callbacks_tab=CallbacksTab,
view_aggregation_tab=ViewAggregationTab,
metrics_tab=MetricsTab}}.
metrics_tab=MetricsTab,
resource=Resource}}.

handle_call(shutdown, _From, State) ->
{reply, ok, State};
Expand All @@ -128,10 +130,9 @@ handle_info(collect, State=#state{id=ReaderId,
tref=undefined,
callbacks_tab=CallbacksTab,
view_aggregation_tab=ViewAggregationTab,
metrics_tab=MetricsTab
metrics_tab=MetricsTab,
resource=Resource
}) ->
Resource = [],

%% collect from view aggregations table and then export
Metrics = collect_(CallbacksTab, ViewAggregationTab, MetricsTab, ReaderId),

Expand All @@ -144,10 +145,10 @@ handle_info(collect, State=#state{id=ReaderId,
tref=TRef,
callbacks_tab=CallbacksTab,
view_aggregation_tab=ViewAggregationTab,
metrics_tab=MetricsTab
metrics_tab=MetricsTab,
resource=Resource
}) when TRef =/= undefined andalso
ExporterIntervalMs =/= undefined ->
Resource = [],
erlang:cancel_timer(TRef, [{async, true}]),
NewTRef = erlang:send_after(ExporterIntervalMs, self(), collect),

Expand Down
13 changes: 10 additions & 3 deletions apps/opentelemetry_experimental/src/otel_otlp_metrics.erl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ to_proto(#metric{name=Name,
description=Description,
unit=Unit,
data=Data}) ->
#{name => Name,
#{name => otel_otlp_common:to_binary(Name),
description => Description,
unit => otel_otlp_common:to_binary(Unit),
data => to_data(Data)}.
Expand All @@ -67,15 +67,15 @@ to_data(#sum{aggregation_temporality=Temporality,
is_monotonic=IsMonotonic,
datapoints=Datapoints}) ->
{sum, #{data_points => [to_data_points(Datapoint) || Datapoint <- Datapoints],
aggregation_temporality => Temporality,
aggregation_temporality => to_otlp_temporality(Temporality),
is_monotonic => IsMonotonic}};
to_data(#gauge{datapoints=Datapoints}) ->
{gauge, #{data_points => [to_data_points(Datapoint) || Datapoint <- Datapoints]}};
to_data(#histogram{datapoints=Datapoints,
aggregation_temporality=Temporality
}) ->
{histogram, #{data_points => [to_histogram_data_points(Datapoint) || Datapoint <- Datapoints],
aggregation_temporality => Temporality}}.
aggregation_temporality => to_otlp_temporality(Temporality)}}.

to_data_points(#datapoint{attributes=Attributes,
start_time_unix_nano=StartTimeUnixNano,
Expand Down Expand Up @@ -124,3 +124,10 @@ to_datapoint_value(Value) when is_integer(Value) ->
{as_int, Value};
to_datapoint_value(Value) when is_float(Value) ->
{as_double, Value}.

to_otlp_temporality(temporality_delta) ->
'AGGREGATION_TEMPORALITY_DELTA';
to_otlp_temporality(temporality_cumulative) ->
'AGGREGATION_TEMPORALITY_CUMULATIVE';
to_otlp_temporality(_) ->
'AGGREGATION_TEMPORALITY_UNSPECIFIED'.