Skip to content

Commit

Permalink
Minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
armenzg committed Sep 20, 2023
1 parent 0af0645 commit 981e80f
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions src/sentry/snuba/metrics/extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,6 @@
# generic percentile is not supported by metrics layer.
}

# Maps plain Discover functions to derived metric functions which are understood by the metrics layer.
_SEARCH_TO_DERIVED_METRIC_AGGREGATES: Dict[str, MetricOperationType] = {
"failure_count": "on_demand_failure_count",
"failure_rate": "on_demand_failure_rate",
"apdex": "on_demand_apdex",
}

# Mapping to infer metric type from Discover function.
_AGGREGATE_TO_METRIC_TYPE = {
"count": "c",
Expand All @@ -133,10 +126,6 @@
"p75": "d",
"p95": "d",
"p99": "d",
# With on demand metrics, evaluated metrics are actually stored, thus we have to choose a concrete metric type.
"failure_count": "c",
"failure_rate": "c",
"apdex": "c",
}

# Query fields that on their own do not require on-demand metric extraction but if present in an on-demand query
Expand Down Expand Up @@ -620,13 +609,37 @@ def apdex_tag_spec(project: Project, argument: Optional[str]) -> List[TagSpec]:
]


# This is used to map a metric to a function which generates a specification
_DERIVED_METRICS: Dict[MetricOperationType, TagsSpecsGenerator] = {
"on_demand_failure_count": failure_tag_spec,
"on_demand_failure_rate": failure_tag_spec,
"on_demand_apdex": apdex_tag_spec,
DERIVED_METRICS = {
"apdex": {
"tag_spec_func": apdex_tag_spec,
"hash_method": "op_plus_arg",
},
"failure_count": {
"tag_spec_func": failure_tag_spec,
"hash_method": "op",
},
"failure_rate": {
"tag_spec_func": failure_tag_spec,
"hash_method": "op",
},
}

# This is used to map a metric to a function which generates a specification
_DERIVED_METRICS: Dict[MetricOperationType, TagsSpecsGenerator] = {}
_HASH_METHOD = {}
for metric in DERIVED_METRICS.keys():
_DERIVED_METRICS[f"on_demand_{metric}"] = DERIVED_METRICS[metric]["tag_spec_func"]
_HASH_METHOD[f"on_demand_{metric}"] = DERIVED_METRICS[metric]["hash_method"]

# Maps plain Discover functions to derived metric functions which are understood by the metrics layer.
_SEARCH_TO_DERIVED_METRIC_AGGREGATES: Dict[str, MetricOperationType] = {}
for metric in DERIVED_METRICS.keys():
_SEARCH_TO_DERIVED_METRIC_AGGREGATES[metric] = f"on_demand_{metric}"

# With on demand metrics, evaluated metrics are actually stored, thus we have to choose a concrete metric type.
for metric in DERIVED_METRICS:
_AGGREGATE_TO_METRIC_TYPE[metric] = "c"


@dataclass(frozen=True)
class FieldParsingResult:
Expand Down Expand Up @@ -700,9 +713,9 @@ def _field_for_hash(self) -> Optional[str]:
# with condition `f` and this will create a problem, since we might already have data for the `count()` and when
# `apdex()` is created in the UI, we will use that metric but that metric didn't extract in the past the tags
# that are used for apdex calculation, effectively causing problems with the data.
if self.op in ["on_demand_failure_count", "on_demand_failure_rate"]:
if _HASH_METHOD[self.op] == "op":
return self.op
elif self.op == "on_demand_apdex":
elif _HASH_METHOD[self.op] == "op_plus_arg":
return f"{self.op}:{self._argument}"

return self._argument
Expand Down

0 comments on commit 981e80f

Please sign in to comment.