Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Commit

Permalink
Polish before release (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
pirgeo authored Aug 25, 2022
1 parent c277296 commit bef7fbd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 66 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ pip install opentelemetry-exporter-dynatrace-metrics
The general setup of OpenTelemetry Python is explained in the official [Getting Started Guide](https://open-telemetry.github.io/opentelemetry-python/getting-started.html#add-metrics).

```python

# setup metrics export pipeline
metrics.set_meter_provider(MeterProvider(
# configure Exporter/MetricReader combination with a 5000ms export
# interval, endpoint url and API token.
metric_readers=[configure_dynatrace_metrics_export(
# configure Exporter/MetricReader combination with a 5000ms export
# interval, endpoint url and API token.
metric_readers=[
configure_dynatrace_metrics_export(
export_interval_millis=5000,
endpoint_url=endpoint_url,
api_token=api_token)
]))
]))

# get a meter
meter = metrics.get_meter(__name__)
Expand Down
35 changes: 11 additions & 24 deletions src/dynatrace/opentelemetry/metrics/export/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,24 @@

from typing import Mapping, Optional

import opentelemetry.sdk.metrics as metrics
from dynatrace.opentelemetry.metrics.export._exporter import (
_DynatraceMetricsExporter,
)
from opentelemetry.sdk.metrics.export import (
AggregationTemporality,
PeriodicExportingMetricReader,
MetricReader
)

from dynatrace.opentelemetry.metrics.export._exporter import (
_DynatraceMetricsExporter
MetricReader,
)

VERSION = "0.3.0-rc2"

_DYNATRACE_TEMPORALITY_PREFERENCE = {
metrics.Counter: AggregationTemporality.DELTA,
metrics.UpDownCounter: AggregationTemporality.CUMULATIVE,
metrics.Histogram: AggregationTemporality.DELTA,
metrics.ObservableCounter: AggregationTemporality.DELTA,
metrics.ObservableUpDownCounter: AggregationTemporality.CUMULATIVE,
metrics.ObservableGauge: AggregationTemporality.CUMULATIVE,
}
VERSION = "0.3.0"


def configure_dynatrace_metrics_export(
endpoint_url: Optional[str] = None,
api_token: Optional[str] = None,
prefix: Optional[str] = None,
default_dimensions: Optional[Mapping[str, str]] = None,
export_dynatrace_metadata: Optional[bool] = False,
export_interval_millis: Optional[float] = None
endpoint_url: Optional[str] = None,
api_token: Optional[str] = None,
prefix: Optional[str] = None,
default_dimensions: Optional[Mapping[str, str]] = None,
export_dynatrace_metadata: Optional[bool] = False,
export_interval_millis: Optional[float] = None
) -> MetricReader:
"""
Configures and creates a PeriodicExportingMetricReader and
Expand Down Expand Up @@ -91,6 +79,5 @@ def configure_dynatrace_metrics_export(
prefix=prefix,
default_dimensions=default_dimensions,
export_dynatrace_metadata=export_dynatrace_metadata,
preferred_temporality=_DYNATRACE_TEMPORALITY_PREFERENCE,
)
)
48 changes: 28 additions & 20 deletions src/dynatrace/opentelemetry/metrics/export/_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,36 @@
# limitations under the License.

import logging
from typing import Mapping, Optional, Dict
from typing import Mapping, Optional

import requests
from dynatrace.metric.utils import (
DynatraceMetricsSerializer,
MetricError, DynatraceMetricsApiConstants
MetricError,
DynatraceMetricsApiConstants,
)
from dynatrace.opentelemetry.metrics.export._factory import (
_OTelDynatraceMetricsFactory,
)
from opentelemetry.sdk.metrics.export import (
MetricExporter,
MetricExportResult,
MetricsData
MetricsData,
)
from opentelemetry.sdk.metrics._internal.aggregation import (

import opentelemetry.sdk.metrics as metrics
from opentelemetry.sdk.metrics.export import (
AggregationTemporality,
)
from opentelemetry.sdk.metrics.view import Aggregation

from dynatrace.opentelemetry.metrics.export._factory import (
_OTelDynatraceMetricsFactory
)
_DYNATRACE_TEMPORALITY_PREFERENCE = {
metrics.Counter: AggregationTemporality.DELTA,
metrics.UpDownCounter: AggregationTemporality.CUMULATIVE,
metrics.Histogram: AggregationTemporality.DELTA,
metrics.ObservableCounter: AggregationTemporality.DELTA,
metrics.ObservableUpDownCounter: AggregationTemporality.CUMULATIVE,
metrics.ObservableGauge: AggregationTemporality.CUMULATIVE,
}


class _DynatraceMetricsExporter(MetricExporter):
Expand All @@ -45,19 +55,17 @@ class _DynatraceMetricsExporter(MetricExporter):
"""

def __init__(
self,
endpoint_url: Optional[str] = None,
api_token: Optional[str] = None,
prefix: Optional[str] = None,
default_dimensions: Optional[Mapping[str, str]] = None,
export_dynatrace_metadata: Optional[bool] = False,
preferred_temporality: Dict[type, AggregationTemporality] = None,
preferred_aggregation: Dict[
type, Aggregation
] = None,
self,
endpoint_url: Optional[str] = None,
api_token: Optional[str] = None,
prefix: Optional[str] = None,
default_dimensions: Optional[Mapping[str, str]] = None,
export_dynatrace_metadata: Optional[bool] = False,
):
super().__init__(preferred_temporality=preferred_temporality,
preferred_aggregation=preferred_aggregation)
super().__init__(
preferred_temporality=_DYNATRACE_TEMPORALITY_PREFERENCE,
preferred_aggregation=None
)
self.__logger = logging.getLogger(__name__)

if endpoint_url:
Expand Down
27 changes: 10 additions & 17 deletions test/test_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@
# 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 math
import re
import unittest
from unittest import mock

import requests

from typing import Sequence, Union
from unittest import mock
from unittest.mock import patch

import requests
from dynatrace.opentelemetry.metrics.export import (
_DynatraceMetricsExporter,
configure_dynatrace_metrics_export,
)
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.view import View
from opentelemetry.sdk.metrics.export import (
Metric,
MetricExportResult,
Expand All @@ -35,18 +37,13 @@
DataT,
ResourceMetrics,
ScopeMetrics,
HistogramDataPoint
HistogramDataPoint,
)
from opentelemetry.sdk.metrics.view import View
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
from parameterized import parameterized

from dynatrace.opentelemetry.metrics.export import (
_DynatraceMetricsExporter,
_DYNATRACE_TEMPORALITY_PREFERENCE,
configure_dynatrace_metrics_export
)


class AnyStringMatching(str):
def __eq__(self, other):
Expand Down Expand Up @@ -490,9 +487,7 @@ def test_multiple_records(self, mock_post):
def test_view(self, mock_post):
mock_post.return_value = self._get_session_response()

exporter = _DynatraceMetricsExporter(
preferred_temporality=_DYNATRACE_TEMPORALITY_PREFERENCE,
)
exporter = _DynatraceMetricsExporter()

metric_reader = PeriodicExportingMetricReader(
export_interval_millis=3600000,
Expand Down Expand Up @@ -536,7 +531,6 @@ def test_configuration_default(self):
prefix=None,
default_dimensions=None,
export_dynatrace_metadata=False,
preferred_temporality=_DYNATRACE_TEMPORALITY_PREFERENCE,
)
mock_reader.assert_called_once_with(
export_interval_millis=None,
Expand Down Expand Up @@ -568,7 +562,6 @@ def test_configuration_custom(self):
prefix="otel.python.test",
default_dimensions={"defaultKey": "defaultValue"},
export_dynatrace_metadata=True,
preferred_temporality=_DYNATRACE_TEMPORALITY_PREFERENCE,
)
mock_reader.assert_called_once_with(
export_interval_millis=100,
Expand Down

0 comments on commit bef7fbd

Please sign in to comment.