Skip to content

Commit 8eccfcf

Browse files
committed
Fix context labels logging
1 parent c791561 commit 8eccfcf

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/metricrule/agent/mrmetric.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,13 @@ def get_instrument_specs(
6363
be used in.
6464
"""
6565
specs = {}
66+
67+
def get_spec_fn(metric_config: metric_configuration_pb2.MetricConfig):
68+
return _get_instrument_spec(metric_config, config.context_labels_from_input)
6669
specs[MetricContext.INPUT] = tuple(
67-
map(_get_instrument_spec, config.input_metrics))
70+
map(get_spec_fn, config.input_metrics))
6871
specs[MetricContext.OUTPUT] = tuple(
69-
map(_get_instrument_spec, config.output_metrics))
72+
map(get_spec_fn, config.output_metrics))
7073
return specs
7174

7275

@@ -88,12 +91,15 @@ def get_metric_instances(
8891
"""
8992
configs: tuple[metric_configuration_pb2.MetricConfig, ...]
9093
filter_str: str
94+
ctx_labels_for_spec: tuple[metric_configuration_pb2.LabelConfig, ...]
9195
if context == MetricContext.INPUT:
9296
configs = tuple(config.input_metrics)
9397
filter_str = _format_filter(config.input_content_filter)
98+
ctx_labels_for_spec = config.context_labels_from_input
9499
elif context == MetricContext.OUTPUT:
95100
configs = tuple(config.output_metrics)
96101
filter_str = _format_filter(config.output_content_filter)
102+
ctx_labels_for_spec = config.context_labels_from_input
97103
else:
98104
return {}
99105

@@ -106,7 +112,7 @@ def get_metric_instances(
106112
for filtered_payload in filtered_values:
107113
values = _get_metric_values(metric_config, filtered_payload)
108114
labels = _get_metric_labels(metric_config, filtered_payload)
109-
spec = _get_instrument_spec(metric_config)
115+
spec = _get_instrument_spec(metric_config, ctx_labels_for_spec)
110116
if spec in outputs:
111117
outputs[spec].append(MetricInstance(values, labels))
112118
else:
@@ -159,10 +165,13 @@ def _format_filter(filter_str: str) -> str:
159165

160166
def _get_instrument_spec(
161167
config: metric_configuration_pb2.MetricConfig,
168+
context_labels: tuple[metric_configuration_pb2.LabelConfig, ...] = ()
162169
) -> MetricInstrumentSpec:
163170
instrument_type = _get_instrument_type(config)
164171
metric_value_type = _get_metric_value_type(config)
165-
label_names = _get_metric_label_keys_no_payload(config)
172+
label_names = _get_metric_label_keys_no_payload(
173+
config) + _label_keys_no_payload(context_labels)
174+
166175
return MetricInstrumentSpec(
167176
instrumentType=instrument_type,
168177
metricValueType=metric_value_type,
@@ -229,9 +238,15 @@ def _get_metric_labels(
229238

230239
def _get_metric_label_keys_no_payload(
231240
config: metric_configuration_pb2.MetricConfig
241+
) -> tuple[str, ...]:
242+
return _label_keys_no_payload(config.labels)
243+
244+
245+
def _label_keys_no_payload(
246+
configs: tuple[metric_configuration_pb2.LabelConfig, ...]
232247
) -> tuple[str, ...]:
233248
label_keys: list[str] = []
234-
for label_config in config.labels:
249+
for label_config in configs:
235250
key = _extract_values(label_config.label_key, {})
236251
label_keys.extend(key)
237252
return tuple(label_keys)

0 commit comments

Comments
 (0)