|
| 1 | +<!-- |
| 2 | +Goal of this doc: |
| 3 | +The user is able to manually instrument their Python application |
| 4 | +--> |
| 5 | + |
| 6 | +# Manual instrumentation |
| 7 | + |
| 8 | +This guide shows you how to use the Elastic Distribution of OpenTelemetry Python (EDOT Python) to manually instrument your Python application and send OpenTelemetry data to an Elastic Observability deployment. |
| 9 | + |
| 10 | +This guide requires to have already added autoinstrumentation with OpenTelemetry to your application per [Get started](./get-started.md). |
| 11 | + |
| 12 | +**New to OpenTelemetry?** If your are new to OpenTelemetry we encourage you to take a look at our [get started documentation](./get-started.md) instead, which will introduce you to autoinstrumentation. |
| 13 | + |
| 14 | +<!-- ✅ Provide _minimal_ configuration/setup --> |
| 15 | +### Configure EDOT Python |
| 16 | + |
| 17 | +To configure EDOT Python, at a minimum you'll need your Elastic Observability cloud deployment's OTLP endpoint and |
| 18 | +authorization data to set a few `OTLP_*` environment variables that will be available when running EDOT Python: |
| 19 | + |
| 20 | +```sh |
| 21 | +export OTEL_RESOURCE_ATTRIBUTES=service.name=<app-name> |
| 22 | +export OTEL_EXPORTER_OTLP_ENDPOINT=https://my-deployment.apm.us-west1.gcp.cloud.es.io |
| 23 | +export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer P....l" |
| 24 | +``` |
| 25 | + |
| 26 | +Refer to our [get started](./get-started.md) page for more details. |
| 27 | + |
| 28 | +<!-- ✅ Manually instrument the application and start sending data to Elastic --> |
| 29 | +### Manually instrument your auto instrumented Python application |
| 30 | + |
| 31 | +In this section we'll show how to add manual instrumentation to an already automatically instrumented application. A use case for |
| 32 | +this setup would be to trace something in particular while keeping the benefits of the simplicity of the automatic instrumentation doing |
| 33 | +the hard work for us. |
| 34 | + |
| 35 | +As an example we'll use an application using the Flask framework that implements an endpoint mounted on `/hello` returning a friendly |
| 36 | +salute. This application is saved in a file named `app.py` that is the default module for Flask applications. |
| 37 | + |
| 38 | +``` |
| 39 | +import random |
| 40 | +
|
| 41 | +from flask import Flask |
| 42 | +from opentelemetry import trace |
| 43 | +
|
| 44 | +tracer = trace.get_tracer(__name__) |
| 45 | +
|
| 46 | +app = Flask(__name__) |
| 47 | +
|
| 48 | +@app.route("/hello") |
| 49 | +def hello(): |
| 50 | + choices = ["there", "world", "folks", "hello"] |
| 51 | + # create a span for the choice of the name, this may be a costly call in your real world application |
| 52 | + with tracer.start_as_current_span("choice") as span: |
| 53 | + choice = random.choice(choices) |
| 54 | + span.set_attribute("choice.value", choice) |
| 55 | + return f"Hello {choice}!" |
| 56 | +``` |
| 57 | + |
| 58 | +We need to make sure to have Flask and the Flask OpenTelemetry instrumentation installed: |
| 59 | + |
| 60 | +```bash |
| 61 | +pip install flask |
| 62 | +opentelemetry-bootstrap --action=install |
| 63 | +``` |
| 64 | + |
| 65 | +And then we can run this application with the following command: |
| 66 | + |
| 67 | +```bash |
| 68 | +opentelemetry-instrument flask run |
| 69 | +``` |
| 70 | + |
| 71 | +We may not only need to add a custom span to our application but also want to use a custom metric, like in the example below where we |
| 72 | +are tracking how many times we are getting one of the possible choices for our salutes. |
| 73 | + |
| 74 | +``` |
| 75 | +import random |
| 76 | +
|
| 77 | +from flask import Flask |
| 78 | +from opentelemetry import metrics, trace |
| 79 | +
|
| 80 | +tracer = trace.get_tracer(__name__) |
| 81 | +meter = metrics.get_meter(__name__) |
| 82 | +
|
| 83 | +hello_counter = meter.create_counter( |
| 84 | + "hello.choice", |
| 85 | + description="The number of times a salute is chosen", |
| 86 | +) |
| 87 | +
|
| 88 | +app = Flask(__name__) |
| 89 | +
|
| 90 | +@app.route("/hello") |
| 91 | +def hello(): |
| 92 | + choices = ["there", "world", "folks", "hello"] |
| 93 | + # create a span for the choice of the name, this may be a costly call in your real world application |
| 94 | + with tracer.start_as_current_span("choice") as span: |
| 95 | + choice = random.choice(choices) |
| 96 | + span.set_attribute("choice.value", choice) |
| 97 | + hello_counter.add(1, {"choice.value": choice}) |
| 98 | + return f"Hello {choice}!" |
| 99 | +``` |
| 100 | + |
| 101 | +<!-- ✅ What success looks like --> |
| 102 | +## Confirm that EDOT Python is working |
| 103 | + |
| 104 | +To confirm that EDOT Python has successfully connected to Elastic: |
| 105 | + |
| 106 | +1. Go to **APM** → **Traces**. |
| 107 | +1. You should see the name of the service to which you just added EDOT Python. It can take several minutes after initializing EDOT Python for the service to show up in this list. |
| 108 | +1. Click on the name in the list to see trace data. |
| 109 | + |
| 110 | +> [!NOTE] |
| 111 | +> There may be no trace data to visualize unless you have _used_ your application since initializing EDOT Python. |
| 112 | +
|
| 113 | +<!-- ✅ What they should do next --> |
| 114 | +## Next steps |
| 115 | + |
| 116 | +* Reference all available [configuration options](./configure.md). |
| 117 | +* Learn more about viewing and interpreting data in the [Observability guide](https://www.elastic.co/guide/en/observability/current/apm.html). |
0 commit comments