Skip to content

Commit

Permalink
Add OTEL logs support
Browse files Browse the repository at this point in the history
  • Loading branch information
idrissneumann committed Mar 14, 2024
1 parent 3d4410c commit 93d2806
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
8 changes: 8 additions & 0 deletions .docker/otel-collector-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ receivers:

exporters:
debug:
logging:
prometheus:
endpoint: "0.0.0.0:8889"
const_labels:
Expand All @@ -15,6 +16,9 @@ exporters:
tls:
insecure: true

processors:
batch:

service:
pipelines:
metrics:
Expand All @@ -23,3 +27,7 @@ service:
traces:
receivers: [otlp]
exporters: [otlp]
logs:
receivers: [otlp]
processors: [batch]
exporters: [logging]
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.6.5
3.6.6
5 changes: 3 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import JSONResponse
from fastapi.exception_handlers import http_exception_handler
from asgi_correlation_id import CorrelationIdMiddleware, correlation_id
from asgi_correlation_id import CorrelationIdMiddleware
from prometheus_fastapi_instrumentator import Instrumentator
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor

Expand All @@ -13,7 +13,7 @@
from utils.cid import get_current_cid
from utils.manifests import get_manifest_as_dict
from utils.heartbit import heartbit
from utils.otel import init_otel_tracer, init_otel_metrics
from utils.otel import init_otel_tracer, init_otel_metrics, init_otel_logger

version = "unkown"
manifest = get_manifest_as_dict()
Expand All @@ -38,6 +38,7 @@

init_otel_tracer()
init_otel_metrics()
init_otel_logger()

heartbit()

Expand Down
1 change: 0 additions & 1 deletion src/utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,3 @@ def log_msg(log_level, message, is_public = False):
if get_int_value_level(log_level) >= get_int_value_level(LOG_LEVEL) and is_notif_enabled():
slack_message(log_level, message, is_public)
discord_message(log_level, message, is_public)

16 changes: 16 additions & 0 deletions src/utils/otel.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
import os
import logging

from opentelemetry import trace
from opentelemetry.metrics import set_meter_provider, get_meter
from opentelemetry._logs import set_logger_provider
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.resources import Resource
from opentelemetry.semconv.resource import ResourceAttributes
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporter
from opentelemetry.exporter.otlp.proto.grpc._log_exporter import OTLPLogExporter

from utils.common import is_enabled

_otel_tracer = trace.get_tracer(__name__)
_otel_collector_endpoint = os.getenv('OTEL_COLLECTOR_ENDPOINT')
_otel_service_name = "imalive-{}".format(os.getenv('IMALIVE_NODE_NAME', "anode"))
_otel_service_version = os.getenv('VERSION', '0.1')

_otel_meter = get_meter(_otel_service_name, version=_otel_service_version)
_otel_resource = Resource.create(attributes={
ResourceAttributes.SERVICE_NAME: _otel_service_name,
})

_logger_provider = LoggerProvider(_otel_resource)
set_logger_provider(_logger_provider)

def init_otel_tracer():
trace.set_tracer_provider(TracerProvider(resource=_otel_resource))

Expand All @@ -33,6 +42,13 @@ def init_otel_metrics():
otlp_exporter = OTLPMetricExporter(endpoint=_otel_collector_endpoint, insecure=True)
set_meter_provider(MeterProvider(resource=_otel_resource, metric_readers=[PeriodicExportingMetricReader(otlp_exporter, export_interval_millis=5000)]))

def init_otel_logger():
if is_enabled(_otel_collector_endpoint):
otlp_exporter = OTLPLogExporter(endpoint=_otel_collector_endpoint, insecure=True)
_logger_provider.add_log_record_processor(BatchLogRecordProcessor(otlp_exporter))
handler = LoggingHandler(level=logging.NOTSET, logger_provider=_logger_provider)
logging.getLogger().addHandler(handler)

def get_otel_tracer():
return _otel_tracer

Expand Down

0 comments on commit 93d2806

Please sign in to comment.