-
Notifications
You must be signed in to change notification settings - Fork 13
feat: Setup metric provider for manual and auto-instrumented measurements #126
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
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
32 changes: 32 additions & 0 deletions
32
cloud_pipelines_backend/instrumentation/opentelemetry/_internal/temporality.py
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,32 @@ | ||
| """ | ||
| Maps string-based temporality configuration to OTel SDK types. | ||
| """ | ||
|
|
||
| from opentelemetry import metrics as otel_metrics | ||
| from opentelemetry.sdk import metrics as otel_sdk_metrics | ||
| from opentelemetry.sdk.metrics import export as otel_metrics_export | ||
|
|
||
| from cloud_pipelines_backend.instrumentation.opentelemetry._internal import ( | ||
| configuration, | ||
| ) | ||
|
|
||
| _TEMPORALITY_MAP = { | ||
| configuration.AggregationTemporality.DELTA: otel_metrics_export.AggregationTemporality.DELTA, | ||
| configuration.AggregationTemporality.CUMULATIVE: otel_metrics_export.AggregationTemporality.CUMULATIVE, | ||
| } | ||
|
|
||
|
|
||
| def build_preferred_temporality( | ||
| temporality: configuration.MetricsTemporalityConfig, | ||
| ) -> dict[type[otel_metrics.Instrument], otel_metrics_export.AggregationTemporality]: | ||
| return { | ||
| otel_sdk_metrics.Counter: _TEMPORALITY_MAP[temporality.counter], | ||
| otel_sdk_metrics.ObservableCounter: _TEMPORALITY_MAP[ | ||
| temporality.observable_counter | ||
| ], | ||
| otel_sdk_metrics.UpDownCounter: _TEMPORALITY_MAP[temporality.up_down_counter], | ||
| otel_sdk_metrics.ObservableUpDownCounter: _TEMPORALITY_MAP[ | ||
| temporality.observable_up_down_counter | ||
| ], | ||
| otel_sdk_metrics.Histogram: _TEMPORALITY_MAP[temporality.histogram], | ||
| } |
85 changes: 85 additions & 0 deletions
85
cloud_pipelines_backend/instrumentation/opentelemetry/metrics.py
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,85 @@ | ||
| """ | ||
| OpenTelemetry metrics configuration. | ||
|
|
||
| This module sets up the global meter provider with an OTLP exporter. | ||
| """ | ||
|
|
||
| import logging | ||
|
|
||
| from opentelemetry import metrics as otel_metrics | ||
| from opentelemetry.exporter.otlp.proto.grpc import ( | ||
| metric_exporter as otel_grpc_metric_exporter, | ||
| ) | ||
| from opentelemetry.exporter.otlp.proto.http import ( | ||
| metric_exporter as otel_http_metric_exporter, | ||
| ) | ||
| from opentelemetry.sdk import metrics as otel_sdk_metrics | ||
| from opentelemetry.sdk import resources as otel_resources | ||
| from opentelemetry.sdk.metrics import export as otel_metrics_export | ||
|
|
||
| from cloud_pipelines_backend.instrumentation.opentelemetry._internal import ( | ||
| configuration, | ||
| ) | ||
| from cloud_pipelines_backend.instrumentation.opentelemetry._internal import ( | ||
| temporality as temporality_mod, | ||
| ) | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def setup( | ||
| endpoint: str, | ||
| protocol: str, | ||
| service_name: str, | ||
| service_version: str | None = None, | ||
| temporality: configuration.MetricsTemporalityConfig | None = None, | ||
| ) -> None: | ||
yuechao-qin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| Configure the global OpenTelemetry meter provider. | ||
|
|
||
| Args: | ||
| endpoint: The OTLP collector endpoint URL. | ||
| protocol: The exporter protocol ("grpc" or "http"). | ||
| service_name: The service name reported to the collector. | ||
| service_version: The service version (e.g. git revision) reported to the collector. | ||
| temporality: Per-instrument aggregation temporality preferences. | ||
| """ | ||
| try: | ||
| _logger.info( | ||
| f"Configuring OpenTelemetry metrics, endpoint={endpoint}, " | ||
| f"protocol={protocol}, service_name={service_name}, " | ||
| f"service_version={service_version}" | ||
| ) | ||
|
|
||
| preferred_temporality = ( | ||
| temporality_mod.build_preferred_temporality(temporality) | ||
| if temporality | ||
| else None | ||
| ) | ||
|
|
||
| if protocol == configuration.ExporterProtocol.GRPC: | ||
| otel_exporter = otel_grpc_metric_exporter.OTLPMetricExporter( | ||
| endpoint=endpoint, | ||
| preferred_temporality=preferred_temporality, | ||
| ) | ||
| else: | ||
| otel_exporter = otel_http_metric_exporter.OTLPMetricExporter( | ||
| endpoint=endpoint, | ||
| preferred_temporality=preferred_temporality, | ||
| ) | ||
|
|
||
| attributes = {otel_resources.SERVICE_NAME: service_name} | ||
| if service_version: | ||
| attributes[otel_resources.SERVICE_VERSION] = service_version | ||
| resource = otel_resources.Resource.create(attributes) | ||
|
|
||
| reader = otel_metrics_export.PeriodicExportingMetricReader(otel_exporter) | ||
| meter_provider = otel_sdk_metrics.MeterProvider( | ||
| resource=resource, | ||
| metric_readers=[reader], | ||
| ) | ||
| otel_metrics.set_meter_provider(meter_provider) | ||
|
|
||
| _logger.info("OpenTelemetry metrics configured successfully.") | ||
| except Exception as e: | ||
| _logger.exception("Failed to configure OpenTelemetry metrics") | ||
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 |
|---|---|---|
| @@ -1,14 +1,18 @@ | ||
| import pytest | ||
| from opentelemetry import metrics as otel_metrics | ||
| from opentelemetry import trace | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def reset_otel_tracer_provider(): | ||
| """Reset the global OTel tracer provider between tests. | ||
| def reset_otel_providers(): | ||
| """Reset global OTel providers between tests. | ||
|
|
||
| OTel only allows set_tracer_provider to be called once per process. | ||
| We reset the internal guard so each test gets a clean slate. | ||
| OTel only allows set_tracer_provider / set_meter_provider to be called | ||
| once per process. We reset the internal guards so each test gets a | ||
| clean slate. | ||
| """ | ||
| yield | ||
| trace._TRACER_PROVIDER_SET_ONCE._done = False | ||
| trace._TRACER_PROVIDER = trace.ProxyTracerProvider() | ||
| otel_metrics._internal._METER_PROVIDER_SET_ONCE._done = False | ||
| otel_metrics._internal._METER_PROVIDER = None |
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.
Uh oh!
There was an error while loading. Please reload this page.