Is it possible to log the agent stdout logging out...
# ask-ai
g
Is it possible to log the agent stdout logging output to newrelic, possibly with open telemetry? The scale plan includes Metrics export APIs -> how can I use those and send them to newrelic, as well?
t
Thanks for reaching out, Haim. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks for reaching out! To help you better, could you please share a bit more about your main goal and setup? • Are you aiming to use OpenTelemetry specifically for logging, or would the automatic New Relic log forwarding for stdout work well for you? • Are you planning to send just stdout/stderr logs, traces and spans, or both logs and traces through OpenTelemetry? • Also, are you using LiveKit Cloud or are you self-hosting your agents? Sources: Log collection | LiveKit Docs | Logs, metrics, and telemetry | LiveKit Docs | livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/log.py
g
I aim to log whatever I can - the more the better. I would prefer to use new relic log forwarding if thats easily compatible with livekit - only directly with opentelemetry if there's no other way.. both logs and traces. We are self hosting the agents.
t
Hi there! To help you log both stdout/stderr logs and traces to New Relic for your self-hosted LiveKit agents, here are a couple of friendly approaches you can try: 1. New Relic Log Forwarding This is the easiest way to get your logs forwarded: • Just set the
NEW_RELIC_LICENSE_KEY
as an environment variable or secret in your deployment environment. For example:
Copy code
# Environment Variable Method
export NEW_RELIC_LICENSE_KEY="your_license_key_here"

# Docker Environment
ENV NEW_RELIC_LICENSE_KEY="your_license_key_here"

# Kubernetes Secret
apiVersion: v1
kind: Secret
metadata:
  name: agent-secrets
type: Opaque
stringData:
  NEW_RELIC_LICENSE_KEY: "your_license_key_here"
Once you have this set up, all your runtime logs (including stdout/stderr) will be automatically sent over to New Relic, making things much smoother! 2. OpenTelemetry for Traces If you want to send traces and spans, you can configure OpenTelemetry right in your agent's entrypoint like this:
Copy code
from livekit.agents.telemetry import set_tracer_provider
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter

def setup_telemetry():
    trace_provider = TracerProvider()
    otlp_exporter = OTLPSpanExporter(
        endpoint="http://your-otel-collector:4318/v1/traces",
        headers={"Authorization": "Bearer your-token"}  # if required
    )
    trace_provider.add_span_processor(BatchSpanProcessor(otlp_exporter))
    set_tracer_provider(trace_provider)
    return trace_provider
Just plug this setup into your agent's main entrypoint, and you’ll be able to capture traces for session activities, LLM operations, and more. It’s a great way to keep an eye on what’s happening under the hood! Additional Notes • Make sure your network can reach New Relic's ingestion endpoints and your OpenTelemetry collector without any hiccups. • If things aren’t working as expected, double-check that your
NEW_RELIC_LICENSE_KEY
is set correctly and accessible. • OpenTelemetry can also handle logs, but that involves a bit more setup beyond what’s shown here. If you have any questions or want a hand with specific configuration steps, please don’t hesitate to reach out—I’m here to help! Sources: Logs, metrics, and telemetry | LiveKit Docs | Log collection | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py
g
The scale plan includes Metrics export APIs -> how can I use those? Are there any docs?
t
I'm glad you're interested in the Metrics Export APIs available in the LiveKit Scale plan! These APIs make it easy to export analytics and telemetry data from agent sessions to external systems. Let me walk you through how you can use them: 1. Event Handling for Metrics Collection You can set up event handlers right in your agent code to capture metrics as they happen. For example:
Copy code
@agent.on("metrics_collected")
async def on_metrics_collected(metrics: AgentMetrics):
    print(logMetrics(metrics))
This way, you’ll be capturing important data like LLM, STT, TTS, VAD, and EOU during your sessions. 2. Exporting Metrics to Third-Party Systems If you want to send these metrics to an external system, you can create an event handler that converts the metrics to JSON and sends them via HTTP. Here’s a simple example:
Copy code
# Use async HTTP requests to send metrics to an external server
async def export_metrics(metrics):
    async with aiohttp.ClientSession() as session:
        await session.post("<YOUR_METRICS_SERVER_URL>", json=metrics)
3. Aggregating Usage Metrics To get a bigger picture, you can use the
UsageCollector
class to gather metrics across a session—this is really helpful for estimating costs and analyzing performance:
Copy code
collector = UsageCollector()
collector.add_metrics(metrics)
summary = collector.build_summary()
4. OpenTelemetry Integration For even deeper insights, you might consider integrating with OpenTelemetry. This lets you trace agent session flows and node-level operations, and you can combine it with third-party tools like Langfuse for advanced analysis. If you’d like to explore more, feel free to check out the LiveKit Python Agents Examples repository and the LiveKit Agents Metrics documentation. I'm here to help, so please don’t hesitate to reach out if you want any help with specific setups or code examples! Sources: livekit/livekit_metrics.pb.go | lib/livekit/proto/livekit_metrics_pb.rb | livekit-protocol/livekit/protocol/metrics.py | LiveKit Pricing | docs/index.yaml