Skip to content
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

Update opentelemetry_metrics_exporter entrypoint to a MetricReader factory #3412

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Modify Prometheus exporter to translate non-monotonic Sums into Gauges
([#3306](https://github.com/open-telemetry/opentelemetry-python/pull/3306))
- Update `opentelemetry_metrics_exporter` entrypoint to a MetricReader factory
([#3412](https://github.com/open-telemetry/opentelemetry-python/pull/3412))



## Version 1.19.0/0.40b0 (2023-07-13)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test = [
otlp_proto_grpc = "opentelemetry.exporter.otlp.proto.grpc._log_exporter:OTLPLogExporter"

[project.entry-points.opentelemetry_metrics_exporter]
otlp_proto_grpc = "opentelemetry.exporter.otlp.proto.grpc.metric_exporter:OTLPMetricExporter"
otlp_proto_grpc = "opentelemetry.exporter.otlp.proto.grpc.metric_exporter:_otlp_metric_exporter_entrypoint"

[project.entry-points.opentelemetry_traces_exporter]
otlp_proto_grpc = "opentelemetry.exporter.otlp.proto.grpc.trace_exporter:OTLPSpanExporter"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@
Metric,
MetricExporter,
MetricExportResult,
MetricReader,
MetricsData,
PeriodicExportingMetricReader,
ResourceMetrics,
ScopeMetrics,
)
Expand Down Expand Up @@ -102,7 +104,6 @@ def __init__(
preferred_aggregation: Dict[type, Aggregation] = None,
max_export_batch_size: Optional[int] = None,
):

if insecure is None:
insecure = environ.get(OTEL_EXPORTER_OTLP_METRICS_INSECURE)
if insecure is not None:
Expand Down Expand Up @@ -260,3 +261,8 @@ def _exporting(self) -> str:
def force_flush(self, timeout_millis: float = 10_000) -> bool:
"""Nothing is buffered in this exporter, so this method does nothing."""
return True


def _otlp_metric_exporter_entrypoint() -> MetricReader:
"""Implementation of opentelemetry_metrics_exporter entrypoint"""
return PeriodicExportingMetricReader(OTLPMetricExporter())
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 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 unittest import TestCase

from opentelemetry.exporter.otlp.proto.grpc._log_exporter import (
OTLPLogExporter,
)
from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import (
_otlp_metric_exporter_entrypoint,
)
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import (
OTLPSpanExporter,
)
from opentelemetry.sdk._configuration import _import_exporters


class TestEntrypoints(TestCase):
def test_import_exporters(self) -> None:
"""
Tests that the entrypoints can be loaded and don't have a typo in the names
"""
(
trace_exporters,
metric_exporters,
logs_exporters,
) = _import_exporters(
trace_exporter_names=["otlp_proto_grpc"],
metric_exporter_names=["otlp_proto_grpc"],
log_exporter_names=["otlp_proto_grpc"],
)

self.assertEqual(
trace_exporters,
{"otlp_proto_grpc": OTLPSpanExporter},
)
self.assertEqual(
metric_exporters,
{"otlp_proto_grpc": _otlp_metric_exporter_entrypoint},
)
self.assertEqual(
logs_exporters,
{"otlp_proto_grpc": OTLPLogExporter},
)
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test = [
otlp_proto_http = "opentelemetry.exporter.otlp.proto.http.trace_exporter:OTLPSpanExporter"

[project.entry-points.opentelemetry_metrics_exporter]
otlp_proto_http = "opentelemetry.exporter.otlp.proto.http.metric_exporter:OTLPMetricExporter"
otlp_proto_http = "opentelemetry.exporter.otlp.proto.http.metric_exporter:_otlp_metric_exporter_entrypoint"

[project.entry-points.opentelemetry_logs_exporter]
otlp_proto_http = "opentelemetry.exporter.otlp.proto.http._log_exporter:OTLPLogExporter"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@
AggregationTemporality,
MetricExporter,
MetricExportResult,
MetricReader,
MetricsData,
PeriodicExportingMetricReader,
)
from opentelemetry.sdk.metrics.export import ( # noqa: F401
Gauge,
Expand Down Expand Up @@ -101,7 +103,6 @@ def _expo(*args, **kwargs):


class OTLPMetricExporter(MetricExporter, OTLPMetricExporterMixin):

_MAX_RETRY_TIMEOUT = 64

def __init__(
Expand Down Expand Up @@ -182,7 +183,6 @@ def export(
) -> MetricExportResult:
serialized_data = encode_metrics(metrics_data)
for delay in _expo(max_value=self._MAX_RETRY_TIMEOUT):

if delay == self._MAX_RETRY_TIMEOUT:
return MetricExportResult.FAILURE

Expand Down Expand Up @@ -247,3 +247,8 @@ def _append_metrics_path(endpoint: str) -> str:
if endpoint.endswith("/"):
return endpoint + DEFAULT_METRICS_EXPORT_PATH
return endpoint + f"/{DEFAULT_METRICS_EXPORT_PATH}"


def _otlp_metric_exporter_entrypoint() -> MetricReader:
"""Implementation of opentelemetry_metrics_exporter entrypoint"""
return PeriodicExportingMetricReader(OTLPMetricExporter())
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 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 unittest import TestCase

from opentelemetry.exporter.otlp.proto.http._log_exporter import (
OTLPLogExporter,
)
from opentelemetry.exporter.otlp.proto.http.metric_exporter import (
_otlp_metric_exporter_entrypoint,
)
from opentelemetry.exporter.otlp.proto.http.trace_exporter import (
OTLPSpanExporter,
)
from opentelemetry.sdk._configuration import _import_exporters


class TestEntrypoints(TestCase):
def test_import_exporters(self) -> None:
"""
Tests that the entrypoints can be loaded and don't have a typo in the names
"""
(
trace_exporters,
metric_exporters,
logs_exporters,
) = _import_exporters(
trace_exporter_names=["otlp_proto_http"],
metric_exporter_names=["otlp_proto_http"],
log_exporter_names=["otlp_proto_http"],
)

self.assertEqual(
trace_exporters,
{"otlp_proto_http": OTLPSpanExporter},
)
self.assertEqual(
metric_exporters,
{"otlp_proto_http": _otlp_metric_exporter_entrypoint},
)
self.assertEqual(
logs_exporters,
{"otlp_proto_http": OTLPLogExporter},
)
2 changes: 1 addition & 1 deletion exporter/opentelemetry-exporter-otlp/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies = [
otlp = "opentelemetry.exporter.otlp.proto.grpc._log_exporter:OTLPLogExporter"

[project.entry-points.opentelemetry_metrics_exporter]
otlp = "opentelemetry.exporter.otlp.proto.grpc.metric_exporter:OTLPMetricExporter"
otlp = "opentelemetry.exporter.otlp.proto.grpc.metric_exporter:_otlp_metric_exporter_entrypoint"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Can we not have "entrypoint" in the factory method name? Replace it with "factory" should be good to represent what it actually is.


[project.entry-points.opentelemetry_traces_exporter]
otlp = "opentelemetry.exporter.otlp.proto.grpc.trace_exporter:OTLPSpanExporter"
Expand Down
55 changes: 55 additions & 0 deletions exporter/opentelemetry-exporter-otlp/tests/test_entrypoints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 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 unittest import TestCase

from opentelemetry.exporter.otlp.proto.grpc._log_exporter import (
OTLPLogExporter,
)
from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import (
_otlp_metric_exporter_entrypoint,
)
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import (
OTLPSpanExporter,
)
from opentelemetry.sdk._configuration import _import_exporters


class TestEntrypoints(TestCase):
def test_import_exporters(self) -> None:
"""
Tests that the entrypoints can be loaded and don't have a typo in the names
"""
(
trace_exporters,
metric_exporters,
logs_exporters,
) = _import_exporters(
trace_exporter_names=["otlp"],
metric_exporter_names=["otlp"],
log_exporter_names=["otlp"],
)

self.assertEqual(
trace_exporters,
{"otlp": OTLPSpanExporter},
)
self.assertEqual(
metric_exporters,
{"otlp": _otlp_metric_exporter_entrypoint},
)
self.assertEqual(
logs_exporters,
{"otlp": OTLPLogExporter},
)
2 changes: 1 addition & 1 deletion opentelemetry-sdk/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ console = "opentelemetry.sdk._logs.export:ConsoleLogExporter"
sdk_meter_provider = "opentelemetry.sdk.metrics:MeterProvider"

[project.entry-points.opentelemetry_metrics_exporter]
console = "opentelemetry.sdk.metrics.export:ConsoleMetricExporter"
console = "opentelemetry.sdk.metrics._internal.export:_console_metric_exporter_entrypoint"

[project.entry-points.opentelemetry_tracer_provider]
sdk_tracer_provider = "opentelemetry.sdk.trace:TracerProvider"
Expand Down
Loading