Skip to content

Commit

Permalink
improve type specs in propagator modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan Sloughter committed Sep 24, 2021
1 parent f772491 commit 45784c3
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 11 deletions.
11 changes: 11 additions & 0 deletions apps/opentelemetry_api/src/otel_propagator_b3multi.erl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
fields(_) ->
[?B3_TRACE_ID, ?B3_SPAN_ID, ?B3_SAMPLED].

-spec inject(Context, Carrier, CarrierSetFun, Options) -> Carrier
when Context :: otel_ctx:t(),
Carrier :: otel_propagator:carrier(),
CarrierSetFun :: otel_propagator_text_map:carrier_set(),
Options :: otel_propagator_text_map:propagator_options().
inject(Ctx, Carrier, CarrierSet, _Options) ->
case otel_tracer:current_span_ctx(Ctx) of
#span_ctx{trace_id=TraceId,
Expand All @@ -86,6 +91,12 @@ inject(Ctx, Carrier, CarrierSet, _Options) ->
Carrier
end.

-spec extract(Context, Carrier, CarrierKeysFun, CarrierGetFun, Options) -> Context
when Context :: otel_ctx:t(),
Carrier :: otel_propagator:carrier(),
CarrierKeysFun :: otel_propagator_text_map:carrier_keys(),
CarrierGetFun :: otel_propagator_text_map:carrier_get(),
Options :: otel_propagator_text_map:propagator_options().
extract(Ctx, Carrier, _CarrierKeysFun, CarrierGet, _Options) ->
try
TraceId = trace_id(Carrier, CarrierGet),
Expand Down
11 changes: 11 additions & 0 deletions apps/opentelemetry_api/src/otel_propagator_baggage.erl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
fields(_) ->
[?BAGGAGE_HEADER].

-spec inject(Context, Carrier, CarrierSetFun, Options) -> Carrier
when Context :: otel_ctx:t(),
Carrier :: otel_propagator:carrier(),
CarrierSetFun :: otel_propagator_text_map:carrier_set(),
Options :: otel_propagator_text_map:propagator_options().
inject(Ctx, Carrier, CarrierSet, _Options) ->
Baggage = otel_baggage:get_all(Ctx),
case maps:fold(fun(Key, Value, Acc) ->
Expand All @@ -64,6 +69,12 @@ inject(Ctx, Carrier, CarrierSet, _Options) ->
Carrier
end.

-spec extract(Context, Carrier, CarrierKeysFun, CarrierGetFun, Options) -> Context
when Context :: otel_ctx:t(),
Carrier :: otel_propagator:carrier(),
CarrierKeysFun :: otel_propagator_text_map:carrier_keys(),
CarrierGetFun :: otel_propagator_text_map:carrier_get(),
Options :: otel_propagator_text_map:propagator_options().
extract(Ctx, Carrier, _CarrierKeysFun, CarrierGet, _Options) ->
case CarrierGet(?BAGGAGE_HEADER, Carrier) of
undefined ->
Expand Down
6 changes: 5 additions & 1 deletion apps/opentelemetry_api/src/otel_propagator_text_map.erl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@
%% an empty list is passed for options if the propagator is a module with no options
-type t() :: module() | {module(), propagator_options()}.

-export_type([t/0]).
-export_type([t/0,
carrier_set/0,
carrier_get/0,
carrier_keys/0,
propagator_options/0]).

-spec fields(otel_propagator:t()) -> [field_key()].
fields(Propagator) when is_atom(Propagator) ->
Expand Down
15 changes: 11 additions & 4 deletions apps/opentelemetry_api/src/otel_propagator_text_map_composite.erl
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,20 @@ fields(Propagators) ->
otel_propagator_text_map:fields(Propagator)
end, Propagators).

-spec inject(otel_ctx:t(), otel_propagator:carrier(), fun(),
[otel_propagator_text_map:t()]) -> otel_propagator:carrier().
-spec inject(Context, Carrier, CarrierSetFun, Injectors) -> Carrier
when Context :: otel_ctx:t(),
Carrier :: otel_propagator:carrier(),
CarrierSetFun :: otel_propagator_text_map:carrier_set(),
Injectors :: [otel_propagator_text_map:t()].
inject(Context, Carrier, CarrierSetFun, Injectors) ->
run_injectors(Context, Injectors, Carrier, CarrierSetFun).

