|
| 1 | +import sentry_sdk |
| 2 | + |
| 3 | +from tests.test_metrics import envelopes_to_metrics |
| 4 | + |
| 5 | + |
| 6 | +def test_scope_precedence(sentry_init, capture_envelopes): |
| 7 | + # Order of precedence, from most important to least: |
| 8 | + # 1. telemetry attributes (directly supplying attributes on creation or using set_attribute) |
| 9 | + # 2. current scope attributes |
| 10 | + # 3. isolation scope attributes |
| 11 | + # 4. global scope attributes |
| 12 | + sentry_init() |
| 13 | + |
| 14 | + envelopes = capture_envelopes() |
| 15 | + |
| 16 | + global_scope = sentry_sdk.get_global_scope() |
| 17 | + global_scope.set_attribute("global.attribute", "global") |
| 18 | + global_scope.set_attribute("overwritten.attribute", "global") |
| 19 | + |
| 20 | + isolation_scope = sentry_sdk.get_isolation_scope() |
| 21 | + isolation_scope.set_attribute("isolation.attribute", "isolation") |
| 22 | + isolation_scope.set_attribute("overwritten.attribute", "isolation") |
| 23 | + |
| 24 | + current_scope = sentry_sdk.get_current_scope() |
| 25 | + current_scope.set_attribute("current.attribute", "current") |
| 26 | + current_scope.set_attribute("overwritten.attribute", "current") |
| 27 | + |
| 28 | + sentry_sdk.metrics.count("test", 1) |
| 29 | + sentry_sdk.get_client().flush() |
| 30 | + |
| 31 | + metrics = envelopes_to_metrics(envelopes) |
| 32 | + (metric,) = metrics |
| 33 | + |
| 34 | + assert metric["attributes"]["global.attribute"] == "global" |
| 35 | + assert metric["attributes"]["isolation.attribute"] == "isolation" |
| 36 | + assert metric["attributes"]["current.attribute"] == "current" |
| 37 | + |
| 38 | + assert metric["attributes"]["overwritten.attribute"] == "current" |
| 39 | + |
| 40 | + |
| 41 | +def test_telemetry_precedence(sentry_init, capture_envelopes): |
| 42 | + # Order of precedence, from most important to least: |
| 43 | + # 1. telemetry attributes (directly supplying attributes on creation or using set_attribute) |
| 44 | + # 2. current scope attributes |
| 45 | + # 3. isolation scope attributes |
| 46 | + # 4. global scope attributes |
| 47 | + sentry_init() |
| 48 | + |
| 49 | + envelopes = capture_envelopes() |
| 50 | + |
| 51 | + global_scope = sentry_sdk.get_global_scope() |
| 52 | + global_scope.set_attribute("global.attribute", "global") |
| 53 | + global_scope.set_attribute("overwritten.attribute", "global") |
| 54 | + |
| 55 | + isolation_scope = sentry_sdk.get_isolation_scope() |
| 56 | + isolation_scope.set_attribute("isolation.attribute", "isolation") |
| 57 | + isolation_scope.set_attribute("overwritten.attribute", "isolation") |
| 58 | + |
| 59 | + current_scope = sentry_sdk.get_current_scope() |
| 60 | + current_scope.set_attribute("current.attribute", "current") |
| 61 | + current_scope.set_attribute("overwritten.attribute", "current") |
| 62 | + |
| 63 | + sentry_sdk.metrics.count( |
| 64 | + "test", |
| 65 | + 1, |
| 66 | + attributes={ |
| 67 | + "telemetry.attribute": "telemetry", |
| 68 | + "overwritten.attribute": "telemetry", |
| 69 | + }, |
| 70 | + ) |
| 71 | + |
| 72 | + sentry_sdk.get_client().flush() |
| 73 | + |
| 74 | + metrics = envelopes_to_metrics(envelopes) |
| 75 | + (metric,) = metrics |
| 76 | + |
| 77 | + assert metric["attributes"]["global.attribute"] == "global" |
| 78 | + assert metric["attributes"]["isolation.attribute"] == "isolation" |
| 79 | + assert metric["attributes"]["current.attribute"] == "current" |
| 80 | + assert metric["attributes"]["telemetry.attribute"] == "telemetry" |
| 81 | + |
| 82 | + assert metric["attributes"]["overwritten.attribute"] == "telemetry" |
| 83 | + |
| 84 | + |
| 85 | +def test_attribute_out_of_scope(sentry_init, capture_envelopes): |
| 86 | + sentry_init() |
| 87 | + |
| 88 | + envelopes = capture_envelopes() |
| 89 | + |
| 90 | + with sentry_sdk.new_scope() as scope: |
| 91 | + scope.set_attribute("outofscope.attribute", "out of scope") |
| 92 | + |
| 93 | + sentry_sdk.metrics.count("test", 1) |
| 94 | + |
| 95 | + sentry_sdk.get_client().flush() |
| 96 | + |
| 97 | + metrics = envelopes_to_metrics(envelopes) |
| 98 | + (metric,) = metrics |
| 99 | + |
| 100 | + assert "outofscope.attribute" not in metric["attributes"] |
| 101 | + |
| 102 | + |
| 103 | +def test_remove_attribute(sentry_init, capture_envelopes): |
| 104 | + sentry_init() |
| 105 | + |
| 106 | + envelopes = capture_envelopes() |
| 107 | + |
| 108 | + with sentry_sdk.new_scope() as scope: |
| 109 | + scope.set_attribute("some.attribute", 123) |
| 110 | + scope.remove_attribute("some.attribute") |
| 111 | + |
| 112 | + sentry_sdk.metrics.count("test", 1) |
| 113 | + |
| 114 | + sentry_sdk.get_client().flush() |
| 115 | + |
| 116 | + metrics = envelopes_to_metrics(envelopes) |
| 117 | + (metric,) = metrics |
| 118 | + |
| 119 | + assert "some.attribute" not in metric["attributes"] |
0 commit comments