Skip to content

Commit a1ab339

Browse files
authored
feat(metrics): Add value, unit to before_emit_metric (#2958)
1 parent 18ccb8f commit a1ab339

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

sentry_sdk/consts.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424
Event,
2525
EventProcessor,
2626
Hint,
27+
MeasurementUnit,
2728
ProfilerMode,
2829
TracesSampler,
2930
TransactionProcessor,
3031
MetricTags,
32+
MetricValue,
3133
)
3234

3335
# Experiments are feature flags to enable and disable certain unstable SDK
@@ -47,7 +49,9 @@
4749
"transport_zlib_compression_level": Optional[int],
4850
"transport_num_pools": Optional[int],
4951
"enable_metrics": Optional[bool],
50-
"before_emit_metric": Optional[Callable[[str, MetricTags], bool]],
52+
"before_emit_metric": Optional[
53+
Callable[[str, MetricValue, MeasurementUnit, MetricTags], bool]
54+
],
5155
"metric_code_locations": Optional[bool],
5256
},
5357
total=False,

sentry_sdk/metrics.py

+22-9
Original file line numberDiff line numberDiff line change
@@ -703,8 +703,8 @@ def _get_aggregator():
703703
)
704704

705705

706-
def _get_aggregator_and_update_tags(key, tags):
707-
# type: (str, Optional[MetricTags]) -> Tuple[Optional[MetricsAggregator], Optional[LocalAggregator], Optional[MetricTags]]
706+
def _get_aggregator_and_update_tags(key, value, unit, tags):
707+
# type: (str, Optional[MetricValue], MeasurementUnit, Optional[MetricTags]) -> Tuple[Optional[MetricsAggregator], Optional[LocalAggregator], Optional[MetricTags]]
708708
hub = sentry_sdk.Hub.current
709709
client = hub.client
710710
if client is None or client.metrics_aggregator is None:
@@ -732,7 +732,7 @@ def _get_aggregator_and_update_tags(key, tags):
732732
if before_emit_callback is not None:
733733
with recursion_protection() as in_metrics:
734734
if not in_metrics:
735-
if not before_emit_callback(key, updated_tags):
735+
if not before_emit_callback(key, value, unit, updated_tags):
736736
return None, None, updated_tags
737737

738738
return client.metrics_aggregator, local_aggregator, updated_tags
@@ -748,7 +748,9 @@ def increment(
748748
):
749749
# type: (...) -> None
750750
"""Increments a counter."""
751-
aggregator, local_aggregator, tags = _get_aggregator_and_update_tags(key, tags)
751+
aggregator, local_aggregator, tags = _get_aggregator_and_update_tags(
752+
key, value, unit, tags
753+
)
752754
if aggregator is not None:
753755
aggregator.add(
754756
"c", key, value, unit, tags, timestamp, local_aggregator, stacklevel
@@ -809,7 +811,10 @@ def __exit__(self, exc_type, exc_value, tb):
809811
# type: (Any, Any, Any) -> None
810812
assert self._span, "did not enter"
811813
aggregator, local_aggregator, tags = _get_aggregator_and_update_tags(
812-
self.key, self.tags
814+
self.key,
815+
self.value,
816+
self.unit,
817+
self.tags,
813818
)
814819
if aggregator is not None:
815820
elapsed = TIMING_FUNCTIONS[self.unit]() - self.entered # type: ignore
@@ -864,7 +869,9 @@ def timing(
864869
- it can be used as a decorator
865870
"""
866871
if value is not None:
867-
aggregator, local_aggregator, tags = _get_aggregator_and_update_tags(key, tags)
872+
aggregator, local_aggregator, tags = _get_aggregator_and_update_tags(
873+
key, value, unit, tags
874+
)
868875
if aggregator is not None:
869876
aggregator.add(
870877
"d", key, value, unit, tags, timestamp, local_aggregator, stacklevel
@@ -882,7 +889,9 @@ def distribution(
882889
):
883890
# type: (...) -> None
884891
"""Emits a distribution."""
885-
aggregator, local_aggregator, tags = _get_aggregator_and_update_tags(key, tags)
892+
aggregator, local_aggregator, tags = _get_aggregator_and_update_tags(
893+
key, value, unit, tags
894+
)
886895
if aggregator is not None:
887896
aggregator.add(
888897
"d", key, value, unit, tags, timestamp, local_aggregator, stacklevel
@@ -899,7 +908,9 @@ def set(
899908
):
900909
# type: (...) -> None
901910
"""Emits a set."""
902-
aggregator, local_aggregator, tags = _get_aggregator_and_update_tags(key, tags)
911+
aggregator, local_aggregator, tags = _get_aggregator_and_update_tags(
912+
key, value, unit, tags
913+
)
903914
if aggregator is not None:
904915
aggregator.add(
905916
"s", key, value, unit, tags, timestamp, local_aggregator, stacklevel
@@ -916,7 +927,9 @@ def gauge(
916927
):
917928
# type: (...) -> None
918929
"""Emits a gauge."""
919-
aggregator, local_aggregator, tags = _get_aggregator_and_update_tags(key, tags)
930+
aggregator, local_aggregator, tags = _get_aggregator_and_update_tags(
931+
key, value, unit, tags
932+
)
920933
if aggregator is not None:
921934
aggregator.add(
922935
"g", key, value, unit, tags, timestamp, local_aggregator, stacklevel

tests/test_metrics.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -734,9 +734,10 @@ def test_tag_normalization(
734734
def test_before_emit_metric(
735735
sentry_init, capture_envelopes, maybe_monkeypatched_threading
736736
):
737-
def before_emit(key, tags):
738-
if key == "removed-metric":
737+
def before_emit(key, value, unit, tags):
738+
if key == "removed-metric" or value == 47 or unit == "unsupported":
739739
return False
740+
740741
tags["extra"] = "foo"
741742
del tags["release"]
742743
# this better be a noop!
@@ -755,6 +756,8 @@ def before_emit(key, tags):
755756
envelopes = capture_envelopes()
756757

757758
metrics.increment("removed-metric", 1.0)
759+
metrics.increment("another-removed-metric", 47)
760+
metrics.increment("yet-another-removed-metric", 1.0, unit="unsupported")
758761
metrics.increment("actual-metric", 1.0)
759762
Hub.current.flush()
760763

0 commit comments

Comments
 (0)