def attach_context(self, context: RequestContext) -> None: """Attach this to the provided :py:class:`~baseplate.RequestContext`. :param context: request context to attach this to """ context.request_context = self context.raw_request_context = self._header
def on_server_span_created(self, context: RequestContext, server_span: Span) -> None: batch = self.client.batch() context.metrics = batch if self.sample_rate == 1.0 or random() < self.sample_rate: observer: SpanObserver = MetricsServerSpanObserver(batch, server_span, self.sample_rate) else: observer = MetricsServerSpanDummyObserver(batch) server_span.register(observer)
def test_observer(observer_cls, name): batch = FakeBatch() span = Span( trace_id=1234, parent_id=2345, span_id=3456, sampled=None, flags=None, name="fancy.span", context=RequestContext({}), ) allow_list = {"client", "endpoint", "tag2"} sample_rate = 0.3 observer = observer_cls(batch, span, allow_list, sample_rate=0.3) observer.on_start() observer.on_incr_tag("my.tag", 2) observer.on_set_tag("tag1", "foo") observer.on_set_tag("tag2", "bar") observer.on_finish(None) assert batch.timers timer = batch.timers.pop() assert batch.timers == [] assert timer["sample_rate"] == sample_rate assert timer["name"] == f"baseplate.{name}.latency" if name in ("server", "local"): assert timer["tags"].pop("endpoint") == "fancy.span" else: assert timer["tags"].pop("client") == "fancy" assert timer["tags"].pop("endpoint") == "span" assert timer["tags"].pop("tag2") == "bar" assert timer["tags"] == {} for _ in range(2): counter = batch.counters.pop() assert counter["sample_rate"] == sample_rate if counter["name"] == f"baseplate.{name}.rate": assert counter["delta"] == 1 assert counter["tags"].pop("success") is True elif counter["name"] == "my.tag": assert counter["delta"] == 2 else: raise Exception(f"unexpected counter: {counter}") if name in ("server", "local"): assert counter["tags"].pop("endpoint") == "fancy.span" else: assert counter["tags"].pop("client") == "fancy" assert counter["tags"].pop("endpoint") == "span" assert counter["tags"].pop("tag2") == "bar" assert counter["tags"] == {}
def test_nested(): batch = FakeBatch() span = ServerSpan( trace_id=1234, parent_id=2345, span_id=3456, sampled=None, flags=None, name="fancy.span", context=RequestContext({}), ) observer = TaggedMetricsLocalSpanObserver(batch, span, {"client", "endpoint"}) span.register(observer) with span.make_child("foo", local=True, component_name="foo") as child_span: assert len(child_span.observers) == 1
def on_server_span_created(self, context: RequestContext, server_span: Span) -> None: observer = SentryServerSpanObserver(self.raven, server_span) server_span.register(observer) context.sentry = self.raven
def on_server_span_created(self, context: RequestContext, server_span: Span) -> None: batch = self.client.batch() context.metrics = batch observer = MetricsServerSpanObserver(batch, server_span) server_span.register(observer)
def __init__(self, context_config: Dict[str, Any], environ: Dict[str, str]): RequestContext.__init__(self, context_config) pyramid.request.Request.__init__(self, environ)
def on_server_span_created(self, context: RequestContext, server_span: Span) -> None: sentry_hub = sentry_sdk.Hub.current observer = _SentryServerSpanObserver(sentry_hub, server_span) server_span.register(observer) context.sentry = sentry_hub
def __init__(self, *args: Any, **kwargs: Any) -> None: context_config = kwargs.pop("context_config", None) RequestContext.__init__(self, context_config=context_config) pyramid.request.Request.__init__(self, *args, **kwargs)