How do I correlate logs, metrics, and traces for VictoriaMetrics?

DevOps, VictoriaMetrics, logs, metrics, traces, correlation, observability, monitoring
Learn how to effectively correlate logs, metrics, and traces in VictoriaMetrics to enhance your monitoring and observability practices.
<?php // Sample code to query logs, metrics, and traces in VictoriaMetrics $victoriaMetricsUrl = "http://localhost:8428/api/v1/export"; // Retrieve metrics $metricsQuery = "sum(rate(http_requests_total[5m])) by (status)"; $metricsResponse = file_get_contents($victoriaMetricsUrl . "?query=" . urlencode($metricsQuery)); // Process metrics response $metricsData = json_decode($metricsResponse, true); // Retrieve logs (assumed to be stored in a compatible logging solution) $logsQuery = "SELECT * FROM logs WHERE status = 'error' AND time > NOW() - INTERVAL '1 hour'"; $logsResponse = file_get_contents("http://your-logs-service/api/logs?query=" . urlencode($logsQuery)); $logsData = json_decode($logsResponse, true); // Correlate traces with metrics data $correlateTraces = function($traceId) use ($victoriaMetricsUrl) { $tracesResponse = file_get_contents($victoriaMetricsUrl . "?traceId=". $traceId); return json_decode($tracesResponse, true); }; // Example of calling the correlation function if (!empty($logsData['traceId'])) { $traceData = $correlateTraces($logsData['traceId']); } // Final output echo json_encode([ 'metrics' => $metricsData, 'logs' => $logsData, 'traces' => isset($traceData) ? $traceData : null ]); ?>

DevOps VictoriaMetrics logs metrics traces correlation observability monitoring