-
Couldn't load subscription status.
- Fork 1.2k
test: enable telemetry tests in server mode #3927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the terms described in the LICENSE file in | ||
| # the root directory of this source tree. | ||
|
|
||
| """Telemetry collector helpers for integration tests.""" | ||
|
|
||
| from .base import BaseTelemetryCollector, SpanStub | ||
| from .in_memory import InMemoryTelemetryCollector, InMemoryTelemetryManager | ||
| from .otlp import OtlpHttpTestCollector | ||
|
|
||
| __all__ = [ | ||
| "BaseTelemetryCollector", | ||
| "SpanStub", | ||
| "InMemoryTelemetryCollector", | ||
| "InMemoryTelemetryManager", | ||
| "OtlpHttpTestCollector", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the terms described in the LICENSE file in | ||
| # the root directory of this source tree. | ||
|
|
||
| """Shared helpers for telemetry test collectors.""" | ||
|
|
||
| from collections.abc import Iterable | ||
| from dataclasses import dataclass | ||
| from typing import Any | ||
|
|
||
|
|
||
| @dataclass | ||
| class SpanStub: | ||
| name: str | ||
| attributes: dict[str, Any] | ||
| resource_attributes: dict[str, Any] | None = None | ||
| events: list[dict[str, Any]] | None = None | ||
| trace_id: str | None = None | ||
| span_id: str | None = None | ||
|
|
||
|
|
||
| def _value_to_python(value: Any) -> Any: | ||
| kind = value.WhichOneof("value") | ||
| if kind == "string_value": | ||
| return value.string_value | ||
| if kind == "int_value": | ||
| return value.int_value | ||
| if kind == "double_value": | ||
| return value.double_value | ||
| if kind == "bool_value": | ||
| return value.bool_value | ||
| if kind == "bytes_value": | ||
| return value.bytes_value | ||
| if kind == "array_value": | ||
| return [_value_to_python(item) for item in value.array_value.values] | ||
| if kind == "kvlist_value": | ||
| return {kv.key: _value_to_python(kv.value) for kv in value.kvlist_value.values} | ||
| return None | ||
|
|
||
|
|
||
| def attributes_to_dict(key_values: Iterable[Any]) -> dict[str, Any]: | ||
| return {key_value.key: _value_to_python(key_value.value) for key_value in key_values} | ||
|
|
||
|
|
||
| def events_to_list(events: Iterable[Any]) -> list[dict[str, Any]]: | ||
| return [ | ||
| { | ||
| "name": event.name, | ||
| "timestamp": event.time_unix_nano, | ||
| "attributes": attributes_to_dict(event.attributes), | ||
| } | ||
| for event in events | ||
| ] | ||
|
|
||
|
|
||
| class BaseTelemetryCollector: | ||
| def get_spans( | ||
| self, | ||
| expected_count: int | None = None, | ||
| timeout: float = 5.0, | ||
| poll_interval: float = 0.05, | ||
| ) -> tuple[Any, ...]: | ||
| import time | ||
|
|
||
| deadline = time.time() + timeout | ||
| min_count = expected_count if expected_count is not None else 1 | ||
| last_len: int | None = None | ||
| stable_iterations = 0 | ||
|
|
||
| while True: | ||
| spans = tuple(self._snapshot_spans()) | ||
|
|
||
| if len(spans) >= min_count: | ||
| if expected_count is not None and len(spans) >= expected_count: | ||
| return spans | ||
|
|
||
| if last_len == len(spans): | ||
| stable_iterations += 1 | ||
| if stable_iterations >= 2: | ||
| return spans | ||
| else: | ||
| stable_iterations = 1 | ||
| else: | ||
| stable_iterations = 0 | ||
|
|
||
| if time.time() >= deadline: | ||
| return spans | ||
|
|
||
| last_len = len(spans) | ||
| time.sleep(poll_interval) | ||
|
|
||
| def get_metrics(self) -> Any | None: | ||
| return self._snapshot_metrics() | ||
|
|
||
| def clear(self) -> None: | ||
| self._clear_impl() | ||
|
|
||
| def _snapshot_spans(self) -> tuple[Any, ...]: # pragma: no cover - interface hook | ||
| raise NotImplementedError | ||
|
|
||
| def _snapshot_metrics(self) -> Any | None: # pragma: no cover - interface hook | ||
| raise NotImplementedError | ||
|
|
||
| def _clear_impl(self) -> None: # pragma: no cover - interface hook | ||
| raise NotImplementedError | ||
|
|
||
| def shutdown(self) -> None: | ||
| """Optional hook for subclasses with background workers.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the terms described in the LICENSE file in | ||
| # the root directory of this source tree. | ||
|
|
||
| """In-memory telemetry collector for library-client tests.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| import opentelemetry.metrics as otel_metrics | ||
| import opentelemetry.trace as otel_trace | ||
| from opentelemetry import metrics, trace | ||
| from opentelemetry.sdk.metrics import MeterProvider | ||
| from opentelemetry.sdk.metrics.export import InMemoryMetricReader | ||
| from opentelemetry.sdk.trace import TracerProvider | ||
| from opentelemetry.sdk.trace.export import SimpleSpanProcessor | ||
| from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter | ||
|
|
||
| import llama_stack.core.telemetry.telemetry as telemetry_module | ||
|
|
||
| from .base import BaseTelemetryCollector, SpanStub | ||
|
|
||
|
|
||
| class InMemoryTelemetryCollector(BaseTelemetryCollector): | ||
| def __init__(self, span_exporter: InMemorySpanExporter, metric_reader: InMemoryMetricReader) -> None: | ||
| self._span_exporter = span_exporter | ||
| self._metric_reader = metric_reader | ||
|
|
||
| def _snapshot_spans(self) -> tuple[Any, ...]: | ||
| spans = [] | ||
| for span in self._span_exporter.get_finished_spans(): | ||
| trace_id = None | ||
| span_id = None | ||
| context = getattr(span, "context", None) | ||
| if context: | ||
| trace_id = f"{context.trace_id:032x}" | ||
| span_id = f"{context.span_id:016x}" | ||
| else: | ||
| trace_id = getattr(span, "trace_id", None) | ||
| span_id = getattr(span, "span_id", None) | ||
|
|
||
| stub = SpanStub( | ||
| span.name, | ||
| span.attributes, | ||
| getattr(span, "resource", None), | ||
| getattr(span, "events", None), | ||
| trace_id, | ||
| span_id, | ||
| ) | ||
| spans.append(stub) | ||
|
|
||
| return tuple(spans) | ||
|
|
||
| def _snapshot_metrics(self) -> Any | None: | ||
| data = self._metric_reader.get_metrics_data() | ||
| if data and data.resource_metrics: | ||
| resource_metric = data.resource_metrics[0] | ||
| if resource_metric.scope_metrics: | ||
| return resource_metric.scope_metrics[0].metrics | ||
| return None | ||
|
|
||
| def _clear_impl(self) -> None: | ||
| self._span_exporter.clear() | ||
| self._metric_reader.get_metrics_data() | ||
|
|
||
|
|
||
| class InMemoryTelemetryManager: | ||
| def __init__(self) -> None: | ||
| if hasattr(otel_trace, "_TRACER_PROVIDER_SET_ONCE"): | ||
| otel_trace._TRACER_PROVIDER_SET_ONCE._done = False # type: ignore[attr-defined] | ||
| if hasattr(otel_metrics, "_METER_PROVIDER_SET_ONCE"): | ||
| otel_metrics._METER_PROVIDER_SET_ONCE._done = False # type: ignore[attr-defined] | ||
|
|
||
| span_exporter = InMemorySpanExporter() | ||
| tracer_provider = TracerProvider() | ||
| tracer_provider.add_span_processor(SimpleSpanProcessor(span_exporter)) | ||
| trace.set_tracer_provider(tracer_provider) | ||
|
|
||
| metric_reader = InMemoryMetricReader() | ||
| meter_provider = MeterProvider(metric_readers=[metric_reader]) | ||
| metrics.set_meter_provider(meter_provider) | ||
|
|
||
| telemetry_module._TRACER_PROVIDER = tracer_provider | ||
|
|
||
| self.collector = InMemoryTelemetryCollector(span_exporter, metric_reader) | ||
| self._tracer_provider = tracer_provider | ||
| self._meter_provider = meter_provider | ||
|
|
||
| def shutdown(self) -> None: | ||
| telemetry_module._TRACER_PROVIDER = None | ||
| self._tracer_provider.shutdown() | ||
| self._meter_provider.shutdown() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you not need to run the docker with this additional port mapped or something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah I guess we run it with
--network host-- which of course doesn't quite work on a mac :)