Skip to content
Open
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ develop: install
pre-commit install

install: clean
$(PIP) install -e ."[api, datadog, prometheus, elasticsearch, opensearch, splunk, pubsub, cloud_monitoring, bigquery, dev]"
$(PIP) install -e ."[api, datadog, prometheus, elasticsearch, opensearch, splunk, pubsub, cloud_monitoring, bigquery, opentelemetry, dev]"

uninstall: clean
$(PIP) freeze --exclude-editable | xargs $(PIP) uninstall -y
Expand Down
34 changes: 34 additions & 0 deletions docs/providers/opentelemetry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# OpenTelemetry

## Exporter

The `opentelemetry` exporter allows to export SLO metrics via [OTLP gRPC](https://opentelemetry.io/docs/specs/otlp/) to any OpenTelemetry-compatible backend (e.g., Grafana Mimir, Datadog, New Relic, Jaeger, etc.).

**Installation:**

```sh
pip install slo-generator[opentelemetry]
```

**Config example:**

```yaml
exporters:
opentelemetry:
endpoint: ${OTLP_ENDPOINT}
headers:
Authorization: Bearer ${OTLP_TOKEN}
insecure: false
```

**Required fields:**

* `endpoint`: OTLP gRPC endpoint (e.g., `localhost:4317`).

**Optional fields:**

* `headers`: `dict` - Headers to send with each request (e.g., for authentication).
* `insecure`: `bool` - Use insecure (non-TLS) connection. Defaults to `false`.
* `metrics`: `list` - List of metrics to export ([see docs](../shared/metrics.md)).

All SLO metrics are exported as gauges with the `slo_` prefix (e.g., `slo_error_budget_burn_rate`). SLO report labels are mapped directly to OpenTelemetry metric attributes.
5 changes: 5 additions & 0 deletions samples/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ exporters:
pubsub:
project_id: ${PUBSUB_PROJECT_ID}
topic_name: ${PUBSUB_TOPIC_NAME}
opentelemetry:
endpoint: ${OTLP_ENDPOINT}
headers:
Authorization: Bearer ${OTLP_TOKEN}
insecure: false
prometheus_self: { }

error_budget_policies:
Expand Down
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ cloud_storage =
google-cloud-storage
elasticsearch =
elasticsearch
opentelemetry =
opentelemetry-api
opentelemetry-sdk
opentelemetry-exporter-otlp-proto-grpc
opensearch =
opensearch-py
splunk =
Expand Down
69 changes: 69 additions & 0 deletions slo_generator/exporters/opentelemetry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Copyright 2024 Google Inc.
#
# 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.
"""
`opentelemetry.py`
OpenTelemetry exporter class.
"""

import logging

from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporter
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
from opentelemetry.sdk.resources import Resource

from .base import MetricsExporter

LOGGER = logging.getLogger(__name__)


class OpenTelemetryExporter(MetricsExporter):
"""OpenTelemetry exporter class."""

REQUIRED_FIELDS = ["endpoint"]
OPTIONAL_FIELDS = ["headers", "insecure"]
METRIC_PREFIX = "slo_"

def export_metric(self, data):
"""Export data via OpenTelemetry OTLP.

Args:
data (dict): Metric data.
"""
endpoint = data["endpoint"]
headers = data.get("headers")
insecure = data.get("insecure", False)

name = data["name"]
description = data["description"]
value = data["value"]
labels = data["labels"]

exporter_kwargs = {"endpoint": endpoint, "insecure": insecure}
if headers:
exporter_kwargs["headers"] = list(headers.items())

otlp_exporter = OTLPMetricExporter(**exporter_kwargs)
reader = PeriodicExportingMetricReader(
otlp_exporter, export_interval_millis=1_000_000
)
resource = Resource.create({"service.name": "slo-generator"})
provider = MeterProvider(resource=resource, metric_readers=[reader])
meter = provider.get_meter("slo-generator")

gauge = meter.create_gauge(name=name, description=description)
gauge.set(value, attributes=labels)

provider.force_flush()
provider.shutdown()
4 changes: 4 additions & 0 deletions tests/unit/fixtures/exporters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@
- good_events_count
- bad_events_count

- class: OpenTelemetry
endpoint: localhost:4317
insecure: true

- class: PrometheusSelf
9 changes: 8 additions & 1 deletion tests/unit/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,19 @@ def test_export_prometheus(self, mock):
export(SLO_REPORT, EXPORTERS[3])

def test_export_prometheus_self(self):
export(SLO_REPORT, EXPORTERS[7])
export(SLO_REPORT, EXPORTERS[8])

@patch.object(Metric, "send", mock_dd_metric_send)
def test_export_datadog(self):
export(SLO_REPORT, EXPORTERS[4])

@patch(
"slo_generator.exporters.opentelemetry.OTLPMetricExporter",
return_value=MagicMock(),
)
def test_export_opentelemetry(self, mock):
export(SLO_REPORT, EXPORTERS[7])

@patch.object(DynatraceClient, "request", side_effect=mock_dt)
def test_export_dynatrace(self, mock):
export(SLO_REPORT, EXPORTERS[5])
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/test_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
"SPLUNK_USER": "fake",
"SPLUNK_PWD": "fake",
"OPENSEARCH_URL": "http://localhost:9201",
"OTLP_ENDPOINT": "localhost:4317",
"OTLP_TOKEN": "fake",
}


Expand Down
Loading