forked from Jayclifford345/adventure
-
Notifications
You must be signed in to change notification settings - Fork 2
/
otel.py
152 lines (121 loc) · 6.44 KB
/
otel.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Import the function to set the global logger provider from the OpenTelemetry logs module.
from opentelemetry._logs import set_logger_provider
# Import the OTLPLogExporter class from the OpenTelemetry gRPC log exporter module for exporting logs.
from opentelemetry.exporter.otlp.proto.http._log_exporter import OTLPLogExporter
# Import LoggerProvider and LoggingHandler classes to create and handle logging with OpenTelemetry.
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
# Import BatchLogRecordProcessor to handle batches of log records before export.
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
# Import the Resource class to associate resources such as service name and instance ID with metrics, logs, and traces.
from opentelemetry.sdk.resources import Resource
# Import the standard Python logging module to log application-specific information.
import logging
# Import metrics-related modules for managing and exporting metrics with OpenTelemetry.
from opentelemetry import metrics
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
# Import tracing-related modules for setting up and managing traces.
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.sdk.trace.sampling import TraceIdRatioBased
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
# Interval in seconds for exporting metrics periodically.
INTERVAL_SEC = 10
class CustomTracer:
def __init__(self):
# Set up TracerProvider only once globally
exporter = OTLPSpanExporter()
span_processor = BatchSpanProcessor(span_exporter=exporter)
# Create a singleton TracerProvider if not already configured
tracer_provider = TracerProvider(
sampler=TraceIdRatioBased(1.0),
resource=Resource.create(
{"service.name": "adventure-game", "service.instance.id": "instance-1"}
)
)
trace.set_tracer_provider(tracer_provider)
trace.get_tracer_provider().add_span_processor(span_processor)
def get_trace(self):
return trace
class CustomMetrics:
"""
CustomMetrics sets up metrics collection using OpenTelemetry with a specified service name.
"""
def __init__(self, service_name):
try:
# Create the metrics exporter to send data to the backend.
exporter = OTLPMetricExporter()
# Set up a PeriodicExportingMetricReader to export metrics at regular intervals.
metric_reader = PeriodicExportingMetricReader(exporter, INTERVAL_SEC)
# Create a MeterProvider for managing metrics with an associated resource (service name and instance).
self.meter_provider = MeterProvider(
metric_readers=[metric_reader],
resource=Resource.create({"service.name": service_name, "service.instance.id": "instance-1"})
)
# Set the meter provider as the global meter provider.
metrics.set_meter_provider(self.meter_provider)
# Obtain a meter to use in creating metrics.
self.meter = metrics.get_meter(__name__)
# Indicate successful metrics configuration.
print("Metrics configured with OpenTelemetry.")
except Exception as e:
# Handle errors during metrics setup and set the meter to None for safety.
self.meter = None
print(f"Error configuring metrics: {e}")
def get_meter(self):
"""
Return the configured meter. Raises an error if the meter isn't configured properly.
"""
if self.meter is None:
raise RuntimeError("Meter is not configured. Please check for errors during initialization.")
return self.meter
class CustomLogFW:
"""
CustomLogFW sets up logging using OpenTelemetry with a specified service name and instance ID.
"""
def __init__(self, service_name):
try:
# Create an instance of LoggerProvider with a Resource object.
# Resource is used to include metadata like the service name and instance ID.
self.logger_provider = LoggerProvider(
resource=Resource.create(
{
"service.name": service_name,
"service.instance.id": "instance-1"
}
)
)
# Flag indicating that the logger provider is properly configured.
self.logger_configured = True
except Exception as e:
# In case of error, set the logger provider to None and set the configured flag to False.
self.logger_provider = None
self.logger_configured = False
print(f"Error configuring logging: {e}")
def setup_logging(self):
"""
Set up the logging configuration for OpenTelemetry.
:return: A LoggingHandler instance configured with the logger provider.
:raises: RuntimeError if the logger provider is not configured properly.
"""
if not self.logger_configured:
# If the logger provider wasn't set up correctly, raise an error.
raise RuntimeError("LoggerProvider not configured correctly. Cannot set up logging.")
# Set the created LoggerProvider as the global logger provider.
set_logger_provider(self.logger_provider)
# Create an instance of OTLPLogExporter to export logs.
exporter = OTLPLogExporter()
# Add a BatchLogRecordProcessor to the logger provider.
# This processor batches logs before sending them to the backend.
self.logger_provider.add_log_record_processor(
BatchLogRecordProcessor(exporter=exporter, max_queue_size=5, max_export_batch_size=1)
)
# Create a LoggingHandler that integrates OpenTelemetry logging with the Python logging system.
# Setting log level to NOTSET to capture all log levels.
handler = LoggingHandler(level=logging.NOTSET, logger_provider=self.logger_provider)
# Indicate successful logging configuration.
print("Logging configured with OpenTelemetry.")
return handler