Claude Skills Guide

Claude Code for Elastic APM Integration Workflow

Modern application monitoring requires seamless integration between development tools and observability platforms. Elastic APM (Application Performance Monitoring) provides powerful capabilities for tracking application performance, distributed traces, and errors. When combined with Claude Code, you can create intelligent automation workflows that proactively monitor, analyze, and respond to application health issues.

This guide explores practical approaches to integrating Claude Code with Elastic APM, providing actionable examples for developers looking to enhance their observability stack.

Understanding the Integration Architecture

Claude Code can interact with Elastic APM through multiple pathways: direct API calls to Elasticsearch, webhook integrations from Elastic alerts, or by reading and analyzing APM data exports. The integration enables you to automate monitoring tasks, generate intelligent insights from traces, and build self-healing infrastructure workflows.

The fundamental architecture involves three components:

For authentication, you’ll need an Elasticsearch API key or basic auth credentials. Store these securely using environment variables rather than hardcoding them in scripts.

Setting Up the Basic Connection

Before building complex workflows, establish a reliable connection between Claude Code and your Elastic APM endpoint. Create a configuration file that handles authentication and provides reusable connection functions.

# Store your Elastic credentials securely
export ES_HOST="https://your-elasticsearch-cluster.us-central1.gcp.cloud.es.io"
export ES_API_KEY="your-api-key-here"
export ES_INDEX="apm-*"

Here’s a practical Python script that establishes connectivity and verifies your setup:

import os
from elasticsearch import Elasticsearch

def get_es_client():
    """Initialize Elasticsearch client with APM credentials."""
    return Elasticsearch(
        os.environ.get("ES_HOST"),
        api_key=os.environ.get("ES_API_KEY"),
        verify_certs=True
    )

def verify_connection():
    """Confirm connectivity to Elastic APM."""
    client = get_es_client()
    health = client.cluster.health()
    print(f"Cluster status: {health['status']}")
    return client

Run this script to ensure your credentials work before proceeding with more complex integrations.

Automated Error Detection and Alerting

One of the most valuable workflows involves automatically detecting errors in your APM data and triggering appropriate responses. This section demonstrates how to build an error monitoring pipeline that identifies issues before they impact users.

Create a script that queries for recent errors across your services:

def get_recent_errors(client, service_name=None, time_range="15m"):
    """Query Elastic APM for recent errors."""
    query = {
        "bool": {
            "must": [
                {"term": {"processor.event": "error"}},
                {"range": {"@timestamp": {"gte": f"now-{time_range}"}}}
            ]
        }
    }
    
    if service_name:
        query["bool"]["must"].append({"term": {"service.name": service_name}})
    
    response = client.search(index="apm-*", query=query, size=100)
    return response["hits"]["hits"]

This function enables you to regularly poll for errors and take action. For continuous monitoring, consider running this as a scheduled task or integrating it with a workflow automation tool.

Performance Anomaly Detection

Beyond basic error tracking, Claude Code can help identify performance anomalies by analyzing APM metrics over time. This workflow compares current performance against historical baselines to detect degradation.

def analyze_transaction_times(client, service_name, operation_name):
    """Analyze transaction performance for anomalies."""
    query = {
        "bool": {
            "must": [
                {"term": {"service.name": service_name}},
                {"term": {"transaction.name": operation_name}},
                {"range": {"@timestamp": {"gte": "now-1h"}}}
            ]
        }
    }
    
    response = client.search(
        index="apm-*",
        query=query,
        size=0,
        aggs={
            "avg_duration": {"avg": {"field": "transaction.duration.us"}},
            "p95_duration": {"percentiles": {"field": "transaction.duration.us", "percents": [95]}}
        }
    )
    
    return {
        "average_ms": response["aggregations"]["avg_duration"]["value"] / 1000,
        "p95_ms": response["aggregations"]["p95_duration"]["values"]["95.0"] / 1000
    }

When performance metrics exceed acceptable thresholds, you can trigger alerts, rollbacks, or scaling actions automatically.

Building Distributed Trace Analysis Workflows

Elastic APM excels at distributed tracing, but analyzing traces manually can be overwhelming. Claude Code can automate trace analysis to identify bottlenecks and optimization opportunities.

def find_slow_traces(client, threshold_ms=1000, service=None):
    """Find traces exceeding performance threshold."""
    query = {
        "bool": {
            "must": [
                {"range": {"trace.duration.us": {"gte": threshold_ms * 1000}}},
                {"range": {"@timestamp": {"gte": "now-1h"}}}
            ]
        }
    }
    
    if service:
        query["bool"]["must"].append({"term": {"service.name": service}})
    
    response = client.search(
        index="apm-traces*",
        query=query,
        size=20,
        sort=[{"@timestamp": {"order": "desc"}}],
        _source=["trace.id", "service.name", "transaction.name", "trace.duration.us"]
    )
    
    return [hit["_source"] for hit in response["hits"]["hits"]]

This workflow surfaces the slowest transactions across your system, enabling targeted optimization efforts.

Service Map Generation and Analysis

Elastic APM’s service maps provide visual representations of service dependencies. Claude Code can analyze these relationships to identify critical paths and potential failure points.

def get_service_dependencies(client):
    """Extract service dependency relationships from APM."""
    query = {
        "term": {"service.framework.name": "unknown"}
    }
    
    response = client.search(
        index="apm*",
        query=query,
        size=0,
        aggs={
            "services": {
                "terms": {"field": "service.name", "size": 100},
                "aggs": {
                    "destinations": {
                        "terms": {"field": "span.destination.service.resource", "size": 20}
                    }
                }
            }
        }
    )
    
    dependencies = []
    for service in response["aggregations"]["services"]["buckets"]:
        for dest in service["destinations"]["buckets"]:
            dependencies.append({
                "source": service["key"],
                "target": dest["key"],
                "calls": dest["doc_count"]
            })
    
    return dependencies

Use this data to generate dependency graphs, identify tightly coupled services, or find opportunities for architectural improvements.

Integrating with Incident Response

For production environments, integrate Claude Code workflows with your incident management system. When APM detects issues, automatically create incident tickets, notify on-call engineers, or trigger runbook execution.

def create_incident_from_apm(error_data):
    """Create incident ticket from APM error data."""
    incident = {
        "title": f"APM Alert: {error_data['service.name']} - {error_data['error.grouping_name']}",
        "description": f"Error detected in {error_data['service.name']}\nTrace: {error_data['trace.id']}",
        "severity": "high" if error_data.get("Culprit") else "medium",
        "tags": ["apm", "auto-created", error_data["service.environment"]]
    }
    
    # Integrate with your incident management tool
    # return incident_api.create(incident)
    return incident

Best Practices for Production Deployments

When deploying Claude Code with Elastic APM in production environments, follow these recommendations:

Secure Your Credentials: Never commit API keys or passwords to version control. Use secrets management tools like HashiCorp Vault, AWS Secrets Manager, or environment variables with appropriate access controls.

Implement Rate Limiting: Elastic has rate limits on API requests. Build appropriate throttling into your workflows to avoid triggering quota restrictions.

Optimize Query Performance: Use specific date ranges and filters in your queries. Scanning entire indices can be slow and expensive. Consider using data streams with appropriate rollover policies.

Monitor the Monitor: Track your automation workflows themselves. Ensure that error detection logic remains accurate as your system evolves.

Conclusion

Integrating Claude Code with Elastic APM opens powerful possibilities for automated observability. From real-time error detection to performance analysis and incident response automation, these workflows help maintain application health while reducing manual monitoring burden.

Start with simple error detection scripts, then gradually add more sophisticated analysis capabilities as your monitoring needs evolve. The combination of Claude Code’s flexibility and Elastic APM’s comprehensive data creates a foundation for truly intelligent operational workflows.

Built by theluckystrike — More at zovo.one