Skip to content

Commit

Permalink
feat: add opentelemetry (#4)
Browse files Browse the repository at this point in the history
also remove some stdlib2-use
  • Loading branch information
chsukivra authored Jun 18, 2024
1 parent 1f3a03e commit a4fe42b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
6 changes: 1 addition & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
deps/
.eunit/
*.beam
*.app
*.plt
_build
1 change: 1 addition & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{cover_print_enabled, true}.
{deps,
[ {stdlib2, ".*", {git, "https://github.com/kivra/stdlib2", {branch, "master"}}}
, {opentelemetry_api, "~> 1.0"}
, {eon, ".*", {git, "https://github.com/kivra/eon", {branch, "master"}}}
]}.

Expand Down
2 changes: 1 addition & 1 deletion src/greph.app.src
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{application, greph, [ {description, "greph"}
, {vsn, git}
, {applications, [kernel, stdlib]}
, {applications, [kernel, stdlib, eon, stdlib2, opentelemetry_api]}
, {env, []}
, {registered, []}
]}.
Expand Down
40 changes: 32 additions & 8 deletions src/greph.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
-export_type([]).

%%%_* Includes =========================================================
-include_lib("kernel/include/logger.hrl").
-include_lib("opentelemetry_api/include/opentelemetry.hrl").
-include_lib("opentelemetry_api/include/otel_tracer.hrl").
-include("greph.hrl").

%%%_* Code =============================================================
Expand Down Expand Up @@ -150,18 +153,39 @@ get_args(Obj, As) ->
{ok, Res} ->
Res;
{error, notfound} = Err ->
?info("missing ~p", [A]),
?LOG_INFO(#{message => greph_args_missing, argname => A}),
throw(Err)
end || A <- As].

eval(Label, F, Args) ->
case ?lift(?time([time, label, Label], call(F, Args))) of
{ok, Res} ->
Res;
{error, Rsn} = Err ->
?info("~p: failed with ~p", [Label, Rsn]),
throw(Err)
end.
SpanName = case Label of
_ when is_atom(Label) ->
BinLabel = atom_to_binary(Label),
<<"greph eval ", BinLabel/binary>>;
_ ->
<<"greph eval">>
end,
SpanOpts = #{
attributes => #{},
links => [],
is_recording => true,
start_time => opentelemetry:timestamp(),
kind => ?SPAN_KIND_INTERNAL
},
?with_span(SpanName, SpanOpts,
fun(_SpanCtx) ->
case
?lift(call(F, Args))
of
{ok, Res} ->
?set_status(?OTEL_STATUS_OK),
Res;
{error, Rsn} = Err ->
?set_status(?OTEL_STATUS_ERROR),
?LOG_INFO(#{message => greph_fail, label => Label, reason => Rsn}),
throw(Err)
end
end).

call(F, A) when is_function(F) -> apply(F, A);
call({M, F}, A) -> apply(M, F, A);
Expand Down

0 comments on commit a4fe42b

Please sign in to comment.