Skip to content

Instantly share code, notes, and snippets.

@jonathan-s
Last active April 22, 2022 12:06
Show Gist options
  • Save jonathan-s/3117c4a987db741dbb160c23b4b10aa8 to your computer and use it in GitHub Desktop.
Save jonathan-s/3117c4a987db741dbb160c23b4b10aa8 to your computer and use it in GitHub Desktop.
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
Copy link

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
)


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")

app_instrumentor = FlaskInstrumentor()
app_instrumentor.uninstrument()

app_instrumentor.instrument(
    request_hook=request_hook,
    response_hook=response_hook
)

app = flask.Flask(__name__)


# Use OTEL_RESOURCE_ATTRIBUTES env

# resource = Resource.create(attributes={
#     SERVICE_NAME: "my-service",
#     DEPLOYMENT_ENVIRONMENT: "local-dev"
# })
# tracer_provider = TracerProvider(resource=resource)
# trace.set_tracer_provider(tracer_provider)


# `opentelemetry-instrument`` does that already by using OTEL_TRACES_EXPORTER=otlp_proto_http

# 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)


# OTEL_RESOURCE_ATTRIBUTES=service.name=my-service OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318 OTEL_TRACES_EXPORTER=otlp_proto_http,console opentelemetry-instrument python app.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment