-
Notifications
You must be signed in to change notification settings - Fork 833
feat(sdk): implement exporter metrics #4976
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
Open
anuraaga
wants to merge
9
commits into
open-telemetry:main
Choose a base branch
from
anuraaga:exporter-metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
43190d3
feat(sdk): implement exporter metrics
anuraaga 83b8af1
changelog
anuraaga d867d48
Lint
anuraaga 0fd15af
All optional
anuraaga 223b516
Fix
anuraaga 9e80c6a
Cleanup
anuraaga 9de118e
Update HTTP exporters
anuraaga f6de494
Format
anuraaga 19a5d8c
Merge branch 'main' into exporter-metrics
xrmx 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
125 changes: 125 additions & 0 deletions
125
...orter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_exporter_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,125 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections import Counter | ||
| from time import perf_counter | ||
| from typing import TYPE_CHECKING, Callable | ||
|
|
||
| from opentelemetry.metrics import MeterProvider, get_meter_provider | ||
| from opentelemetry.semconv._incubating.attributes.otel_attributes import ( | ||
| OTEL_COMPONENT_NAME, | ||
| OTEL_COMPONENT_TYPE, | ||
| OtelComponentTypeValues, | ||
| ) | ||
| from opentelemetry.semconv._incubating.metrics.otel_metrics import ( | ||
| create_otel_sdk_exporter_log_exported, | ||
| create_otel_sdk_exporter_log_inflight, | ||
| create_otel_sdk_exporter_metric_data_point_exported, | ||
| create_otel_sdk_exporter_metric_data_point_inflight, | ||
| create_otel_sdk_exporter_operation_duration, | ||
| create_otel_sdk_exporter_span_exported, | ||
| create_otel_sdk_exporter_span_inflight, | ||
| ) | ||
| from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE | ||
| from opentelemetry.semconv.attributes.server_attributes import ( | ||
| SERVER_ADDRESS, | ||
| SERVER_PORT, | ||
| ) | ||
|
|
||
| if TYPE_CHECKING: | ||
| from typing import Literal | ||
| from urllib.parse import ParseResult as UrlParseResult | ||
|
|
||
| from opentelemetry.util.types import Attributes, AttributeValue | ||
|
|
||
| _component_counter = Counter() | ||
|
|
||
|
|
||
| class ExporterMetrics: | ||
| def __init__( | ||
| self, | ||
| component_type: OtelComponentTypeValues | None, | ||
| signal: Literal["traces", "metrics", "logs"], | ||
| endpoint: UrlParseResult, | ||
| meter_provider: MeterProvider | None, | ||
| ) -> None: | ||
| if signal == "traces": | ||
| create_exported = create_otel_sdk_exporter_span_exported | ||
| create_inflight = create_otel_sdk_exporter_span_inflight | ||
| elif signal == "logs": | ||
| create_exported = create_otel_sdk_exporter_log_exported | ||
| create_inflight = create_otel_sdk_exporter_log_inflight | ||
| else: | ||
| create_exported = ( | ||
| create_otel_sdk_exporter_metric_data_point_exported | ||
| ) | ||
| create_inflight = ( | ||
| create_otel_sdk_exporter_metric_data_point_inflight | ||
| ) | ||
|
|
||
| port = endpoint.port | ||
| if port is None: | ||
| if endpoint.scheme == "https": | ||
| port = 443 | ||
| elif endpoint.scheme == "http": | ||
| port = 80 | ||
|
|
||
| component_type = ( | ||
| component_type or OtelComponentTypeValues("unknown_otlp_exporter") | ||
| ).value | ||
| count = _component_counter[component_type] | ||
| _component_counter[component_type] = count + 1 | ||
| self._standard_attrs: dict[str, AttributeValue] = { | ||
| OTEL_COMPONENT_TYPE: component_type, | ||
| OTEL_COMPONENT_NAME: f"{component_type}/{count}", | ||
| } | ||
| if endpoint.hostname: | ||
| self._standard_attrs[SERVER_ADDRESS] = endpoint.hostname | ||
| if port is not None: | ||
| self._standard_attrs[SERVER_PORT] = port | ||
|
|
||
| meter_provider = meter_provider or get_meter_provider() | ||
| meter = meter_provider.get_meter("opentelemetry-sdk") | ||
| self._inflight = create_inflight(meter) | ||
| self._exported = create_exported(meter) | ||
| self._duration = create_otel_sdk_exporter_operation_duration(meter) | ||
|
|
||
| def start_export( | ||
| self, num_items: int | ||
| ) -> Callable[[Exception | None, Attributes], None]: | ||
| start_time = perf_counter() | ||
| self._inflight.add(num_items, self._standard_attrs) | ||
|
|
||
| def finish_export( | ||
| error: Exception | None, | ||
| error_attrs: Attributes, | ||
| ): | ||
| end_time = perf_counter() | ||
| self._inflight.add(-num_items, self._standard_attrs) | ||
| exported_attrs = ( | ||
| {**self._standard_attrs, ERROR_TYPE: type(error).__qualname__} | ||
| if error | ||
| else self._standard_attrs | ||
| ) | ||
| self._exported.add(num_items, exported_attrs) | ||
| duration_attrs = ( | ||
| {**exported_attrs, **error_attrs} | ||
| if error_attrs | ||
| else exported_attrs | ||
| ) | ||
| self._duration.record(end_time - start_time, duration_attrs) | ||
|
|
||
| return finish_export | ||
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
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.
Could we instead just have three methods
on_start,on_endandon_error(similar to the logging implementation) and use an instance variable orContextVarfor tracking the duration?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.
Not sure if that would simplify things, especially since exceptions are suppressed by the exporter implementations (otherwise a context manager would work great).
ContextVaris good for black-box code but seems unnecessary when we are instrumenting the SDK itself. Could return a class, but it seems like it would be very similar to theCallable. Can do that if it seems better though.Uh oh!
There was an error while loading. Please reload this page.
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.
Does using a context manager as a mutable object work to record error info? We can use
finallyto do metric stuff afterreturnis called inexport.and then in
ExporterMetrics:start_export->finish_exportwould probably work in practice because the operations infinishare fine to be left dangling if we don't call them in every case (like if exporter shutdown during export) but design wise it seems dangerous to me.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.
Thanks for the idea! I'll give that a try