-spec extract(otel_ctx:t(), otel_propagator:carrier(), fun(), fun(),
[otel_propagator_text_map:t()]) -> otel_ctx:t().
-spec extract(Context, Carrier, CarrierKeysFun, CarrierGetFun, Extractors) -> Context
when Context :: otel_ctx:t(),
Carrier :: otel_propagator:carrier(),
CarrierKeysFun :: otel_propagator_text_map:carrier_keys(),
CarrierGetFun :: otel_propagator_text_map:carrier_get(),
Extractors :: [otel_propagator_text_map:t()].
extract(Context, Carrier, CarrierKeysFun, CarrierGetFun, Extractors) ->
run_extractors(Context, Extractors, Carrier, CarrierKeysFun, CarrierGetFun).

Expand Down
19 changes: 13 additions & 6 deletions apps/opentelemetry_api/src/otel_propagator_text_map_noop.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,19 @@
fields(_Propagators) ->
[].

-spec inject(otel_ctx:t(), otel_propagator:carrier(), fun(),
[otel_propagator_text_map:t()]) -> otel_propagator:carrier().
inject(_Context, Carrier, _CarrierSetFun, _Injectors) ->
-spec inject(Context, Carrier, CarrierSetFun, Options) -> Carrier
when Context :: otel_ctx:t(),
Carrier :: otel_propagator:carrier(),
CarrierSetFun :: otel_propagator_text_map:carrier_set(),
Options :: otel_propagator_text_map:propagator_options().
inject(_Context, Carrier, _CarrierSetFun, _Options) ->
Carrier.

-spec extract(otel_ctx:t(), otel_propagator:carrier(), fun(), fun(),
[otel_propagator_text_map:t()]) -> otel_ctx:t().
extract(Context, _Carrier, _CarrierKeysFun, _CarrierGetFun, _Extractors) ->
-spec extract(Context, Carrier, CarrierKeysFun, CarrierGetFun, Options) -> Context
when Context :: otel_ctx:t(),
Carrier :: otel_propagator:carrier(),
CarrierKeysFun :: otel_propagator_text_map:carrier_keys(),
CarrierGetFun :: otel_propagator_text_map:carrier_get(),
Options :: otel_propagator_text_map:propagator_options().
extract(Context, _Carrier, _CarrierKeysFun, _CarrierGetFun, _Options) ->
Context.
11 changes: 11 additions & 0 deletions apps/opentelemetry_api/src/otel_propagator_trace_context.erl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
fields(_) ->
[?HEADER_KEY, ?STATE_HEADER_KEY].

-spec inject(Context, Carrier, CarrierSetFun, Options) -> Carrier
when Context :: otel_ctx:t(),
Carrier :: otel_propagator:carrier(),
CarrierSetFun :: otel_propagator_text_map:carrier_set(),
Options :: otel_propagator_text_map:propagator_options().
inject(Ctx, Carrier, CarrierSet, _Options) ->
case otel_tracer:current_span_ctx(Ctx) of
SpanCtx=#span_ctx{trace_id=TraceId,
Expand All @@ -69,6 +74,12 @@ inject(Ctx, Carrier, CarrierSet, _Options) ->
Carrier
end.

-spec extract(Context, Carrier, CarrierKeysFun, CarrierGetFun, Options) -> Context
when Context :: otel_ctx:t(),
Carrier :: otel_propagator:carrier(),
CarrierKeysFun :: otel_propagator_text_map:carrier_keys(),
CarrierGetFun :: otel_propagator_text_map:carrier_get(),
Options :: otel_propagator_text_map:propagator_options().
extract(Ctx, Carrier, _CarrierKeysFun, CarrierGet, _Options) ->
SpanCtxString = CarrierGet(?HEADER_KEY, Carrier),
case decode(string:trim(SpanCtxString)) of
Expand Down

0 comments on commit 45784c3

Please sign in to comment.