generated from honeycombio/.github
-
Notifications
You must be signed in to change notification settings - Fork 6
/
distro.py
80 lines (65 loc) · 2.69 KB
/
distro.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""This honey-flavored Distro configures OpenTelemetry for use with Honeycomb.
Typical usage example:
using the opentelemetry-instrument command with
requisite env variables set:
$bash> opentelemetry-instrument python program.py
or configured by code within your service:
configure_opentelemetry(
HoneycombOptions(
debug=True,
apikey=os.getenv("HONEYCOMB_API_KEY"),
service_name="otel-python-example"
)
)
"""
from logging import getLogger
from typing import Optional
from opentelemetry.instrumentation.distro import BaseDistro
from opentelemetry.metrics import set_meter_provider
from opentelemetry.trace import set_tracer_provider
from honeycomb.opentelemetry.metrics import create_meter_provider
from honeycomb.opentelemetry.options import HoneycombOptions
from honeycomb.opentelemetry.resource import create_resource
from honeycomb.opentelemetry.trace import create_tracer_provider
_logger = getLogger(__name__)
def configure_opentelemetry(
options: Optional[HoneycombOptions] = None,
):
"""
Configures the OpenTelemetry SDK to send telemetry to Honeycomb.
Args:
options (HoneycombOptions, optional): the HoneycombOptions used to
configure the the SDK. These options can be set either as parameters
to this function or through environment variables
Note: API key is a required option.
"""
if options is None:
options = HoneycombOptions()
_logger.info("🐝 Configuring OpenTelemetry using Honeycomb distro 🐝")
_logger.debug(vars(options))
resource = create_resource(options)
set_tracer_provider(
create_tracer_provider(options, resource)
)
if options.metrics_dataset:
set_meter_provider(
create_meter_provider(options, resource)
)
# pylint: disable=too-few-public-methods
class HoneycombDistro(BaseDistro):
"""
An extension of the base python OpenTelemetry distro, which provides
a mechanism to automatically configure some of the more common options
for users. This class is auto-detected by the `opentelemetry-instrument`
command.
This class doesn't need to be touched directly when using the distro. If
you'd like to explicitly set configuration in code, use the
configure_opentelemetry() function above instead of the
`opentelemetry-instrument` command.
If you're wondering about the under-the-hood magic - we add the following
declaration to package metadata in our pyproject.toml, like so:
[tool.poetry.plugins."opentelemetry_distro"]
distro = "honeycomb.opentelemetry.distro:HoneycombDistro"
"""
def _configure(self, **kwargs):
configure_opentelemetry()