Skip to content

Commit

Permalink
feat: Use OTel distro class to configure SDK - allow both programatic…
Browse files Browse the repository at this point in the history
… and auto-configure hooks (#22)

## Which problem is this PR solving?
Updates the distro to introduce a the HoneycombDistro class which
introduces the hook for auto configuration via BaseDistro. Also exposes
the `configure_opentelemetry` function that can be used programatically
to configure the SDK.

- Closes #10 

## Short description of the changes
- Adds new HoneycombDistro class that depends on BaseDistro
- Adds `configure_opentelemetry` function that sets up the OTel SDK via
environment variables
- Adds unit tests for the distro to verify API key, endpoint and service
name can be set via parameter or env var

NOTE: env vars are used over parameters

Co-authored-by: Mike Goldsmth <goldsmith.mike@gmail.com>
  • Loading branch information
emilyashley and MikeGoldsmith committed Dec 5, 2022
1 parent 0aa43f0 commit 845508c
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 67 deletions.
8 changes: 4 additions & 4 deletions examples/hello-world-flask/app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from flask import Flask
from opentelemetry import trace
from src.honeycomb.opentelemetry import hello
# todo: how to not include the src in imports /
# is this because a local dep? or bc the parent pyproject.toml config?
from honeycomb.opentelemetry import configure_opentelemetry

configure_opentelemetry()

app = Flask(__name__)

Expand All @@ -12,4 +12,4 @@ def hello_world():
with trace.get_tracer(__name__).start_as_current_span("foo"):
with trace.get_tracer(__name__).start_as_current_span("bar"):
print("baz")
return hello.hello_world()
return "Hello World"
2 changes: 1 addition & 1 deletion examples/hello-world-flask/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ opentelemetry-instrumentation = "^0.35b0"
opentelemetry-distro = "^0.35b0"
opentelemetry-instrumentation-flask = "^0.35b0"
opentelemetry-exporter-otlp-proto-grpc = "^1.14.0"
honeycomb-opentelemetry-python = {path = "../../", develop = true}
honeycomb-opentelemetry = {path = "../../", develop = true}

[build-system]
requires = ["poetry-core"]
Expand Down
1 change: 1 addition & 0 deletions honeycomb/opentelemetry/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from honeycomb.opentelemetry.distro import configure_opentelemetry
58 changes: 58 additions & 0 deletions honeycomb/opentelemetry/distro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""
Add module doc string
"""
import os
from opentelemetry.instrumentation.distro import BaseDistro
from opentelemetry.environment_variables import OTEL_TRACES_EXPORTER, OTEL_METRICS_EXPORTER
from opentelemetry.sdk.environment_variables import (
OTEL_SERVICE_NAME,
OTEL_EXPORTER_OTLP_PROTOCOL,
OTEL_EXPORTER_OTLP_HEADERS,
OTEL_EXPORTER_OTLP_ENDPOINT
)

HONEYCOMB_API_KEY = "HONEYCOMB_API_KEY"
HONEYCOMB_API_ENDPOINT = "HONEYCOMB_API_ENDPOINT"

DEFAULT_API_ENDPOINT = "api.honeycomb.io:443"
DEFAULT_SERVICE_NAME = "unknown_service:python"


def configure_opentelemetry(
apikey: str = None,
service_name: str = None,
endpoint: str = None
):
os.environ.setdefault(OTEL_EXPORTER_OTLP_PROTOCOL, "grpc")
os.environ.setdefault(OTEL_TRACES_EXPORTER, "otlp")
# disable metrics for now
os.environ.setdefault(OTEL_METRICS_EXPORTER, "none")

service_name = os.environ.get(OTEL_SERVICE_NAME, service_name)
if not service_name:
# TODO - warn no service name set, defaulting to unknown_service:python
service_name = DEFAULT_SERVICE_NAME
os.environ.setdefault(OTEL_SERVICE_NAME, service_name)

endpoint = os.environ.get(OTEL_EXPORTER_OTLP_ENDPOINT, endpoint)
if not endpoint:
endpoint = DEFAULT_API_ENDPOINT
os.environ.setdefault(OTEL_EXPORTER_OTLP_ENDPOINT, endpoint)

apikey = os.environ.get(HONEYCOMB_API_KEY, apikey)
if apikey:
os.environ.setdefault(OTEL_EXPORTER_OTLP_HEADERS,
f"x-honeycomb-team={apikey}")
else:
# TODO - warn no API key set
pass


class HoneycombDistro(BaseDistro):
"""
The OpenTelemetry provided Distro configures a default set of
configuration out of the box.
"""

def _configure(self, **kwargs):
configure_opentelemetry()
114 changes: 112 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
[tool.poetry]
name = "honeycomb-opentelemetry-python"
name = "honeycomb-opentelemetry"
version = "0.1.0"
description = "Honeycomb OpenTelemetry Distro for Python"
authors = ["Honeycomb <support@honeycomb.io>"]
readme = "README.md"
packages = [{include = "src"}]
packages = [{include = "honeycomb" }]

[tool.poetry.dependencies]
python = "^3.10"
opentelemetry-instrumentation = "^0.35b0"
opentelemetry-sdk = "^1.14.0"

[tool.poetry.group.dev.dependencies]
coverage = "^6.5.0"
Expand All @@ -18,9 +20,3 @@ pycodestyle = "^2.10.0"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.pytest.ini_options]
pythonpath = "src"
addopts = [
"--import-mode=importlib",
]
Empty file.
47 changes: 0 additions & 47 deletions src/honeycomb/opentelemetry/hello.py

This file was deleted.

Loading

0 comments on commit 845508c

Please sign in to comment.