Are you prepared to start out your journey on the highway to accumulating telemetry knowledge out of your functions? Nice observability begins with nice instrumentation!
On this collection, you may discover the way to undertake OpenTelemetry (OTel) and the way to instrument an software to gather tracing telemetry. You may discover ways to leverage out-of-the-box computerized instrumentation instruments and perceive when it’s a necessity to discover extra superior guide instrumentation in your functions. By the top of this collection, you may have an understanding of how telemetry travels out of your functions to the OpenTelemetry Collector, and be able to carry OpenTelemetry to your future tasks. Every little thing mentioned right here is supported by a hands-on, self-paced workshop authored by Paige Cruz.
Within the earlier article, we explored the way to leverage programmatic instrumentation in our software as builders would of their day by day coding utilizing OTel libraries. On this article, we stock onwards to enhance the visualization of our telemetry knowledge that was being displayed in console output. We’re going to programmatically instrument and configure our software to direct all telemetry knowledge to a Jaeger occasion for visible insights.
It’s assumed that you just adopted the earlier articles in establishing each OpenTelemetry and the instance Python software challenge, but when not, return and see the earlier articles as it isn’t coated right here.
Instrumenting With Jaeger
As talked about beforehand, OTel doesn’t present a backend to retailer its collected telemetry knowledge. As an alternative, it is appearing as a collector of telemetry knowledge and forwarding that to our most popular backend system. For visualizing telemetry knowledge we want this backend storage the place we then can question our knowledge that tooling, corresponding to Jaeger can then visualize in dashboards.
Jaeger was constructed with all of this in thoughts, offering a really fast approach to get began if you happen to provide it with OpenTelemetry Protocol (OTLP) formatted knowledge. We’re going to use the default Jaeger in-memory storage for our telemetry knowledge by sending it to Jaeger and discover it visually utilizing the predefined UI dashboards.
Carrying on from our earlier setup the place we instrumented programmatically to output our span knowledge to the console, we’ll modify this to now use the OTLPSpanExporter
in our software code. This may be configured to ship our span knowledge on to a Jaeger occasion with an OTLP endpoint.
Utilizing the downloaded challenge we put in from earlier articles, we are able to open the file programmatic/Buildfile-prog and add the daring traces beneath to put in the OTLP Exporter library:
FROM python:3.12-bullseye WORKDIR /app COPY necessities.txt necessities.txt RUN pip set up -r necessities.txt RUN pip set up opentelemetry-api opentelemetry-exporter-otlp opentelemetry-sdk opentelemetry-instrumentation-flask opentelemetry-instrumentation-jinja2 opentelemetry-instrumentation-requests COPY . . CMD [ "flask", "run", "--host=0.0.0.0"]
Subsequent, we have to modify the applying code present in programmatic/app.py to import the OLTPSpanExporter
library and swap out the ConsoleSpanExporter
as proven in daring:
import random import re import urllib3 import requests from flask import Flask, render_template, request from breeds import breeds from opentelemetry.hint import set_tracer_provider from opentelemetry.sdk.hint import TracerProvider from opentelemetry.sdk.hint.export import SimpleSpanProcessor from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter from opentelemetry.instrumentation.flask import FlaskInstrumentor from opentelemetry.instrumentation.jinja2 import Jinja2Instrumentor from opentelemetry.instrumentation.requests import RequestsInstrumentor supplier = TracerProvider() processor = SimpleSpanProcessor(OTLPSpanExporter()) supplier.add_span_processor(processor) set_tracer_provider(supplier) ...
Subsequent, we insert the imports wanted for flask
, jinja2
, and requests
instrumentation libraries above the part we simply created. The code to be added is proven in daring beneath:
import random import re import urllib3 import requests from flask import Flask, render_template, request from breeds import breeds from opentelemetry.hint import set_tracer_provider from opentelemetry.sdk.hint import TracerProvider from opentelemetry.sdk.hint.export import SimpleSpanProcessor, ConsoleSpanExporter from opentelemetry.instrumentation.flask import FlaskInstrumentor from opentelemetry.instrumentation.jinja2 import Jinja2Instrumentor from opentelemetry.instrumentation.requests import RequestsInstrumentor supplier = TracerProvider() processor = SimpleSpanProcessor(ConsoleSpanExporter()) supplier.add_span_processor(processor) set_tracer_provider(supplier) app = Flask("hello-otel") FlaskInstrumentor().instrument_app(app) Jinja2Instrumentor().instrument() RequestsInstrumentor().instrument() ...
Subsequent, we are able to assessment the pod configuration file the place all the small print are discovered to run a number of container pictures in a Kubernetes pod. We might be utilizing the Jaeger all-in-one picture, which is designed for fast native testing. It consists of the Jaeger UI, jaeger-collector, jaeger-query, and jaeger-agent, with an in-memory storage part.
Save and shut the file programmatic/app.py and construct the brand new container picture with the next command:
$ podman construct -t hello-otel:prog -f programmatic/Buildfile-prog Efficiently tagged localhost/hello-otel:prog 516c5299a32b68e7a4634ce15d1fd659eed2164ebe945ef1673f7a55630e22c8
Subsequent, open the file programmatic/app_pod.yaml and assessment the Jaeger part. Word the ports: 16686
and 4318
. The primary is for the Jaeger UI and the second is for telemetry knowledge despatched by way of OTLP over HTTP:
... - title: jaeger-all-in-one picture: jaegertracing/all-in-one:1.56 assets: limits: reminiscence: "128Mi" cpu: "500m" ports: - containerPort: 16686 hostPort: 16686 - containerPort: 4318 env: - title: COLLECTOR_OTLP_ENABLED worth: "true" - title: OTEL_TRACES_EXPORTER worth: "otlp"
Now let’s run all the configuration, placing our software and the Jaeger all-in-one containers in motion in a pod with the next:
$ podman play kube programmatic/app_pod.yaml Pod: 277dd50306eed445f4f43fc33111eedb31ed5804db1f60a6f0784a2333a54de0 Containers: b9d5075e2051502b12510deddfb34498f32b2ae12554c5328fecd9725c7b1fe2 8505e8c8cfffaff1f473cfbbf5d9a312ca8247f32653cccf7d192305c1ca741a
Open a browser and examine the Jaeger UI at http://localhost:16686 which ought to show the Gopher Detective as proven beneath:
Now we are able to generate telemetry knowledge by accessing our software and making a number of requests to the http://localhost:8001/doggo endpoint and see one thing like this:
Refreshing the doggo software emits traces with spans from every instrumentation library beneath offering a pleasant visible within the Jaeger UI:
- Flask spans representing requests to the app
- Requests spans for the exterior request to Canine API
- Jinja2 spans for HTML template compilation
Again in our Jaeger UI, we are able to choose hello-otel from the service dropdown menu and click on on the discover traces button on the underside proper. Affirm that you just see traces returned for the operation /doggo, one thing like this:
Confirm by clicking on a span title, the highest one for instance, and examine the hint waterfall view:
This completes the tour of programmatic instrumentation the place we put in and configured OpenTelemetry API and SDK programmatically in our software, efficiently despatched traces, and at last configured our software to view our traces within the Jaeger UI.
These examples use code from a Python software which you can discover within the offered hands-on workshop.
What’s Subsequent?
This text accomplished our journey into programmatic instrumentation the place we instrumented our software as builders and seen our traces within the Jaeger UI.
In our subsequent article, we’ll be visually exploring, querying, and viewing our tracing knowledge in Jaeger.