forked from morganzero/cloudflare-companion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add telemetry logging functionality to the codebase
- Loading branch information
Showing
4 changed files
with
268 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
Oops, something went wrong.