Last active
April 22, 2022 12:06
-
-
Save jonathan-s/3117c4a987db741dbb160c23b4b10aa8 to your computer and use it in GitHub Desktop.
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
import flask | |
import requests | |
from opentelemetry import trace | |
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter | |
from opentelemetry.instrumentation.flask import FlaskInstrumentor | |
from opentelemetry.sdk.trace import TracerProvider | |
from opentelemetry.sdk.trace.export import ( | |
BatchSpanProcessor, | |
) | |
from opentelemetry.sdk.resources import ( | |
SERVICE_NAME, | |
DEPLOYMENT_ENVIRONMENT, | |
Resource | |
) | |
app = flask.Flask(__name__) | |
def request_hook(span, environ): | |
if span and span.is_recording(): | |
pass | |
def response_hook(span, status: str, response_headers): | |
if span and span.is_recording(): | |
span.set_attribute("custom.label", "hello world") | |
resource = Resource.create(attributes={ | |
SERVICE_NAME: "my-service", | |
DEPLOYMENT_ENVIRONMENT: "local-dev" | |
}) | |
tracer_provider = TracerProvider(resource=resource) | |
trace.set_tracer_provider(tracer_provider) | |
app_instrumentor = FlaskInstrumentor() | |
app_instrumentor.uninstrument() | |
app_instrumentor.instrument_app( | |
app, | |
tracer_provider=tracer_provider, | |
request_hook=request_hook, | |
response_hook=response_hook | |
) | |
exporter = OTLPSpanExporter() | |
tracer_provider.add_span_processor( | |
BatchSpanProcessor(exporter) | |
) | |
@app.route("/") | |
def hello(): | |
requests.get("http://www.example.com") | |
return "hello" | |
if __name__ == '__main__': | |
app.run(port=5000) |
srikanthccv
commented
Apr 22, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment