APM API Queries Documentation :: Kloudfuse Docs

APM API Queries Documentation

This document provides a comprehensive guide to all Prometheus and GraphQL queries used in the APM (Application Performance Monitoring) system.

Overview

The APM system uses raw spans data to create edge_latency_* metrics that power RED metrics and dependency/service graph metrics. These metrics are exposed through Prometheus and accessed via PromQL queries.

Key Metrics Types

Edge Latency Metrics

All APM metrics are based on the edge_latency_* family of metrics derived from span data.

Common Labels

Service List Page Queries

P99 Latency Calculation

Description: Calculates the 99th percentile latency for all services

histogram_quantile(0.99,
  sum(rate(edge_latency_bucket{span_type!="db"}[${stepInMs}ms]))
  by (service_hash, service_name, le)
)

Parameters:

P95 Latency Calculation

histogram_quantile(0.95,
  sum(rate(edge_latency_bucket{span_type!="db"}[${stepInMs}ms]))
  by (service_hash, service_name, le)
)

P90 Latency Calculation

histogram_quantile(0.90,
  sum(rate(edge_latency_bucket{span_type!="db"}[${stepInMs}ms]))
  by (service_hash, service_name, le)
)

P75 Latency Calculation

histogram_quantile(0.75,
  sum(rate(edge_latency_bucket{span_type!="db"}[${stepInMs}ms]))
  by (service_hash, service_name, le)
)

P50 Latency (Median) Calculation

histogram_quantile(0.50,
  sum(rate(edge_latency_bucket{span_type!="db"}[${stepInMs}ms]))
  by (service_hash, service_name, le)
)

Average Latency

sum by (service_hash, service_name) (rate(edge_latency_sum{span_type!="db"}[${stepInMs}ms]))
/
sum by (service_hash, service_name) (rate(edge_latency_count{span_type!="db"}[${stepInMs}ms]))

Maximum Latency

max(max_over_time(edge_latency_max{span_type!="db"}[${stepInMs}ms]))
by (service_hash, service_name)

Minimum Latency

min(min_over_time(edge_latency_min{span_type!="db"}[${stepInMs}ms]))
by (service_hash, service_name)

Request Count

round(sum by (service_hash, service_name)
  (increase(edge_latency_count{span_type!="db"}[${stepInMs}ms]))
)

Requests Per Second

sum by (service_hash, service_name)
  (rate(edge_latency_count{span_type!="db"}[${stepInMs}ms]))

Error Rate

sum by (service_hash, service_name) (rate(edge_latency_count{span_type!="db",error="true"}[${stepInMs}ms]))
/
sum by (service_hash, service_name) (rate(edge_latency_count{span_type!="db"}[${stepInMs}ms]))

APDEX Score

(sum by (service_hash, service_name) (increase(edge_latency_bucket{span_type!="db",le="1.0"}[${stepInMs}ms]))
 + sum by (service_hash, service_name) (increase(edge_latency_bucket{span_type!="db",le="0.5"}[${stepInMs}ms])))
/
(2 * sum by (service_hash, service_name) (increase(edge_latency_count{span_type!="db"}[${stepInMs}ms])))

Service Details Page Queries

When viewing a specific service, queries are filtered by service_hash:

Service P99 Latency Over Time

histogram_quantile(0.99,
  sum(rate(edge_latency_bucket{service_hash="${serviceHash}"}[${rateIntervalSeconds}]))
  by (${property}, le)
)

Parameters:

Service Request Rate

sum by (${property})
  (rate(edge_latency_count{service_hash="${serviceHash}"}[${rateIntervalSeconds}]))

Service Error Rate

sum by (${property}) (rate(edge_latency_count{service_hash="${serviceHash}",error="true"}[${rateIntervalSeconds}]))
/
sum by (${property}) (rate(edge_latency_count{service_hash="${serviceHash}"}[${rateIntervalSeconds}]))

Downstream Dependencies (Client Services)

For analyzing which services call the current service:

histogram_quantile(0.99,
  sum(rate(edge_latency_bucket{client_service_hash="${serviceHash}"}[${rateIntervalSeconds}]))
  by (service_hash, service_name, le)
)

Trace List Page Queries

Trace queries are handled through GraphQL rather than Prometheus metrics.

GraphQL Queries

Get Services List

query GetServices {
  services(
    filter: {
      attributeFilter: {
        eq: { key: "${customerFilterKey}", value: "${customerFilterValue}" }
      }
    }
    durationSecs: ${durationSecs}
    kfSource: "${kfSource}"
    service: { kfType: "${spanTypeFilter}" }
    timestamp: "${endTime}"
  ) {
    name
    distinctLabels
    labels
    hash
    kfType
  }
}

Get Traces

{
  traces(
    durationSecs: ${durationSecs}
    filter: ${buildTracesFilter(...)}
    limit: ${limit}
    pageNum: ${pageNum}
    timestamp: "${endTime}"
    sortField: "${sortBy}"
    sortOrder: ${sortOrder}
  ) {
    traceId
    span {
      spanId
      parentSpanId
      startTimeNs
      endTimeNs
      attributes
      durationNs
      name
      service {
        name
        labels
        hash
        distinctLabels
      }
      statusCode
      method
      endpoint
      rootSpan
    }
    traceMetrics {
      spanCount
      serviceExecTimeNs
    }
  }
}

Get SLOs

{
  listSLOs {
    id
    name
    type
    service {
      name
      hash
      distinctLabels
      kfType
      labels
    }
    goodEventsSLIQuery
    totalEventsSLIQuery
    matchers
    latencyThreshold
    objective
    description
    timeWindow
    alertUid
    contactPoints
  }
}

Database-Specific Queries

For database operations, queries filter by span_type="db":

Database P99 Latency

histogram_quantile(0.99,
  sum(rate(edge_latency_bucket{span_type="db"}[${stepInMs}ms]))
  by (service_hash, service_name, le)
)

Database Request Count

round(sum by (service_hash, service_name)
  (increase(edge_latency_count{span_type="db"}[${stepInMs}ms]))
)

Common Query Parameters

Time Windows

Filters

Aggregation

Usage Examples

Example 1: Get P99 latency for a specific service

curl -X POST http://api.example.com/prometheus/api/v1/query \
  -d 'query=histogram_quantile(0.99, sum(rate(edge_latency_bucket{service_hash="abc123"}[5m])) by (le))'

Example 2: Get all services via GraphQL

curl -X POST http://api.example.com/graphql \
  -H "Content-Type: application/json" \
  -d '{
    "query": "{ services(durationSecs: 3600, timestamp: \"2024-01-01T00:00:00Z\") { name hash } }"
  }'

Example 3: Calculate service error rate over last hour

curl -X POST http://api.example.com/prometheus/api/v1/query \
  -d 'query=sum by (service_name) (rate(edge_latency_count{error="true"}[1h])) / sum by (service_name) (rate(edge_latency_count[1h]))'

Notes

  1. All latency values in Prometheus metrics are in milliseconds

  2. GraphQL timestamps use ISO 8601 format

  3. The span_type!="db" filter excludes database operations from general service metrics

  4. APDEX thresholds are typically 0.5s (satisfied) and 1.0s (tolerable)

  5. Rate intervals should be at least 4x the scrape interval for accuracy