Is there a datadog integration for LiveKit Cloud?
# ask-ai
b
Is there a datadog integration for LiveKit Cloud?
t
Thanks for reaching out, Mike. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Absolutely! LiveKit Cloud does support integration with Datadog for log forwarding and analytics. Let me walk you through how to get it set up: 1. Add Datadog Secrets: First, you'll want to add your Datadog API token and region as secrets in your LiveKit Cloud project. For example, you can run:
lk agent update-secrets --secrets "DATADOG_TOKEN=your-client-token"
2. Initialize Datadog Logs: Next, in your client-side code, initialize Datadog logs using environment variables. If you’re using a Next.js app, you’d set
NEXT_PUBLIC_DATADOG_CLIENT_TOKEN
and
NEXT_PUBLIC_DATADOG_SITE
in your
.env.local
file. 3. Set Log Extension: Then, use the LiveKit SDK’s
setLogExtension
method to forward those logs to Datadog. Here’s an example:
Copy code
datadogLogs.init({
  clientToken: process.env.NEXT_PUBLIC_DATADOG_CLIENT_TOKEN,
  site: process.env.NEXT_PUBLIC_DATADOG_SITE,
});

setLogExtension((level, msg, context) => {
  switch (level) {
    case LogLevel.debug:
      datadogLogs.logger.debug(msg, context);
      break;
    case <http://LogLevel.info|LogLevel.info>:
      <http://datadogLogs.logger.info|datadogLogs.logger.info>(msg, context);
      break;
    case LogLevel.warn:
      datadogLogs.logger.warn(msg, context);
      break;
    case LogLevel.error:
      datadogLogs.logger.error(msg, context);
      break;
  }
});
4. Example Application: If you want a hands-on example, check out the LiveKit Meet example—it shows this integration in action and can be a great reference. With this setup, you’ll be able to keep a close eye on your app’s performance and troubleshoot any issues smoothly using Datadog’s analytics. If you have any questions along the way, just let me know—I’m here to help!
b
I just want metrics and not logs integration
t
To integrate LiveKit Cloud with Datadog for metrics only, here’s a simple step-by-step guide to help you out: 1. Configure Datadog Secrets: First, set your Datadog client token as a secret using the LiveKit CLI: `lk agent update-secrets --secrets "DATADOG_TOKEN=yourDatadogClientToken"`If you want, you can also specify the Datadog region like this:
lk agent update-secrets --secrets "DATADOG_REGION=us1"
2. Instrument Your Agent: Use the
log_metrics
helper function to handle metrics events. Here’s an example in Python:
Copy code
from livekit.agents import metrics, AgentSession

@session.on("metrics_collected")
def _on_metrics_collected(ev: MetricsCollectedEvent):
    metrics.log_metrics(ev.metrics)
3. Aggregate Metrics (Optional): If you want to summarize session metrics, you can use the
UsageCollector
like this:
Copy code
from livekit.agents import metrics, AgentSession

usage_collector = metrics.UsageCollector()

@session.on("metrics_collected")
def _on_metrics_collected(ev: MetricsCollectedEvent):
    usage_collector.collect(ev.metrics)

async def log_usage():
    summary = usage_collector.get_summary()
    <http://logger.info|logger.info>(f"Usage: {summary}")

ctx.add_shutdown_callback(log_usage)
4. Set Up Datadog to Parse Logs as Metrics: Take advantage of Datadog’s log-to-metrics feature to turn your log data into metrics. Just make sure your logs are tagged or structured so Datadog can easily recognize them as metrics. 5. Verify Metrics: Finally, check your Datadog dashboard to see if the metrics are showing up as expected. You can use tags from your LiveKit agent logs to help with querying. This setup should make it straightforward to monitor and analyze your metrics in Datadog. If you run into any questions or need a hand with anything else, just let me know—I’m here to help! Sources: Capturing metrics | LiveKit Docs | pnpm-lock.yaml | metrics/send-metrics-to-3p/metrics_server/README.md | Log collection | LiveKit Docs