Enable telemetry in your service
Add two lines at startup before you create yourServiceHost:
Python
Example: Weather Toolkit with custom instrumentation
The example adds custom spans around validation, the external HTTP call, and response parsing. It also adds logs and metrics.Custom Spans
Use spans to track a specific operation in trace. Spans can help you understand timing for each step and attach attributes/events to make debugging easier. This example records:toolkit.execute(created by MeshAgent) this is the high-level tool call including information about which tool was called and what information was passed to the toolvalidate_input– checks city/units, adds events for pass/failfetch_weather_api– the external HTTP call with:http.url,http.method,http.status_code,http.response_size- events like
api_request_start,api_request_success,api_timeout,rate_limit_exceeded
parse_response– extracts the fields you return to the caller
Python
Custom Logs
otel_config() sets up an OTEL logging handler so your normal logging.info() lines are captured. Add extra logs to make it easier to debug and understand…
Python
Custom Metrics
You can also add additional counters and historgrams to help track trends over time.Python
Running the sample
You can deploy and run this toolkit as a service and invoke the weather tool,get_weather(), from Studio or the CLI.
Step 1: Package and deploy the room service
To deploy the otel service permanently, you’ll package your code with ameshagent.yaml file that defines the service configuration and a container image that MeshAgent can run.
For full details on the service spec and deployment flow, see Packaging Services and Deploying Services.
MeshAgent supports two deployment patterns for containers:
- Runtime image + code mount (recommended): Use a pre-built MeshAgent runtime image (like
python-sdk-slim) that contains Python and all MeshAgent dependencies. Mount your lightweight code-only image on top. This keeps your code image tiny (~KB), eliminates dependency installation time, and allows your service to start quickly. - Single Image: Bundle your code and all dependencies into one image. This is good when you need extra libraries that aren’t already in the runtime image, but can result in larger images and slower pulls.
python-docs-examples image so you can still run the documentation examples without building your own images. If you want to build and push your own code image, follow the steps below and update the image mount section of the meshagent.yaml file.
Prepare your project structure
This example organizes the tool code and configuration in the same folder, making each sample self-contained:
Note: If you’re building a single tool, you only need the observability/ folder. The structure shown supports multiple samples sharing one Dockerfile.
Step 1a: Build a Docker container
Create a scratch Dockerfile and copy the files you want to run. This creates a minimal image containing only your code files.
docker buildx:
bash
Note: Building from the project root copies your entire project structure into the image. For a single tool, this is fine - your image will just contain one folder. For multi-tool projects, all samples will be in one image, but each can deploy independently using its own meshagent.yaml.
Step 1b: Package the service
Define the service configuration in a meshagent.yaml file. This service will have a container section that references:
- Runtime image: The MeshAgent Python SDK image with all dependencies
- Code mount: Your code-only image mounted at /src
- Command path: Points to your sample’s specific location
- Your code image contains
/observability/observability.py - It’s mounted at
/srcin the runtime container - The command runs
python /src/observability/observability.py
Note: The default YAML in the docs uses us-central1-docker.pkg.dev/meshagent-public/images/python-docs-examples so you can test this example immediately without building your own image first. Replace this with your actual image tag when deploying your own code.
Step 1c: Deploy the service
Next from the CLI in the directory where your meshagent.yaml file is run:
Step 2: Invoking the tool
Once you’ve created the service you can invoke the tool directly from MeshAgent Studio by going into the room, clicking the menu icon, selecting Toolkits, then select the Weather Tool and click Invoke. The UI will display a dialog for you to input the city and units you want to get the weather from. You can also invoke the tool using the MeshAgent CLI:bash
Viewing telemetry
From MeshAgent Studio you will see Logs, Traces, and Metrics both from the Session view of the room and from the Developer Console. The Session viewer provides a focused view of session details which are accessible during and after each session. The Developer Console appears on the lower part of the screen once you’re inside a room and shows live traces, logs, and metrics as you interact with your services inside a room. After invoking the weather tool in this example you should see a trace tree appear in the Traces tab with a structure like this:Next Steps
- Agents: Understand how agents work in MeshAgent and start building your first one!
- Tools: Learn how to build tools human and agent participants can use inside a MeshAgent room.
- Services & Containers: Learn how to deploy instrumented agents and tools as project wide or room specific services in MeshAgent