DEV Community

Cover image for New Relic Template for Strands
Shakir for AWS Community Builders

Posted on • Edited on

New Relic Template for Strands

Hi 👋, we’ll see about observability with New Relic / OTEL, for Strands Agents that shows some quick insights such as tokens used, cost for the tokens(based on a static cost set as variable), request duration, errors etc.

Before you begin

You can check this video for explanantion of the poc app used here, basically we would be using kubernetes mcp to interact with a k3s cluster and opentelemetry will be enabled to generate observability, the observability part for strands was discussed in this post and this video. Optionally, if you'd like to know how to collect data on New Relic through otel collector, please check this post.

Ok, so what's new 🤔, we have to just set the OTLP variables and modify the code a bit so that telemetry is sent to NewRelic endpoint. And a newrelic template was built to visualize the telemetry data.

Code

The code for the lab and the dashboard configuration is present here. You can clone and checkout as follows.

git clone https://github.com/networkandcode/networkandcode.github.io/tree/1014fbcffeda3b61d331421d2cc67d11ca98c597

cd networkandcode.github.io/strands-examples/strands-newrelic-demo/

git switch 1014fbc --detach

$ ls -a
.   .env.example     README.md         k8s_mcp_app.py  pyproject.toml    strands-agent-dashboard.json
..  .python-version  k8s_mcp_agent.py  main.py         set_telemetry.py  uv.lock
Enter fullscreen mode Exit fullscreen mode

ENV

You have to first get the API key of type Ingest License and set it as OTEL_EXPORTER_OTLP_HEADERS=api-key=<the-copied-value>. So the env file would now be as follows.

$ cat .env
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-west-2

OTEL_EXPORTER_OTLP_ENDPOINT="https://otlp.nr-data.net"
OTEL_EXPORTER_OTLP_HEADERS=api-key=
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
Enter fullscreen mode Exit fullscreen mode

The set_telemetry file is modified.

$ cat set_telemetry.py
Enter fullscreen mode Exit fullscreen mode
from dotenv import load_dotenv
from strands.telemetry import StrandsTelemetry

def set_telemetry():
    load_dotenv()
    strands_telemetry = StrandsTelemetry()
    strands_telemetry.setup_otlp_exporter()
Enter fullscreen mode Exit fullscreen mode

Run

We can now run the app, send some prompts.

$ uv run streamlit run k8s_mcp_app.py
Enter fullscreen mode Exit fullscreen mode

Streamlit screenshot

Newrelic

Telemetry data should now flow to newrelic, we can visualize data 📊 and here is a screenshot from the dashboard.
Newrelic dashboard screenshot

It's a single dashboard with 3 pages 🗂️ to show App level, Request(Trace) level and Operation(Span) level data. The dashboard is available as json here:

$ ls strands-agent-dashboard.json 
strands-agent-dashboard.json
Enter fullscreen mode Exit fullscreen mode

And this can be imported to newrelic as new dashboard. You may have a look and inform if they are any errors in the dashboard. Ok so that's it for the post, thank you 🤝 for reading.

Top comments (0)