Skip to content

Commit

Permalink
test: fix instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
JoanFM committed Nov 6, 2024
1 parent df81dbd commit ea363cf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
22 changes: 15 additions & 7 deletions tests/unit/serve/instrumentation/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(
type, "opentelemetry.sdk.metrics.view.Aggregation"
] = None,
):
print(f'JOAN IS HERE DIRMETRIC')
super().__init__(
preferred_temporality=preferred_temporality,
preferred_aggregation=preferred_aggregation,
Expand All @@ -40,6 +41,7 @@ def export(
timeout_millis: float = 10_000,
**kwargs,
) -> MetricExportResult:
print(f'export to {self.metric_filename} => {metrics_data.to_json()[0:3]}')
self.f.write(metrics_data.to_json())
self.f.write('\n')
self.f.flush()
Expand Down Expand Up @@ -75,6 +77,7 @@ def monkeypatch_metric_exporter(
f.write('0')

def collect_metrics():
print(f'tick_counter_filename {tick_counter_filename}')
with open(tick_counter_filename, 'r', encoding='utf-8') as ft:
tick_counter = int(ft.read())
with open(tick_counter_filename, 'w', encoding='utf-8') as ft2:
Expand All @@ -88,15 +91,20 @@ def _get_service_name(otel_measurement):

def read_metrics():
def read_metric_file(filename):
print(f'filename {filename}')
with open(filename, 'r', encoding='utf-8') as fr:
r = fr.read()
print(f'READ {r}')
return json.loads(r)

return {
_get_service_name(i): i
for i in map(read_metric_file, metrics_path.glob('*'))
}
print(f'READ {r[0:3]}')
try:
return json.loads(r)
except:
return None

ret = {}
for i in map(read_metric_file, metrics_path.glob('*')):
if i is not None:
ret[_get_service_name(i)] = i
return ret

class PatchedTextReader(PeriodicExportingMetricReader):
def __init__(self, *args, **kwargs) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def meow(self, docs, **kwargs):
f.post('/')
collect_metrics()
metrics = read_metrics()
print(f' metrics {metrics.keys()}')
gateway_metrics = metrics['gateway/rep-0']['resource_metrics'][0][
'scope_metrics'
][0]['metrics']
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/serve/instrumentation/test_instrumentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ def _sleep():
}

@MetricsTimer(summary, histogram, labels)
def _sleep():
def _sleep_2():
time.sleep(0.1)

_sleep()
_sleep_2()

# Prometheus samples
summary_count_sample = [
Expand All @@ -107,5 +107,5 @@ def _sleep():
.to_json()
)
assert 'time_taken_decorator' == histogram_metric['name']
assert 1 == histogram_metric['data']['data_points'][0]['count']
assert labels == histogram_metric['data']['data_points'][0]['attributes']
assert 1 == histogram_metric['data']['data_points'][1]['count']
assert labels == histogram_metric['data']['data_points'][1]['attributes']

0 comments on commit ea363cf

Please sign in to comment.