Skip to content

Commit

Permalink
Add telemetry logging functionality to the codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
inean committed Sep 14, 2024
1 parent 5f56cb4 commit d492f89
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ dependencies = [
requires-python = ">= 3.11"

[project.optional-dependencies]
telemetry = [
"opentelemetry-api",
"opentelemetry-sdk",
"opentelemetry-exporter-otlp-proto-grpc",
]

[project.urls]
Homepage = "https://github.com/inean/dns-synchub"
Expand Down
2 changes: 1 addition & 1 deletion src/dns_synchub/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def initialize_logger(settings: Settings) -> logging.Logger:
}
if any(telemetry_options.values()):
try:
from dns_synchub.telemetry import telemetry_log_handler # type: ignore
from dns_synchub.telemetry import telemetry_log_handler

handler = telemetry_log_handler(settings.service_name, **telemetry_options)
logger.addHandler(handler)
Expand Down
56 changes: 56 additions & 0 deletions src/dns_synchub/telemetry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

import atexit
from functools import lru_cache
import logging
import os
import random
import string
import time
from typing import TypedDict

from opentelemetry._logs import set_logger_provider
from opentelemetry.exporter.otlp.proto.grpc._log_exporter import OTLPLogExporter
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor, ConsoleLogExporter
from opentelemetry.sdk.resources import Resource
from typing_extensions import Unpack


# Define the ConfigType TypedDict with optional parameters
class _ConfigType(TypedDict): ...


class ConfigType(_ConfigType, total=False):
service_id: str
use_otlp_console_handler: bool
use_otlp_handler: bool


@lru_cache
def telemetry_log_handler(service_name: str, **config: Unpack[ConfigType]) -> logging.Handler:
# Set up a logging provider
logger_provider = LoggerProvider(
resource=Resource.create({
'service.name': service_name,
'service.instance.id': config.get('service_id', os.uname().nodename),
}),
)
set_logger_provider(logger_provider)
atexit.register(logger_provider.shutdown)

# Configure ConsoleLogExporter
if config.get('use_console_exporter', False):
term_exporter = ConsoleLogExporter()
logger_provider.add_log_record_processor(BatchLogRecordProcessor(term_exporter))

# Configure OTLPLogExporter
if config.get('use_oltp_exporter', True):
if 'OTEL_EXPORTER_OTLP_ENDPOINT' not in os.environ:
raise ValueError('OTEL_EXPORTER_OTLP_ENDPOINT environment variable not set')
otlp_exporter = OTLPLogExporter(insecure=True)
logger_provider.add_log_record_processor(BatchLogRecordProcessor(otlp_exporter))

# Set up a logging handler for standard Python logging
return LoggingHandler(level=logging.NOTSET, logger_provider=logger_provider)


Loading

0 comments on commit d492f89

Please sign in to comment.