Created
June 22, 2023 09:31
-
-
Save derekkraan/511ce285cdc55bf499ded2bda760272f to your computer and use it in GitHub Desktop.
Sampler for Opentelemetry / Honeycomb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Opentelemetry.CodeCodeShipSampler do | |
@behaviour :otel_sampler | |
@impl :otel_sampler | |
def setup(_sampler_opts) do | |
[] | |
end | |
@impl :otel_sampler | |
def description(_sampler_config) do | |
"CodeCodeShipSampler" | |
end | |
# sample rate here is "1/x events get sent", so if you put 10, then one in ten events get sampled, nine out of ten get discarded. | |
defp sample_rate( | |
_ctx, | |
_trace_id, | |
_links, | |
_span_name, | |
_span_kind, | |
%{source: "oban_producers"}, | |
_sampler_config | |
) do | |
50 | |
end | |
defp sample_rate( | |
_ctx, | |
_trace_id, | |
_links, | |
_span_name, | |
_span_kind, | |
%{source: "oban_jobs"}, | |
_sampler_config | |
) do | |
50 | |
end | |
defp sample_rate( | |
_ctx, | |
_trace_id, | |
_links, | |
"aws_request", | |
_span_kind, | |
_attrs, | |
_sampler_config | |
) do | |
10 | |
end | |
defp sample_rate( | |
_ctx, | |
_trace_id, | |
_links, | |
_span_name, | |
_span_kind, | |
%{"http.target": "/health_check"}, | |
_sampler_config | |
) do | |
50 | |
end | |
defp sample_rate( | |
_ctx, | |
_trace_id, | |
_links, | |
_span_name, | |
_span_kind, | |
_span_attributes, | |
_sampler_config | |
) do | |
# 1 = always sample | |
1 | |
end | |
@impl :otel_sampler | |
def should_sample( | |
ctx, | |
trace_id, | |
links, | |
span_name, | |
span_kind, | |
attributes, | |
sampler_config | |
) do | |
sample_rate = | |
sample_rate( | |
ctx, | |
trace_id, | |
links, | |
span_name, | |
span_kind, | |
attributes, | |
sampler_config | |
) | |
{result, _attrs, tracestate} = | |
:otel_sampler_trace_id_ratio_based.should_sample( | |
ctx, | |
trace_id, | |
links, | |
span_name, | |
span_kind, | |
attributes, | |
:otel_sampler_trace_id_ratio_based.setup(1.0 / sample_rate) | |
) | |
# Honeycomb wants to know the SampleRate so that it can account for the non-sampled spans | |
{result, [SampleRate: sample_rate], tracestate} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment