Skip to content

Commit

Permalink
Add Open Telemetry logging signals (#102)
Browse files Browse the repository at this point in the history
* Add Open Telemetry logging signals

Adding logging intrumentation enables correlation with other
Open Telemetry signals, inscreasing observability for ETOS API.

* Update .gitignore
  • Loading branch information
fredjn authored Feb 5, 2025
1 parent 912f21a commit 0d66500
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ coverage.xml

# Build and docs folder/files
build/*
python/build/*
bin/*
dist/*
sdist/*
python/dist/*
python/sdist/*
docs/api/*
docs/_rst/*
docs/_build/*
Expand Down
4 changes: 2 additions & 2 deletions python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ classifiers = [
]
requires-python = ">=3.9"
dependencies = [
"etos_lib==4.4.2",
"etos_lib==4.5.0",
"etcd3gw~=2.3",
"uvicorn~=0.22",
"fastapi~=0.115.6",
Expand All @@ -27,7 +27,7 @@ dependencies = [
"sse-starlette~=1.6",
"opentelemetry-api~=1.21",
"opentelemetry-exporter-otlp~=1.21",
"opentelemetry-instrumentation-fastapi==0.46b0",
"opentelemetry-instrumentation-fastapi~=0.46b0",
"opentelemetry-sdk~=1.21",
"jsontas~=1.4",
"packageurl-python~=0.11",
Expand Down
32 changes: 25 additions & 7 deletions python/src/etos_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.sdk.resources import SERVICE_NAME, SERVICE_NAMESPACE, SERVICE_VERSION, Resource
from opentelemetry.sdk.resources import (
DEPLOYMENT_ENVIRONMENT,
SERVICE_NAME,
SERVICE_VERSION,
OTELResourceDetector,
ProcessResourceDetector,
Resource,
get_aggregated_resources,
)
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor

Expand All @@ -41,19 +49,29 @@

DEV = os.getenv("DEV", "false").lower() == "true"
ENVIRONMENT = "development" if DEV else "production"
setup_logging("ETOS API", VERSION, ENVIRONMENT)

if os.getenv("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"):
PROVIDER = TracerProvider(
resource=Resource.create(
{SERVICE_NAME: "etos-api", SERVICE_VERSION: VERSION, SERVICE_NAMESPACE: ENVIRONMENT}
)
if os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT"):
OTEL_RESOURCE = Resource.create(
{
SERVICE_NAME: "etos-api",
SERVICE_VERSION: VERSION,
DEPLOYMENT_ENVIRONMENT: ENVIRONMENT,
},
)

OTEL_RESOURCE = get_aggregated_resources(
[OTELResourceDetector(), ProcessResourceDetector()],
).merge(OTEL_RESOURCE)

PROVIDER = TracerProvider(resource=OTEL_RESOURCE)
EXPORTER = OTLPSpanExporter()
PROCESSOR = BatchSpanProcessor(EXPORTER)
PROVIDER.add_span_processor(PROCESSOR)
trace.set_tracer_provider(PROVIDER)
setup_logging("ETOS API", VERSION, ENVIRONMENT, OTEL_RESOURCE)

FastAPIInstrumentor().instrument_app(APP, tracer_provider=PROVIDER, excluded_urls=".*/ping")
else:
setup_logging("ETOS API", VERSION, ENVIRONMENT)

RegisterProviders()

0 comments on commit 0d66500

Please sign in to comment.