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

exporter-otlp: adding metrics exporter structure #2323

Merged
Merged
Show file tree
Hide file tree
Changes from 11 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Decode URL-encoded headers in environment variables
([#2312](https://github.com/open-telemetry/opentelemetry-python/pull/2312))
- [exporter/opentelemetry-exporter-otlp-proto-grpc] Add OTLPMetricExporter
([#2323](https://github.com/open-telemetry/opentelemetry-python/pull/2323))

## [1.8.0-0.27b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.8.0-0.27b0) - 2021-12-17

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@
# See the License for the specific language governing permissions and
# limitations under the License.


class MetricExporter:
pass
# metrics.py
ocelotl marked this conversation as resolved.
Show resolved Hide resolved
# TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# 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 typing import Optional, Sequence
from grpc import ChannelCredentials, Compression
from opentelemetry.exporter.otlp.proto.grpc.exporter import (
OTLPExporterMixin,
)
from opentelemetry.proto.collector.metrics.v1.metrics_service_pb2_grpc import (
MetricsServiceStub,
)
from opentelemetry.sdk._metrics.data import (
MetricData,
)

from opentelemetry.sdk._metrics.export import (
MetricExporter,
MetricExportResult,
)


class OTLPMetricExporter(
MetricExporter,
OTLPExporterMixin,
codeboten marked this conversation as resolved.
Show resolved Hide resolved
):
_result = MetricExportResult
_stub = MetricsServiceStub

def __init__(
self,
endpoint: Optional[str] = None,
insecure: Optional[bool] = None,
credentials: Optional[ChannelCredentials] = None,
headers: Optional[Sequence] = None,
timeout: Optional[int] = None,
compression: Optional[Compression] = None,
):
super().__init__(
**{
"endpoint": endpoint,
"insecure": insecure,
"credentials": credentials,
"headers": headers,
"timeout": timeout,
"compression": compression,
}
ocelotl marked this conversation as resolved.
Show resolved Hide resolved
)

def _translate_data(
self, data: Sequence[MetricData]
) -> MetricExportResult:
return super()._translate_data(data)

def export(self, batch: Sequence[MetricData]) -> MetricExportResult:
for data in batch:
# TODO: do something with the data
pass
return MetricExportResult.SUCCESS

def shutdown(self):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright The OpenTelemetry Authors
codeboten marked this conversation as resolved.
Show resolved Hide resolved
#
# 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 google.protobuf.duration_pb2 import Duration
from google.rpc.error_details_pb2 import RetryInfo
from grpc import StatusCode

from opentelemetry.proto.collector.metrics.v1.metrics_service_pb2 import (
ExportMetricsServiceResponse,
)
from opentelemetry.proto.collector.metrics.v1.metrics_service_pb2_grpc import (
MetricsServiceServicer,
)


class MetricsServiceServicerUNAVAILABLEDelay(MetricsServiceServicer):
# pylint: disable=invalid-name,unused-argument,no-self-use
def Export(self, request, context):
context.set_code(StatusCode.UNAVAILABLE)

context.send_initial_metadata(
(("google.rpc.retryinfo-bin", RetryInfo().SerializeToString()),)
)
context.set_trailing_metadata(
(
(
"google.rpc.retryinfo-bin",
RetryInfo(
retry_delay=Duration(seconds=4)
).SerializeToString(),
),
)
)

return ExportMetricsServiceResponse()


class MetricsServiceServicerUNAVAILABLE(MetricsServiceServicer):
# pylint: disable=invalid-name,unused-argument,no-self-use
def Export(self, request, context):
context.set_code(StatusCode.UNAVAILABLE)

return ExportMetricsServiceResponse()


class MetricsServiceServicerSUCCESS(MetricsServiceServicer):
# pylint: disable=invalid-name,unused-argument,no-self-use
def Export(self, request, context):
context.set_code(StatusCode.OK)

return ExportMetricsServiceResponse()


class MetricsServiceServicerALREADY_EXISTS(MetricsServiceServicer):
# pylint: disable=invalid-name,unused-argument,no-self-use
def Export(self, request, context):
context.set_code(StatusCode.ALREADY_EXISTS)

return ExportMetricsServiceResponse()
10 changes: 9 additions & 1 deletion exporter/opentelemetry-exporter-otlp/tests/test_otlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
from opentelemetry.exporter.otlp.proto.grpc._log_exporter import (
OTLPLogExporter,
)
from opentelemetry.exporter.otlp.proto.grpc._metric_exporter import (
OTLPMetricExporter,
)
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import (
OTLPSpanExporter,
)
Expand All @@ -27,7 +30,12 @@

class TestOTLPExporters(unittest.TestCase):
def test_constructors(self):
for exporter in [OTLPSpanExporter, HTTPSpanExporter, OTLPLogExporter]:
for exporter in [
OTLPSpanExporter,
HTTPSpanExporter,
OTLPLogExporter,
OTLPMetricExporter,
]:
try:
exporter()
except Exception: # pylint: disable=broad-except
Expand Down
2 changes: 2 additions & 0 deletions opentelemetry-sdk/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ opentelemetry_log_emitter_provider =
sdk_log_emitter_provider = opentelemetry.sdk._logs:LogEmitterProvider
opentelemetry_logs_exporter =
console = opentelemetry.sdk._logs.export:ConsoleLogExporter
opentelemetry_metrics_exporter =
console = opentelemetry.sdk._metrics.export:ConsoleMetricExporter
opentelemetry_id_generator =
random = opentelemetry.sdk.trace.id_generator:RandomIdGenerator
opentelemetry_environment_variables =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
ObservableUpDownCounter as APIObservableUpDownCounter,
)
from opentelemetry._metrics.instrument import UpDownCounter as APIUpDownCounter
from opentelemetry.sdk._metrics.export.metric_exporter import MetricExporter
from opentelemetry.sdk._metrics.export import MetricExporter
from opentelemetry.sdk._metrics.instrument import (
Counter,
Histogram,
Expand Down
17 changes: 17 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/_metrics/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 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.


class MetricData:
"""TODO"""
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,71 @@
# 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.

import abc
import sys
codeboten marked this conversation as resolved.
Show resolved Hide resolved
from enum import Enum
from os import linesep
from typing import IO, Callable, Sequence

from opentelemetry.sdk._metrics.data import MetricData
from opentelemetry.sdk._metrics.measurement import Measurement


class MetricExportResult(Enum):
SUCCESS = 0
FAILURE = 1


class MetricExporter(abc.ABC):
"""Interface for exporting metrics.

Interface to be implemented by services that want to export metrics received
in their own format.
"""

def export(self, metrics: Sequence[MetricData]) -> "MetricExportResult":
"""Exports a batch of telemetry data.

Args:
metrics: The list of `opentelemetry.trace.Span` objects to be exported
codeboten marked this conversation as resolved.
Show resolved Hide resolved

Returns:
The result of the export
"""

@abc.abstractmethod
def shutdown(self) -> None:
"""Shuts down the exporter.

Called when the SDK is shut down.
"""


class ConsoleMetricExporter(MetricExporter):
"""Implementation of :class:`MetricExporter` that prints metrics to the
console.

This class can be used for diagnostic purposes. It prints the exported
metrics to the console STDOUT.
"""

def __init__(
self,
out: IO = sys.stdout,
formatter: Callable[
[Measurement], str
codeboten marked this conversation as resolved.
Show resolved Hide resolved
] = lambda record: record.to_json()
codeboten marked this conversation as resolved.
Show resolved Hide resolved
+ linesep,
):
self.out = out
self.formatter = formatter

def export(self, metrics: Sequence[MetricData]) -> MetricExportResult:
for data in metrics:
self.out.write(self.formatter(data.log_record))
codeboten marked this conversation as resolved.
Show resolved Hide resolved
self.out.flush()
return MetricExportResult.SUCCESS

def shutdown(self) -> None:
pass
5 changes: 5 additions & 0 deletions opentelemetry-sdk/tests/test_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
_init_tracing,
)
from opentelemetry.sdk._logs.export import ConsoleLogExporter
from opentelemetry.sdk._metrics.export import ConsoleMetricExporter
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
from opentelemetry.sdk.trace.id_generator import IdGenerator, RandomIdGenerator
Expand Down Expand Up @@ -195,3 +196,7 @@ def test_console_exporters(self):
self.assertEqual(
logs_exporters["console"].__class__, ConsoleLogExporter.__class__
)
self.assertEqual(
logs_exporters["console"].__class__,
ConsoleMetricExporter.__class__,
)