-
Notifications
You must be signed in to change notification settings - Fork 866
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
16 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 ba1f62e
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
anuraaga 4f52160
context manager
anuraaga dfbdd8a
defaults
anuraaga 243cdb7
Fixes
anuraaga 45b7f43
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
anuraaga 3c1de6b
Fix merge
anuraaga 8174777
Merge branch 'main' into exporter-metrics
lzchen 67d66dc
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
123 changes: 123 additions & 0 deletions
123
...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,123 @@ | ||
| # 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 | ||
|
herin049 marked this conversation as resolved.
|
||
| 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( | ||
|
xrmx marked this conversation as resolved.
Outdated
|
||
| 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.
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.