/
otel_tracer_default.erl
68 lines (60 loc) · 2.91 KB
/
otel_tracer_default.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
%%%------------------------------------------------------------------------
%% Copyright 2019, OpenTelemetry Authors
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% @doc
%% @end
%%%-------------------------------------------------------------------------
-module(otel_tracer_default).
-behaviour(otel_tracer).
-export([start_span/4,
with_span/5,
b3_propagators/0,
w3c_propagators/0]).
-include_lib("opentelemetry_api/include/opentelemetry.hrl").
-include("otel_tracer.hrl").
-include("otel_span_ets.hrl").
%% @doc Starts an inactive Span and returns its SpanCtx.
-spec start_span(otel_ctx:t(), opentelemetry:tracer(), opentelemetry:span_name(),
otel_span:start_opts()) -> opentelemetry:span_ctx().
start_span(Ctx, Tracer={_, #tracer{on_start_processors=Processors,
on_end_processors=OnEndProcessors,
instrumentation_library=InstrumentationLibrary}}, Name, Opts) ->
Opts1 = maybe_set_sampler(Tracer, Opts),
SpanCtx = otel_span_ets:start_span(Ctx, Name, Opts1, Processors, InstrumentationLibrary),
SpanCtx#span_ctx{span_sdk={otel_span_ets, OnEndProcessors}}.
maybe_set_sampler(_Tracer, Opts) when is_map_key(sampler, Opts) ->
Opts;
maybe_set_sampler({_, #tracer{sampler=Sampler}}, Opts) ->
Opts#{sampler => Sampler}.
-spec with_span(otel_ctx:t(), opentelemetry:tracer(), opentelemetry:span_name(),
otel_span:start_opts(), otel_tracer:traced_fun(T)) -> T.
with_span(Ctx, Tracer, SpanName, Opts, Fun) ->
SpanCtx = start_span(Ctx, Tracer, SpanName, Opts),
Ctx1 = otel_tracer:set_current_span(Ctx, SpanCtx),
otel_ctx:attach(Ctx1),
try
Fun(SpanCtx)
after
%% passing SpanCtx directly ensures that this `end_span' ends the span started
%% in this function. If spans in `Fun()' were started and not finished properly
%% they will be abandoned and it be up to the `otel_span_sweeper' to eventually remove them.
_ = otel_span_ets:end_span(SpanCtx),
otel_ctx:attach(Ctx)
end.
-spec b3_propagators() -> {otel_propagator:text_map_extractor(), otel_propagator:text_map_injector()}.
b3_propagators() ->
otel_tracer:text_map_propagators(otel_propagator_http_b3).
-spec w3c_propagators() -> {otel_propagator:text_map_extractor(), otel_propagator:text_map_injector()}.
w3c_propagators() ->
otel_tracer:text_map_propagators(otel_propagator_http_w3c).