Skip to content

Commit

Permalink
Update opentelemetry_metrics_exporter entrypoint to a MetricReader fa…
Browse files Browse the repository at this point in the history
…ctory
  • Loading branch information
aabmass committed Aug 24, 2023
1 parent d8490c5 commit 4c079fe
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 38 deletions.
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"

[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

0 comments on commit 4c079fe

Please sign in to comment.