def test_adds_tracestate_to_transaction_when_to_traceparent_called( sentry_init): sentry_init( dsn= "https://[email protected]/12312012", environment="dogpark", release="off.leash.park", ) transaction = Transaction( name="/interactions/other-dogs/new-dog", op="greeting.sniff", ) # no inherited tracestate, and none created in Transaction constructor assert transaction._sentry_tracestate is None transaction.to_tracestate() assert transaction._sentry_tracestate is not None
def test_tracestate_is_immutable_once_set(sentry_init, monkeypatch, set_by): monkeypatch.setattr( sentry_sdk.tracing, "compute_tracestate_entry", mock.Mock(return_value="sentry=doGsaREgReaT"), ) sentry_init( dsn= "https://[email protected]/12312012", environment="dogpark", release="off.leash.park", ) # for each scenario, get to the point where tracestate has been set if set_by == "inheritance": transaction = Transaction( name="/interactions/other-dogs/new-dog", op="greeting.sniff", sentry_tracestate=("sentry=doGsaREgReaT"), ) else: transaction = Transaction( name="/interactions/other-dogs/new-dog", op="greeting.sniff", ) if set_by == "to_tracestate": transaction.to_tracestate() if set_by == "get_trace_context": transaction.get_trace_context() assert transaction._sentry_tracestate == "sentry=doGsaREgReaT" # user data would be included in tracestate if it were recomputed at this point sentry_sdk.set_user({"id": 12312013, "segment": "bigs"}) # value hasn't changed assert transaction._sentry_tracestate == "sentry=doGsaREgReaT"
def test_to_tracestate(sentry_init): sentry_init( dsn= "https://[email protected]/12312012", environment="dogpark", release="off.leash.park", ) # it correctly uses the value from the transaction itself or the span's # containing transaction transaction_no_third_party = Transaction( trace_id="12312012123120121231201212312012", sentry_tracestate="sentry=doGsaREgReaT", ) non_orphan_span = Span() non_orphan_span._containing_transaction = transaction_no_third_party assert transaction_no_third_party.to_tracestate() == "sentry=doGsaREgReaT" assert non_orphan_span.to_tracestate() == "sentry=doGsaREgReaT" # it combines sentry and third-party values correctly transaction_with_third_party = Transaction( trace_id="12312012123120121231201212312012", sentry_tracestate="sentry=doGsaREgReaT", third_party_tracestate="maisey=silly", ) assert (transaction_with_third_party.to_tracestate() == "sentry=doGsaREgReaT,maisey=silly") # it computes a tracestate from scratch for orphan transactions orphan_span = Span(trace_id="12312012123120121231201212312012", ) assert orphan_span._containing_transaction is None assert orphan_span.to_tracestate() == "sentry=" + compute_tracestate_value( { "trace_id": "12312012123120121231201212312012", "environment": "dogpark", "release": "off.leash.park", "public_key": "dogsarebadatkeepingsecrets", })
def test_tracestate_computation(sentry_init): sentry_init( dsn= "https://[email protected]/12312012", environment="dogpark", release="off.leash.park", ) sentry_sdk.set_user({"id": 12312013, "segment": "bigs"}) transaction = Transaction( name="/interactions/other-dogs/new-dog", op="greeting.sniff", trace_id="12312012123120121231201212312012", ) # force lazy computation to create a value transaction.to_tracestate() computed_value = transaction._sentry_tracestate.replace("sentry=", "") # we have to decode and reinflate the data because we can guarantee that the # order of the entries in the jsonified dict will be the same here as when # the tracestate is computed reinflated_trace_data = json.loads(from_base64(computed_value)) assert reinflated_trace_data == { "trace_id": "12312012123120121231201212312012", "environment": "dogpark", "release": "off.leash.park", "public_key": "dogsarebadatkeepingsecrets", "user": { "id": 12312013, "segment": "bigs" }, "transaction": "/interactions/other-dogs/new-dog", }