forked from AOT-Technologies/forms-flow-ai
-
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.
Merge pull request AOT-Technologies#2613 from sinto-aot/fwf-1900-open…
…telemetry-utils Added telemetry.py in forms-flow-api-utils
- Loading branch information
Showing
2 changed files
with
37 additions
and
0 deletions.
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
36 changes: 36 additions & 0 deletions
36
forms-flow-api-utils/src/formsflow_api_utils/utils/telemetry.py
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,36 @@ | ||
"""This exposes Opentelemetry service.""" | ||
|
||
import os | ||
|
||
from opentelemetry import trace | ||
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import ( | ||
OTLPSpanExporter, | ||
) | ||
from opentelemetry.instrumentation.flask import FlaskInstrumentor | ||
from opentelemetry.instrumentation.requests import RequestsInstrumentor | ||
from opentelemetry.sdk.resources import Resource | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
|
||
|
||
def setup_tracing(app, service_name): | ||
"""Sets up OpenTelemetry tracing for the application.""" | ||
# Check if OpenTelemetry tracing is enabled | ||
if os.getenv("ENABLE_OPENTELEMETRY", "false").lower() != "true": | ||
app.logger.info("OpenTelemetry tracing is disabled.") | ||
return | ||
|
||
resource = Resource.create({"service.name": service_name}) | ||
trace_provider = TracerProvider(resource=resource) | ||
trace.set_tracer_provider(trace_provider) | ||
|
||
# Configure the OTLP exporter to send data to the OpenTelemetry Collector | ||
otlp_endpoint = os.getenv("OTLP_ENDPOINT") | ||
otlp_exporter = OTLPSpanExporter(endpoint=otlp_endpoint, insecure=True) | ||
|
||
# Add the OTLP exporter to the tracer provider | ||
otlp_span_processor = BatchSpanProcessor(otlp_exporter) | ||
trace_provider.add_span_processor(otlp_span_processor) | ||
|
||
FlaskInstrumentor().instrument_app(app) | ||
RequestsInstrumentor().instrument() |