1
0
Fork 0
mirror of https://gitlab.com/bramw/baserow.git synced 2025-04-15 09:34:13 +00:00

Allow forcing full OTEL trace by setting query param

This commit is contained in:
Bram Wiepjes 2024-09-02 17:13:21 +00:00
parent 5f60851e2a
commit 42f8ca0e3f
4 changed files with 54 additions and 6 deletions
backend
src/baserow/core/telemetry
tests/baserow/core/telemetry
deploy/otel

View file

@ -8,15 +8,16 @@ from opentelemetry.sdk.trace.sampling import (
DEFAULT_ON,
ParentBasedTraceIdRatio,
Sampler,
TraceIdRatioBased,
)
from .sampling import ForcableTraceIdRatioBased
_KNOWN_SAMPLERS = {
"always_on": ALWAYS_ON,
"always_off": ALWAYS_OFF,
"parentbased_always_on": DEFAULT_ON,
"parentbased_always_off": DEFAULT_OFF,
"traceidratio": TraceIdRatioBased,
"traceidratio": ForcableTraceIdRatioBased,
"parentbased_traceidratio": ParentBasedTraceIdRatio,
}

View file

@ -0,0 +1,46 @@
from typing import Optional
from opentelemetry import trace
from opentelemetry.context import Context
from opentelemetry.sdk.trace import Span
from opentelemetry.sdk.trace.sampling import _KNOWN_SAMPLERS as ROOT_KNOWN_SAMPLERS
from opentelemetry.sdk.trace.sampling import (
Decision,
SamplingResult,
TraceIdRatioBased,
_get_parent_trace_state,
)
class ForcableTraceIdRatioBased(TraceIdRatioBased):
def should_sample(
self,
parent_context: Optional["Context"],
trace_id: int,
name: str,
kind=None,
attributes=None,
links=None,
trace_state=None,
) -> SamplingResult:
current_span = trace.get_current_span(parent_context)
if isinstance(current_span, Span):
span_attributes = current_span.attributes
# Check if the HTTP request contains the query parameter to force full
# tracing, and if so do the full trace.
http_url = span_attributes.get("http.url", "")
if "force_full_otel_trace=true" in http_url:
return SamplingResult(
Decision.RECORD_AND_SAMPLE,
span_attributes,
_get_parent_trace_state(parent_context),
)
# If the full trace query parameter is not provided, then fallback on the
# original TraceIdRatioBased class.
return super().should_sample(
parent_context, trace_id, name, kind, attributes, links, trace_state
)
ROOT_KNOWN_SAMPLERS["traceidratio"] = ForcableTraceIdRatioBased

View file

@ -4,10 +4,10 @@ from opentelemetry.sdk.trace.sampling import (
DEFAULT_OFF,
DEFAULT_ON,
ParentBasedTraceIdRatio,
TraceIdRatioBased,
)
from baserow.core.telemetry.env_overrides_parser import get_sampler_overrides_from_str
from baserow.core.telemetry.sampling import ForcableTraceIdRatioBased
def test_invalid_sampler_strings():
@ -52,7 +52,7 @@ def test_valid_sampler_strings():
def test_valid_sampler_with_args():
for_trace_id = get_sampler_overrides_from_str("my.module=traceidratio@0.4")
assert type(for_trace_id["my.module"]) is TraceIdRatioBased
assert type(for_trace_id["my.module"]) is ForcableTraceIdRatioBased
assert for_trace_id["my.module"].rate == 0.4
for_parent_trace_id = get_sampler_overrides_from_str(
"my.module=parentbased_traceidratio@0.4"

View file

@ -2,7 +2,8 @@ receivers:
otlp:
protocols:
grpc: # port 4317
http: # port 4318
http:
endpoint: "0.0.0.0:4318"
processors:
# Disable sending spans for the sync templates task as it contains a huge number
@ -55,4 +56,4 @@ service:
exporters: [otlp,logging]
extensions:
health_check:
health_check: