You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert

525
525
> Trace detail showing the function invocation with the custom span. The span has the order id attribute and events that we added to it.
526
526
527
+
## Capture traces from libraries and frameworks
528
+
529
+
Traces and auto-instrumentation can become really valuable when you have more complex function workflows where functions are chained together or interact with external systems. These functions most likely use third-party Python libraries to perform actions like query a database, make HTTP requests, talk to a message broker or key value store, etc. Auto-instrumentation allows you to capture traces from many popular libraries without changing your code.
530
+
531
+
We will expand the order function to simulate this kind of workflow. The order function will look up an order in a PostgreSQL database and invoke a second function, `order-confirmation` when it is done processing the order.
532
+
533
+
To follow along with this example you can quickly deploy a PostgreSQL database in your cluster using [arkade](https://github.com/alexellis/arkade?tab=readme-ov-file#getting-arkade):
534
+
535
+
```sh
536
+
arkade install postgresql
537
+
```
538
+
arkade prints out connection instructions and the command to get the database password. Connect to the database and seed it with an orders table and some dummy content.
539
+
540
+
Create the `orders` table:
541
+
542
+
```sql
543
+
CREATE TABLE orders (
544
+
id SERIAL PRIMARY KEY,
545
+
customer_name VARCHAR(100) NOT NULL,
546
+
order_date DATE NOT NULL DEFAULT CURRENT_DATE,
547
+
total_amount NUMERIC(10, 2) NOT NULL
548
+
);
549
+
```
550
+
551
+
Insert a sample order:
552
+
553
+
```sql
554
+
INSERT INTO orders (customer_name, order_date, total_amount)
555
+
VALUES ('Alice Johnson', '2025-05-12', 149.99);
556
+
```
557
+
558
+
[Psycopg](https://www.psycopg.org/docs/) will be used to query the Postgresql database and the [Requests](https://requests.readthedocs.io/en/latest/) package to make HTTP requests. Both packages need to be appended to the `requirements.txt` file for the order function.
559
+
560
+
```diff
561
+
opentelemetry-distro
562
+
opentelemetry-exporter-otlp
563
+
+psycopg2-binary
564
+
+requests
565
+
```
566
+
567
+
Update the `handler.py` to get orders from the database and invoke the `order-confirmation` function.
In a real application this function would perform jobs like sending out the order confirmation via email. To keep things simple we are just going to sleep a couple of milliseconds to simulate some work.
662
+
663
+
```python
664
+
import time
665
+
666
+
def handle(event, context):
667
+
668
+
# simulate sending order confirmation email
669
+
time.sleep(0.2)
670
+
671
+
return {
672
+
"statusCode": 200,
673
+
"body": "Confirmation order"
674
+
}
675
+
```
676
+
677
+
Deploy both functions:
678
+
679
+
```sh
680
+
faas-cli up
681
+
```
682
+
683
+
Invoke the order function and view the trace in Grafana.

690
+
> Screenshot of a trace visualization for the order function in Grafana
691
+
692
+
The trace includes 5 spans. We can clearly see the different operations the function performed and how much time they took. The top level span for the GET request to `orders.openfaas-fn` shows the complete function invocation took 616.73ms. As children of that span we see our custom `process_order` span and the `POST` request made to the `order-confirmation` function. We also get a span with the details of the database query.
693
+
694
+
Note that we did not have to make any changes to get traces from the `psycopg2` and `requests` package. The auto-instrumentation took care of that.
695
+
696
+
The Python agent by default will detect a Python program’s packages and instrument any packages it can. This makes instrumentation easy, but can also result in too much or unwanted data. You can omit specific packages from instrumentation by using the `OTEL_PYTHON_DISABLED_INSTRUMENTATIONS` environment variable. See: [Disabling Specific Instrumentations](https://opentelemetry.io/docs/zero-code/python/configuration/) for more details.
697
+
527
698
## OpenTelemetry on OpenFaaS Edge
528
699
529
700
Collecting telemetry is also supported on [OpenFaaS Edge](https://docs.openfaas.com/edge/overview/). OpenFaaS Edge is a lightweight option for running OpenFaaS functions that does not use Kubernetes, ideal for edge computing environments.
0 commit comments