Esempio n. 1
0
def test_has_tracestate_enabled(sentry_init, tracestate_enabled):
    experiments = ({
        "propagate_tracestate": tracestate_enabled
    } if tracestate_enabled is not None else {})
    sentry_init(_experiments=experiments)

    if tracestate_enabled is True:
        assert has_tracestate_enabled() is True
    else:
        assert has_tracestate_enabled() is False
Esempio n. 2
0
    def iter_headers(self):
        # type: () -> Iterator[Tuple[str, str]]
        """
        Creates a generator which returns the span's `sentry-trace` and
        `tracestate` headers.

        If the span's containing transaction doesn't yet have a
        `sentry_tracestate` value, this will cause one to be generated and
        stored.
        """
        yield "sentry-trace", self.to_traceparent()

        tracestate = self.to_tracestate() if has_tracestate_enabled(self) else None
        # `tracestate` will only be `None` if there's no client or no DSN
        # TODO (kmclb) the above will be true once the feature is no longer
        # behind a flag
        if tracestate:
            yield "tracestate", tracestate
Esempio n. 3
0
    def capture_event(
        self,
        event,  # type: Event
        hint=None,  # type: Optional[Hint]
        scope=None,  # type: Optional[Scope]
    ):
        # type: (...) -> Optional[str]
        """Captures an event.

        :param event: A ready-made event that can be directly sent to Sentry.

        :param hint: Contains metadata about the event that can be read from `before_send`, such as the original exception object or a HTTP request object.

        :returns: An event ID. May be `None` if there is no DSN set or of if the SDK decided to discard the event for other reasons. In such situations setting `debug=True` on `init()` may help.
        """
        if disable_capture_event.get(False):
            return None

        if self.transport is None:
            return None
        if hint is None:
            hint = {}
        event_id = event.get("event_id")
        hint = dict(hint or ())  # type: Hint

        if event_id is None:
            event["event_id"] = event_id = uuid.uuid4().hex
        if not self._should_capture(event, hint, scope):
            return None

        event_opt = self._prepare_event(event, hint, scope)
        if event_opt is None:
            return None

        # whenever we capture an event we also check if the session needs
        # to be updated based on that information.
        session = scope._session if scope else None
        if session:
            self._update_session_from_event(session, event)

        attachments = hint.get("attachments")
        is_transaction = event_opt.get("type") == "transaction"

        # this is outside of the `if` immediately below because even if we don't
        # use the value, we want to make sure we remove it before the event is
        # sent
        raw_tracestate = (
            event_opt.get("contexts", {}).get("trace", {}).pop("tracestate", "")
        )

        # Transactions or events with attachments should go to the /envelope/
        # endpoint.
        if is_transaction or attachments:

            headers = {
                "event_id": event_opt["event_id"],
                "sent_at": format_timestamp(datetime.utcnow()),
            }

            tracestate_data = raw_tracestate and reinflate_tracestate(
                raw_tracestate.replace("sentry=", "")
            )
            if tracestate_data and has_tracestate_enabled():
                headers["trace"] = tracestate_data

            envelope = Envelope(headers=headers)

            if is_transaction:
                envelope.add_transaction(event_opt)
            else:
                envelope.add_event(event_opt)

            for attachment in attachments or ():
                envelope.add_item(attachment.to_envelope_item())
            self.transport.capture_envelope(envelope)
        else:
            # All other events go to the /store/ endpoint.
            self.transport.capture_event(event_opt)
        return event_id