Skip to content

Commit

Permalink
feat: Configure logging (#37)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?
Setups logging when using the distro. Log level can be set either using
`OTEL_LOG_LEVEL` environment variable or the `log_level` option.

- Closes #36 

## Short description of the changes
- Setups and registers a logger during HoneycombOptions init
- Use logger to indicate Honeycomb distro is being used during
`configure_opentelemetry`
- Removes left over debug log in a test

## How to verify that this has the expected result
Log entires are printed out at the configured log level.

Note: This is based on the follow PR, and will auto update once it's
merged
- #27

Co-authored-by: Robb Kidd <robbkidd@honeycomb.io>
  • Loading branch information
MikeGoldsmith and robbkidd authored Dec 12, 2022
1 parent 460da85 commit 9cfd202
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
4 changes: 4 additions & 0 deletions honeycomb/opentelemetry/distro.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
from honeycomb.opentelemetry.options import HoneycombOptions
from honeycomb.opentelemetry.resource import create_resource
from honeycomb.opentelemetry.trace import create_tracer_provider
from logging import getLogger
from opentelemetry.instrumentation.distro import BaseDistro
from opentelemetry.metrics import set_meter_provider
from opentelemetry.trace import set_tracer_provider

_logger = getLogger(__name__)


def configure_opentelemetry(
options: HoneycombOptions = HoneycombOptions(),
):
_logger.debug("🐝 Configuring OpenTelemetry using Honeycomb distro 🐝")
resource = create_resource(options)
set_tracer_provider(
create_tracer_provider(options, resource)
Expand Down
27 changes: 24 additions & 3 deletions honeycomb/opentelemetry/options.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import logging
import os
from opentelemetry.sdk.environment_variables import (
OTEL_SERVICE_NAME,
OTEL_EXPORTER_OTLP_ENDPOINT
)
from grpc import ssl_channel_credentials


HONEYCOMB_API_KEY = "HONEYCOMB_API_KEY"
HONEYCOMB_API_ENDPOINT = "HONEYCOMB_API_ENDPOINT"
OTEL_LOG_LEVEL = "OTEL_LOG_LEVEL"
DEFAULT_API_ENDPOINT = "api.honeycomb.io:443"
DEFAULT_SERVICE_NAME = "unknown_service:python"
DEFAULT_LOG_LEVEL = "ERROR"

log_levels = {
"NOTSET": logging.NOTSET,
"DEBUG": logging.DEBUG,
"INFO": logging.INFO,
"WARNING": logging.WARNING,
"ERROR": logging.ERROR,
"CRITICAL": logging.CRITICAL,
}

_logger = logging.getLogger(__name__)


class HoneycombOptions:
Expand All @@ -18,13 +31,21 @@ class HoneycombOptions:
endpoint = DEFAULT_API_ENDPOINT
insecure = False
enable_metrics = False
log_level = DEFAULT_LOG_LEVEL

def __init__(
self, apikey: str = None,
self,
apikey: str = None,
service_name: str = None,
endpoint: str = None,
insecure: bool = False
insecure: bool = False,
log_level: str = None
):
log_level = os.environ.get(OTEL_LOG_LEVEL, log_level)
if log_level and log_level.upper() in log_levels:
self.log_level = log_level.upper()
logging.basicConfig(level=log_levels[self.log_level])

self.apikey = os.environ.get(HONEYCOMB_API_KEY, apikey)

self.service_name = os.environ.get(OTEL_SERVICE_NAME, service_name)
Expand Down
1 change: 0 additions & 1 deletion tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def test_defaults(monkeypatch):
def test_can_set_service_name_with_param(monkeypatch):
monkeypatch.delenv(OTEL_SERVICE_NAME, raising=False)
options = HoneycombOptions(service_name='my-service')
print(vars(options))
assert options.service_name == 'my-service'


Expand Down

0 comments on commit 9cfd202

Please sign in to comment.