| scribe |
|
|---|
TL;DR: The graph engine builds a directed traffic graph by querying Prometheus for Istio telemetry (
istio_requests_totaland related metrics), constructing aTrafficMapof typed nodes and edges, then running a pipeline of appenders that enrich the graph with health, security policy, Istio config decorations, and Ambient/waypoint topology. Namespace graph results are cached per browser session and refreshed in the background; node-detail graphs are never cached.
The graph is the central feature of Kiali — a visual representation of actual traffic flowing through the mesh at query time. The graph engine is responsible for:
- Translating HTTP request parameters into a well-typed
Optionsstruct. - Querying Prometheus for raw traffic telemetry.
- Building a
TrafficMap(directed graph of nodes and edges) from the query results. - Enriching the graph via an ordered pipeline of
Appenderimplementations. - Converting the internal
TrafficMapinto a serialisable config format (currentlygraph/config/common) for the frontend. - Caching the resulting graph per session and refreshing it in the background.
All graph code lives under graph/. The Istio-specific telemetry implementation lives under graph/telemetry/istio/.
type TrafficMap map[string]*NodeA map from node ID to *Node. IDs are deterministic strings derived from node identity (cluster + namespace + workload/app/version/service + graph type).
type Node struct {
ID string
NodeType string // "app" | "service" | "workload" | "box" | "aggregate" | "unknown"
Cluster string
Namespace string
Workload string
App string
Version string
Service string
Edges []*Edge
Metadata Metadata // open-ended key-value enrichment
}Node types:
NodeType |
Description |
|---|---|
app |
An app-label group; in versionedApp graph type this includes Version |
workload |
A concrete Kubernetes workload controller |
service |
A Kubernetes Service (injected when injectServiceNodes=true) |
box |
A grouping node (isBox metadata = "app" |
aggregate |
A traffic aggregation node for a Prometheus metric attribute |
unknown |
Traffic from sources outside the mesh |
When constructing nodes, unnecessary fields are trimmed per type: service nodes drop App/Workload/Version; workload nodes with Unknown app or version labels have those fields cleared.
type Edge struct {
Source *Node
Dest *Node
Metadata Metadata
}Edges carry traffic protocol, response codes, rates, and security policy metadata.
BlackHoleCluster/PassthroughCluster— Envoy internal cluster names for dropped/passthrough traffic; marked withIsEgressClustermetadata.Unknown— the"unknown"sentinel used when Istio cannot determine source/destination labels.
graph/meta.go defines typed constants for all metadata keys used on nodes and edges:
Node metadata keys (selection):
| Key | Type | Meaning |
|---|---|---|
isAmbient |
bool | Node is in the Ambient mesh |
isDead |
bool | No backing workload found |
isEgressCluster |
bool | BlackHole or Passthrough cluster |
isEgressGateway |
bool | Istio egress gateway |
isGatewayAPI |
bool | Gateway API ingress gateway |
isIngressGateway |
bool | Istio ingress gateway |
isIdle |
bool | No incoming traffic (idle service) |
isInaccessible |
bool | Outside caller's RBAC scope |
isMTLS |
bool | Edge is mTLS-encrypted |
isOutOfMesh |
bool | Missing sidecar or not ambient |
isOutside |
bool | Node is outside the requested namespaces |
isRoot |
bool | Traffic source with no inbound edges |
isServiceEntry |
bool | Backed by an Istio ServiceEntry |
isWaypoint |
bool | Ambient waypoint proxy node |
hasCB |
bool | Has circuit breaker (DestinationRule) |
hasFaultInjection |
bool | VirtualService fault injection |
hasMirroring |
bool | VirtualService traffic mirroring |
hasTrafficShifting |
bool | VirtualService traffic shifting |
hasVS |
bool | Has an associated VirtualService |
hasWorkloadEntry |
bool | Backed by a WorkloadEntry |
healthData |
any | Raw health data for the node |
healthStatus |
string | Calculated health status |
labels |
map | Kubernetes labels |
responseTime |
float64 | p50/p95/p99/avg response time |
throughput |
float64 | Bytes/sec throughput |
destPrincipal |
string | mTLS destination principal |
sourcePrincipal |
string | mTLS source principal |
Edge metadata keys include: protocol, responseTime, throughput, isMTLS, destServices.
graph/options.go defines all parameters for a single graph request.
type Options struct {
ConfigVendor string // "common" (default)
TelemetryVendor string // "istio" (default)
ConfigOptions
TelemetryOptions
}Key fields:
| Field | Type | Default | Description |
|---|---|---|---|
Duration |
time.Duration |
10m | Prometheus look-back window |
GraphType |
string | "workload" |
"workload" | "app" | "versionedApp" | "service" |
QueryTime |
int64 | now | Unix timestamp for the query |
Namespaces |
NamespaceInfoMap |
— | Namespaces to graph |
IncludeIdleEdges |
bool | false | Include edges with 0 traffic |
InjectServiceNodes |
bool | false | Insert Service nodes between source and dest |
Rates.Http |
string | "requests" |
"requests" | "none" |
Rates.Grpc |
string | "requests" |
"requests" | "sent" | "received" | "total" |
Rates.Tcp |
string | "sent" |
"sent" | "received" | "total" | "none" |
Rates.Ambient |
string | "total" |
"none" | "total" | "waypoint" | "ztunnel" |
BoxBy |
string | "none" |
"app" | "cluster" | "namespace" | "none" |
Appenders |
RequestedAppenders |
all | Comma-separated appender names, or all |
AccessibleNamespaces |
AccessibleNamespaces |
— | The requesting user's accessible namespaces, keyed by cluster/namespace. Authoritative scoping for appender fetches |
SessionID |
string | — | Browser session cookie value (for cache keying) |
RefreshInterval |
time.Duration |
— | Requested background refresh interval |
Used for single-node detail views:
type NodeOptions struct {
Aggregate string
AggregateValue string
App string
Cluster string
Namespace NamespaceInfo
Service string
Version string
Workload string
}NewOptions(r, businessLayer, conf) parses all of the above from the HTTP request's path variables and query parameters.
GraphNamespaces(ctx, business, prom, o) and GraphNode(ctx, business, prom, o) are the two public functions called by the HTTP handlers.
For the Istio telemetry vendor, both functions call into graph/telemetry/istio/istio.go.
The algorithm (from the package comment):
Step 1) For each namespace:
a) Query Prometheus to retrieve source-destination dependencies.
Build a namespace-level TrafficMap.
b) Apply namespace-scoped appenders (non-finalizers) to the namespace map.
c) Merge the namespace map into the global TrafficMap.
Step 2) For the global TrafficMap:
a) Apply finalizer appenders to the complete graph.
b) Convert to the requested config format (Common) and return.
If graphType == "service", telemetry.ReduceToServiceGraph() condenses the final map.
MergeTrafficMaps (in graph/telemetry/common.go) merges two maps by preferring the namespace-local copy of duplicate nodes (which has all appender info applied) and deduplicating edges.
Before querying namespaces, if Rates.Ambient != "none", the engine calls GetWaypointMap() to build a lookup of ambient waypoint nodes. This is used by the AmbientAppender finalizer to correctly model waypoint-routed traffic.
buildNamespaceTrafficMap in istio.go fires multiple Prometheus instant-vector queries for each namespace:
The query group-by labels are:
source_cluster, source_workload_namespace, source_workload,
source_canonical_service, source_canonical_revision,
destination_cluster, destination_service_namespace,
destination_service, destination_service_name,
destination_workload_namespace, destination_workload,
destination_canonical_service, destination_canonical_revision,
request_protocol, response_code, grpc_response_status, response_flags
Up to four queries are issued per namespace:
- Incoming source telemetry — captures failed requests that never reach a destination workload (unserviced namespace services).
- Incoming Ambient source telemetry — for Ambient namespaces only (conditional); captures traffic from non-waypoint ingress gateways that would not appear in destination telemetry.
- Incoming destination telemetry — the primary query; captures all traffic arriving at workloads in the namespace.
- Outgoing source telemetry — captures traffic leaving the namespace.
The reporter label filter (source vs destination) is controlled by util.GetReporter(side, rates).
Similar query structure for TCP byte-rate traffic.
Detected by grpcMetric = regexp.MustCompile("istio_.*_messages") and handled separately.
populateTrafficMap(ctx, trafficMap, &trafficVector, metric, o, globalInfo) iterates the Prometheus result vector and creates or updates Node and Edge entries in the map. Nodes are created with NewNode(cluster, serviceNS, service, workloadNS, workload, app, version, graphType) which computes a deterministic ID.
A SHA-256 hash (tsHash) of each time-series label set is stored in node metadata to detect whether telemetry has changed between cache refreshes.
type Appender[T any] interface {
AppendGraph(ctx context.Context, trafficMap TrafficMap,
globalInfo *GlobalInfo[T],
namespaceInfo *AppenderNamespaceInfo[T])
IsFinalizer() bool // true = runs once on the final merged graph
Name() string // used to select appenders via query param
}GlobalInfo[T] holds the business layer, Prometheus client, config, cluster list, and vendor-specific Vendor T (*GlobalIstioInfo for Istio).
GlobalIstioInfo caches data that multiple appenders share:
AmbientWaypoints— map of waypoint node keysAppsMap— app list items keyed bycluster:namespaceServiceEntryHosts— service entry host setsServiceLists— service lists per namespaceWorkloadLists— workload lists per namespaceWorkloadMap— workload-to-node mapping (finalizers only)
Namespace authorization in appenders: globalInfo.Clusters spans every Kiali-visible cluster, but the user only holds clients for the clusters they have a session on. Any appender doing cluster-wide fetches (the IstioAppender's service lists and Istio config, the ServiceEntryAppender, the WorkloadEntryAppender) must carry TelemetryOptions.AccessibleNamespaces and skip clusters without a user client — AccessibleNamespaces.HasCluster(name) reports whether one exists. An unguarded cluster-wide fetch panics in graph.CheckError and fails the whole graph with a 500 for any user not logged into every cluster. Nodes from skipped clusters render without that appender's decoration. New appenders must follow the same pattern; see "Namespace Access Control" in docs/agents/auth-and-security.md.
Namespace-scoped appenders run per namespace in this order:
ServiceEntryAppender— marks nodes backed by ServiceEntry objects; must run first so other appenders can rely onIsServiceEntry.DeadNodeAppender— removes nodes with no traffic and no backing workload (reduces noise for subsequent appenders).WorkloadEntryAppender— decorates nodes backed by WorkloadEntry objects.ResponseTimeAppender— queries Prometheus foristio_request_duration_millisecondshistograms (p50/p95/p99/avg) and attachesresponseTimemetadata to edges and nodes.SecurityPolicyAppender— queries Prometheus for mTLSconnection_security_policylabels and attachesisMTLS/ principal metadata.ThroughputAppender— queries Prometheus for byte-rate metrics and attachesthroughputmetadata.AggregateNodeAppender— injects aggregate nodes for a specified Prometheus metric attribute (defaultrequest_operation).IdleNodeAppender— injects service nodes that exist but have no active traffic (only for service-type graphs withinjectServiceNodes).MeshCheckAppender— marks nodes that are out of mesh (isOutOfMesh).
Finalizer appenders run once on the complete merged graph:
ExtensionsAppender— adds nodes/edges from configured external extensions (always runs first).OutsiderAppender— marks nodes outside the requested namespaces asisOutside/isInaccessible(always runs).IstioAppender— decorates nodes and edges with Istio config presence (hasVS,hasCB,hasFaultInjection, etc.).AmbientAppender— models Ambient mesh waypoint routing; re-wires edges to surface waypoint nodes.HealthAppender— computes and attaches health status to all nodes using the traffic data collected during graph generation (runs afterOutsiderAppenderso inaccessible nodes are skipped).LabelerAppender— attaches Kubernetes labels to nodes.TrafficGeneratorAppender— marks root nodes that are pure traffic generators (always runs last).
Callers can request a subset of appenders via the appenders query parameter (comma-separated names). The OutsiderAppender and TrafficGeneratorAppender always run regardless.
| Name constant | Type | Query param name | Purpose |
|---|---|---|---|
AggregateNodeAppenderName |
namespace | aggregateNode |
Inject aggregate traffic nodes |
DeadNodeAppenderName |
namespace | deadNode |
Remove stale nodes |
IdleNodeAppenderName |
namespace | idleNode |
Add idle service nodes |
MeshCheckAppenderName |
namespace | meshCheck |
Mark out-of-mesh nodes. Also accepts legacy alias sidecarsCheck (maps to the same appender for backward compatibility) |
ResponseTimeAppenderName |
namespace | responseTime |
Attach p50/p95/p99/avg latency |
SecurityPolicyAppenderName |
namespace | securityPolicy |
Attach mTLS metadata |
ServiceEntryAppenderName |
namespace | serviceEntry |
Mark ServiceEntry-backed nodes |
ThroughputAppenderName |
namespace | throughput |
Attach byte-rate throughput |
WorkloadEntryAppenderName |
namespace | workloadEntry |
Mark WorkloadEntry-backed nodes |
AmbientAppenderName |
finalizer | ambient |
Model waypoint routing |
HealthAppenderName |
finalizer | health |
Attach health status |
IstioAppenderName |
finalizer | istio |
Attach Istio config decorations |
LabelerAppenderName |
finalizer | labeler |
Attach Kubernetes labels |
ExtensionsAppenderName |
finalizer | (always) | Add extension nodes |
OutsiderAppenderName |
finalizer | (always) | Mark outside/inaccessible nodes |
TrafficGeneratorAppenderName |
finalizer | (always) | Mark traffic generator roots |
Only namespace graphs are cached. Node-detail graphs (GraphNode) are always generated fresh. Namespace graphs are cached per browser session — keyed by the session cookie value (SessionID) extracted from each request. The per-session (not per-options) key is a deliberate security choice: different users have different RBAC scopes, so cross-user cache sharing would risk leaking data from one user's namespace view into another's. Multiple tabs in the same browser share one session and one cached graph. Different browsers or incognito windows have separate sessions.
Graph caching is enabled by default (kiali_internal.graph_cache.enabled: true) and configured under the kiali_internal.graph_cache section (a deliberately obscure path — this is an internal tuning knob, not a user-facing setting). Defaults: refresh_interval: "60s", inactivity_timeout: "10m", max_cache_memory_mb: 1000.
| Field | Type | Default | Purpose |
|---|---|---|---|
Enabled |
bool | true | Enable/disable graph caching globally |
RefreshInterval |
string (duration) | "60s" |
Background refresh period |
InactivityTimeout |
string (duration) | "10m" |
Evict session graphs idle longer than this |
MaxCacheMemoryMB |
int | 1000 | Memory cap across all cached sessions |
GraphCacheImpl is the concrete type created by NewGraphCache(ctx, config) in router.go. Its sessionGraphs map[string]*CachedGraph is the in-memory store, protected by a sync.RWMutex.
GetSessionGraph updates LastAccessed on every read. The internal method getSessionGraphInternal reads without updating LastAccessed — used by the RefreshJob so that background checks don't artificially extend session lifetimes.
Memory estimation: EstimateGraphMemory(trafficMap) computes (nodes × 3 KB) + (edges × 1 KB) × 1.1 overhead. The estimate is calculated once on first SetSessionGraph and reused on subsequent updates — on the assumption that graph size is stable between refreshes.
Memory enforcement: before storing a new graph, checkMemoryLimits evicts the least-recently-used sessions (evictLRU) until the projected total falls below MaxCacheMemoryMB. Evictions are tracked by the kiali_graph_cache_evictions_total Prometheus counter (cache hits and misses are tracked by kiali_graph_cache_hits_total and kiali_graph_cache_misses_total).
Every GraphNamespaces request passes through graphNamespacesWithCache:
- Cache disabled or no SessionID → call
api.GraphNamespacesdirectly (no caching). - Historical query (
QueryTimeProvided = true, explicitqueryTimeparam) → bypass cache, generate fresh graph, leave any existing cache/job intact. This allows graph replay without disrupting the live background refresh. - Client bypass (
RefreshInterval <= 0) → stop the session's refresh job, evict the cached graph, generate fresh graph. Used when the user turns off auto-refresh in the UI. - Cache hit + options match → return the cached
TrafficMapconverted to config format immediately. If the requestedRefreshIntervalhas changed, calljob.UpdateInterval(newInterval)to adjust the background ticker without interrupting the running job. - Cache miss or options mismatch → generate graph via
api.GraphNamespaces, store in cache, start a newRefreshJobfor the session.
graphOptionsMatch compares namespaces, duration, graph type, inject-service-nodes, idle-edges, boxBy, appenders, and rate settings. It deliberately ignores QueryTime — the background refresh handles time progression automatically.
Each session gets one RefreshJob goroutine, managed by a RefreshJobManager (one per server, created in routing/router.go). The job lifecycle:
Start pattern (interval/2 offset):
NewRefreshJob → go job.Start()
|-- wait interval/2 --> fire first refresh
|-- create time.NewTicker(interval) --> fire subsequent refreshes
The first refresh fires at interval/2 rather than interval. This reduces worst-case staleness when a user first loads the graph: on average the cached data will be at most interval/2 old rather than interval old. This is a heuristic approximation, not a hard guarantee.
Refresh cycle (refresh method):
getSessionGraphInternal— checks the session's graph exists without touchingLastAccessed.- Inactivity check — if
time.Since(LastAccessed) > InactivityTimeout, evict and stop. - Update
Options.QueryTimetotime.Now()in a copy — this is the moving time window that keeps the cached graph current. - Call
graphGenerator(ctx, refreshedOptions)— theGraphGeneratorfunction (typefunc(ctx, Options) (TrafficMap, error)) injected at cache-miss time. - On success: call
SetSessionGraphwith the freshTrafficMap, preserving the originalLastAccessedtimestamp. - On error: log and return — the stale graph remains in cache. The job will retry on the next tick.
Panic recovery: If graphGenerator panics, the deferred recovery logs the stack trace, evicts the session graph, and calls Stop(). A panic means the refresh cycle is broken, and serving a stale graph would be misleading — the next user request becomes a cache miss and regenerates from scratch.
Interval changes: UpdateInterval(newInterval) cancels any pending ticker, starts a goroutine that waits newInterval/2 then fires a refresh, then sends a new time.Ticker to the Start loop via resetChan. A context (updateIntervalCancel) guards against concurrent UpdateInterval calls racing each other.
Stopping: Stop() closes stopChan and cancels the job's context, which unblocks the select in Start. The RefreshJobManager.StopAll() is called on server shutdown.
graph/api/api.go exposes:
func GraphNamespaces(ctx, business, prom, o) (code int, graphConfig interface{}, trafficMap TrafficMap)
func GraphNode(ctx, business, prom, o) (code int, graphConfig interface{})graphNamespacesIstio:
- Gets cluster list from
business.Mesh.Clusters(). - Creates
GlobalInfo(business layer, Prometheus client, config, clusters,NewGlobalIstioInfo()). - Calls
istio.BuildNamespacesTrafficMap(ctx, o.TelemetryOptions, globalInfo). - Calls
generateGraph(ctx, trafficMap, o)which invokes the config vendor'sNewConfig()to produce the serialisable graph config.
The HTTP handlers in handlers/ extract Options from the request, check the session graph cache, and call these functions only on a cache miss or invalidation.
Internal Prometheus metrics track graph generation time (GetGraphGenerationTimePrometheusTimer), per-appender time (GetGraphAppenderTimePrometheusTimer), and total node count (SetGraphNodes).
prometheus/client.go provides:
Clientstruct wrappingprom_v1.APIwith helper methods for instant and range queries.QueryRecorder— aprom_v1.APIwrapper that logs all queries and their results to a file (used for test recording).
The ClientInterface is the abstraction used throughout the business layer and graph engine. A no-op implementation (noop_client.go) is available for testing.
prometheus/metrics.go defines helper types for building Prometheus label matchers, and prometheus/types.go defines response data structures used by the business layer's metrics/dashboards feature.
The graph engine calls Prometheus via graph.PromQuery(ctx, query, ts, promApi, conf) (defined in graph/util.go), which wraps promApi.Query() with error handling that panics with a structured graph.Error — these panics are caught at the handler level and converted to HTTP 500 responses.