-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
あで
committed
Jun 13, 2023
1 parent
5a8a344
commit 20ca9fa
Showing
8 changed files
with
612 additions
and
93 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
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
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,45 @@ | ||
from opentelemetry import trace | ||
from autometrics import autometrics | ||
from prometheus_client import REGISTRY | ||
from prometheus_client.openmetrics.exposition import generate_latest | ||
from starlette import applications | ||
from starlette.responses import PlainTextResponse | ||
from starlette.routing import Route | ||
from opentelemetry import trace | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export import ( | ||
BatchSpanProcessor, | ||
ConsoleSpanExporter, | ||
) | ||
|
||
# Let's start by setting up the OpenTelemetry SDK with some defaults | ||
provider = TracerProvider() | ||
processor = BatchSpanProcessor(ConsoleSpanExporter()) | ||
provider.add_span_processor(processor) | ||
trace.set_tracer_provider(provider) | ||
|
||
# Now we can instrument our Starlette application | ||
tracer = trace.get_tracer(__name__) | ||
|
||
|
||
# We need to add tracer decorator before autometrics so that we see the spans | ||
@tracer.start_as_current_span("hello") | ||
@autometrics | ||
def hello(request): | ||
return PlainTextResponse("hello world") | ||
|
||
|
||
def metrics(request): | ||
# Exemplars are not supported by default prometheus format, so we specifically | ||
# make an endpoint that uses the OpenMetrics format that supoorts exemplars. | ||
body = generate_latest(REGISTRY) | ||
return PlainTextResponse(body, media_type="application/openmetrics-text") | ||
|
||
|
||
app = applications.Starlette(routes=[Route("/", hello), Route("/metrics", metrics)]) | ||
|
||
# Now, start the app (env variables are required to enable exemplars): | ||
# AUTOMETRICS_TRACKER=prometheus AUTOMETRICS_EXEMPLARS=true uvicorn starlette-otel-exemplars:app --port 3000 | ||
# And make some requests to /. You should see the spans in the console. | ||
# You can hover over the hello handler and see the links to the metrics provided by autometrics. | ||
# If you open the queries in prometheus, you should see span and trace id exemplars added to metrics. |
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 @@ | ||
AUTOMETRICS_TRACKER=prometheus AUTOMETRICS_EXEMPLARS=true uvicorn starlette-otel-exemplars:app --port 3000 |
Oops, something went wrong.