Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/erlang-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
run:
name: Run checks
needs: setup
uses: valitydev/erlang-workflows/.github/workflows/erlang-parallel-build.yml@v1.0.10
uses: valitydev/erlang-workflows/.github/workflows/erlang-parallel-build.yml@v1.0.14
with:
otp-version: ${{ needs.setup.outputs.otp-version }}
rebar-version: ${{ needs.setup.outputs.rebar-version }}
Expand Down
8 changes: 4 additions & 4 deletions src/woody_event_handler_otel.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@

%% Client events
handle_event(Event, RpcID, _Meta = #{url := Url}, _Opts) when ?IS_CLIENT_INTERNAL(Event) ->
with_span(otel_ctx:get_current(), mk_ref(RpcID), fun(SpanCtx) ->
_ = otel_span:add_event(SpanCtx, atom_to_binary(Event), #{url => Url})
with_existing_span(otel_ctx:get_current(), mk_ref(RpcID), fun(SpanCtx) ->
_ = otel_span:add_event(SpanCtx, woody_util:normalize_event_name(Event), #{url => Url})
end);
%% Internal error handling
handle_event(?EV_INTERNAL_ERROR, RpcID, Meta = #{error := Error, class := Class, reason := Reason}, _Opts) ->
Stacktrace = maps:get(stack, Meta, []),
Details = io_lib:format("~ts: ~ts", [Error, Reason]),
with_span(otel_ctx:get_current(), mk_ref(RpcID), fun(SpanCtx) ->
with_existing_span(otel_ctx:get_current(), mk_ref(RpcID), fun(SpanCtx) ->
_ = otel_span:record_exception(SpanCtx, genlib:define(Class, error), Details, Stacktrace, #{}),
otel_maybe_cleanup(Meta, SpanCtx)
end);
Expand Down Expand Up @@ -68,7 +68,7 @@ span_end(Ctx, Key, OnBeforeEnd) ->
ok
end.

with_span(Ctx, Key, F) ->
with_existing_span(Ctx, Key, F) ->
SpanCtx = woody_util:span_stack_get(Key, Ctx, otel_tracer:current_span_ctx(Ctx)),
_ = F(SpanCtx),
ok.
Expand Down
24 changes: 24 additions & 0 deletions src/woody_hackney_prometheus.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
-define(NB_REQUESTS, hackney_nb_requests).
-define(TOTAL_REQUESTS, hackney_total_requests).
-define(HOST_NB_REQUESTS, hackney_host_nb_requests).
-define(HOST_REQUEST_TIME, hackney_host_request_time).
-define(HOST_CONNECT_TIMEOUT, hackney_host_connect_timeout).
-define(HOST_CONNECT_ERROR, hackney_host_connect_error).
-define(HOST_NEW_CONNECTION, hackney_host_new_connection).
Expand All @@ -50,6 +51,13 @@ new(counter, [hackney, nb_requests]) ->
{labels, [host]},
{help, "Number of running requests."}
]),
true = prometheus_histogram:declare([
{name, ?HOST_REQUEST_TIME},
{registry, registry()},
{labels, [host]},
{buckets, request_time_buckets_ms()},
{help, "Request time."}
]),
[
true = prometheus_counter:declare([
{name, Name},
Expand Down Expand Up @@ -117,6 +125,9 @@ decrement_counter(_Name, _Value) ->
-spec update_histogram(name(), fun(() -> ok) | number()) -> ok.
update_histogram(_Name, Fun) when is_function(Fun, 0) ->
Fun();
update_histogram([hackney, Host, request_time], Value) ->
_ = prometheus_histogram:observe(registry(), ?HOST_REQUEST_TIME, [Host], Value),
ok;
update_histogram(_Name, _Value) ->
ok.

Expand All @@ -132,3 +143,16 @@ update_meter(_Name, _Value) ->

registry() ->
default.

request_time_buckets_ms() ->
[
5,
10,
25,
50,
100,
250,
500,
1000,
10000
].
35 changes: 35 additions & 0 deletions src/woody_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
-export([span_stack_put/3]).
-export([span_stack_get/3]).
-export([span_stack_pop/2]).
-export([normalize_event_name/1]).

%%
%% Internal API
Expand Down Expand Up @@ -95,3 +96,37 @@ span_stack_pop(Key, Context) ->
Context1 = otel_ctx:set_value(Context, ?OTEL_SPANS_STACK, Stack1),
{ok, SpanCtx, ParentSpanCtx, Context1}
end.

%% TODO Normalize according spec:
%% https://github.com/open-telemetry/semantic-conventions/blob/main/docs/general/events.md
%%
%% Specifically this:
%%
%% Semantic conventions MUST limit names to printable Basic Latin
%% characters (more precisely to U+0021 .. U+007E subset of Unicode
%% code points). It is recommended to further limit names to the
%% following Unicode code points: Latin alphabet, Numeric, Underscore,
%% Dot (as namespace delimiter).
-define(EVENT_NAME_FORBIDDEN_CHARS, [<<" ">>]).

-spec normalize_event_name(binary() | atom()) -> opentelemetry:event_name().
normalize_event_name(Event) when is_atom(Event) ->
normalize_event_name(atom_to_binary(Event, unicode));
normalize_event_name(Event) when is_binary(Event) ->
binary:replace(Event, ?EVENT_NAME_FORBIDDEN_CHARS, <<$_>>, [global]).

-ifdef(TEST).

-include_lib("eunit/include/eunit.hrl").

-spec test() -> _.

-spec normalize_event_name_test_() -> _.
normalize_event_name_test_() ->
[
?_assertEqual(<<"client_resolve_begin">>, normalize_event_name('client resolve begin')),
?_assertEqual(<<"client_resolve/begin">>, normalize_event_name('client resolve/begin')),
?_assertEqual(<<"client.resolve">>, normalize_event_name('client.resolve'))
].

-endif.