|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +// MetricResponse represents the structure of the API response |
| 14 | +type MetricResponse struct { |
| 15 | + Status string `json:"status"` |
| 16 | + Data struct { |
| 17 | + ResultType string `json:"resultType"` |
| 18 | + Result []struct { |
| 19 | + Metric map[string]string `json:"metric"` |
| 20 | + Value []interface{} `json:"value"` |
| 21 | + } `json:"result"` |
| 22 | + } `json:"data"` |
| 23 | +} |
| 24 | + |
| 25 | +func TestLogzioMonitoringMetrics(t *testing.T) { |
| 26 | + metricsApiKey := os.Getenv("LOGZIO_METRICS_API_KEY") |
| 27 | + if metricsApiKey == "" { |
| 28 | + t.Fatalf("LOGZIO_METRICS_API_KEY environment variable not set") |
| 29 | + } |
| 30 | + |
| 31 | + metricResponse, err := fetchMetrics(metricsApiKey) |
| 32 | + if err != nil { |
| 33 | + t.Fatalf("Failed to fetch metrics: %v", err) |
| 34 | + } |
| 35 | + |
| 36 | + requiredMetrics := map[string][]string{ |
| 37 | + "kube_pod_status_phase": {"p8s_logzio_name", "namespace", "pod", "phase", "uid"}, |
| 38 | + "kube_pod_info": {"p8s_logzio_name", "namespace", "host_ip", "node", "pod"}, |
| 39 | + "container_cpu_usage_seconds_total": {"p8s_logzio_name", "namespace", "pod", "region", "topology_kubernetes_io_region", "container"}, |
| 40 | + "kube_pod_container_resource_limits": {"p8s_logzio_name", "namespace", "pod", "resource"}, |
| 41 | + "container_memory_working_set_bytes": {"p8s_logzio_name", "namespace", "pod", "container"}, |
| 42 | + "kube_pod_container_info": {"p8s_logzio_name", "namespace", "pod"}, |
| 43 | + "container_network_transmit_bytes_total": {"p8s_logzio_name", "namespace", "pod"}, |
| 44 | + "container_network_receive_bytes_total": {"p8s_logzio_name", "namespace", "pod"}, |
| 45 | + "kube_pod_created": {"p8s_logzio_name", "namespace", "pod"}, |
| 46 | + "kube_pod_owner": {"p8s_logzio_name", "namespace", "pod", "owner_kind", "owner_name"}, |
| 47 | + "kube_pod_container_status_restarts_total": {"p8s_logzio_name", "namespace", "pod"}, |
| 48 | + "kube_pod_status_reason": {"p8s_logzio_name", "namespace", "pod", "reason"}, |
| 49 | + "kube_pod_container_status_waiting_reason": {"p8s_logzio_name", "namespace", "pod", "reason"}, |
| 50 | + "node_cpu_seconds_total": {"p8s_logzio_name", "instance", "kubernetes_node"}, |
| 51 | + "kube_node_status_allocatable": {"p8s_logzio_name", "node", "resource"}, |
| 52 | + "node_memory_MemAvailable_bytes": {"p8s_logzio_name", "instance", "kubernetes_node"}, |
| 53 | + "node_memory_MemTotal_bytes": {"p8s_logzio_name", "instance", "kubernetes_node"}, |
| 54 | + "kube_node_role": {"p8s_logzio_name", "status", "role", "node"}, |
| 55 | + "kube_node_status_condition": {"p8s_logzio_name", "status", "role", "node"}, |
| 56 | + "kube_node_created": {"p8s_logzio_name", "node"}, |
| 57 | + "node_filesystem_avail_bytes": {"p8s_logzio_name", "instance", "kubernetes_node"}, |
| 58 | + "node_filesystem_size_bytes": {"p8s_logzio_name", "instance", "kubernetes_node"}, |
| 59 | + "kube_replicaset_owner": {"p8s_logzio_name", "namespace", "owner_kind", "owner_name", "replicaset"}, |
| 60 | + "kube_deployment_created": {"p8s_logzio_name", "namespace", "deployment"}, |
| 61 | + "kube_deployment_status_condition": {"p8s_logzio_name", "namespace", "deployment", "status"}, |
| 62 | + "calls_total": {"k8s_node_name", "k8s_namespace_name", "k8s_pod_name", "span_kind", "operation"}, |
| 63 | + "latency_sum": {"k8s_node_name", "k8s_namespace_name", "k8s_pod_name", "span_kind", "operation"}, |
| 64 | + "latency_count": {"k8s_node_name", "k8s_namespace_name", "k8s_pod_name", "span_kind", "operation"}, |
| 65 | + "latency_bucket": {"k8s_node_name", "k8s_namespace_name", "k8s_pod_name", "span_kind", "operation"}, |
| 66 | + } |
| 67 | + |
| 68 | + if metricResponse.Status != "success" { |
| 69 | + t.Errorf("No metrics found") |
| 70 | + } |
| 71 | + // Verify required metrics |
| 72 | + missingMetrics := verifyMetrics(metricResponse, requiredMetrics) |
| 73 | + if len(missingMetrics) > 0 { |
| 74 | + var sb strings.Builder |
| 75 | + for _, metric := range missingMetrics { |
| 76 | + sb.WriteString(metric + "\n") |
| 77 | + } |
| 78 | + t.Errorf("Missing metrics or labels:\n%s", sb.String()) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// fetchMetrics fetches the metrics from the logz.io API |
| 83 | +func fetchMetrics(metricsApiKey string) (*MetricResponse, error) { |
| 84 | + url := fmt.Sprintf("%s/metrics/prometheus/api/v1/query?query={env_id='multi-env-test'}", BaseLogzioApiUrl) |
| 85 | + client := &http.Client{} |
| 86 | + req, err := http.NewRequest("GET", url, nil) |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + req.Header.Set("Accept", "application/json") |
| 91 | + req.Header.Set("X-API-TOKEN", metricsApiKey) |
| 92 | + |
| 93 | + resp, err := client.Do(req) |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + defer resp.Body.Close() |
| 98 | + |
| 99 | + if resp.StatusCode != http.StatusOK { |
| 100 | + return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) |
| 101 | + } |
| 102 | + |
| 103 | + body, err := io.ReadAll(resp.Body) |
| 104 | + if err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + |
| 108 | + var metricResponse MetricResponse |
| 109 | + err = json.Unmarshal(body, &metricResponse) |
| 110 | + if err != nil { |
| 111 | + return nil, err |
| 112 | + } |
| 113 | + |
| 114 | + return &metricResponse, nil |
| 115 | +} |
| 116 | + |
| 117 | +// verifyMetrics checks if the required metrics and their labels are present in the response |
| 118 | +func verifyMetrics(metricResponse *MetricResponse, requiredMetrics map[string][]string) []string { |
| 119 | + missingMetrics := []string{} |
| 120 | + |
| 121 | + for metricName, requiredLabels := range requiredMetrics { |
| 122 | + found := false |
| 123 | + for _, result := range metricResponse.Data.Result { |
| 124 | + if result.Metric["__name__"] == metricName { |
| 125 | + found = true |
| 126 | + for _, label := range requiredLabels { |
| 127 | + if _, exists := result.Metric[label]; !exists { |
| 128 | + missingMetrics = append(missingMetrics, fmt.Sprintf("%s (missing label: %s)", metricName, label)) |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + if !found { |
| 134 | + missingMetrics = append(missingMetrics, metricName+" (not found)") |
| 135 | + } |
| 136 | + } |
| 137 | + return deduplicate(missingMetrics) |
| 138 | +} |
| 139 | + |
| 140 | +// deduplicate removes duplicate strings from the input array. |
| 141 | +func deduplicate(data []string) []string { |
| 142 | + uniqueMap := make(map[string]bool) |
| 143 | + var uniqueList []string |
| 144 | + |
| 145 | + for _, item := range data { |
| 146 | + trimmedItem := strings.TrimSpace(item) |
| 147 | + if _, exists := uniqueMap[trimmedItem]; !exists { |
| 148 | + uniqueMap[trimmedItem] = true |
| 149 | + uniqueList = append(uniqueList, trimmedItem) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return uniqueList |
| 154 | +} |
0 commit comments