From a115efc3b4be24cb3018a476f2b53086b85f5487 Mon Sep 17 00:00:00 2001 From: 8naama Date: Tue, 10 Dec 2024 10:36:09 +0200 Subject: [PATCH] fix test --- charts/logzio-apm-collector/values.yaml | 2 +- tests/apm_metrics_e2e_test.go | 123 +----------------------- 2 files changed, 3 insertions(+), 122 deletions(-) diff --git a/charts/logzio-apm-collector/values.yaml b/charts/logzio-apm-collector/values.yaml index d407c763..7ad351c8 100644 --- a/charts/logzio-apm-collector/values.yaml +++ b/charts/logzio-apm-collector/values.yaml @@ -202,7 +202,7 @@ spmForwarderConfig: pipelines: traces/spm: receivers: [jaeger, zipkin, otlp] - processors: [resourcedetection/all, attributes/env_id, k8sattributes, resource/k8s, batch] + processors: [resourcedetection/all, attributes/env_id, k8sattributes, batch] exporters: [otlp] # SPM Collector configuration diff --git a/tests/apm_metrics_e2e_test.go b/tests/apm_metrics_e2e_test.go index 8d330759..63cf0399 100644 --- a/tests/apm_metrics_e2e_test.go +++ b/tests/apm_metrics_e2e_test.go @@ -1,30 +1,12 @@ package tests import ( - "encoding/json" "fmt" - "go.uber.org/zap" - "io" - "net/http" - "net/url" "os" - "strings" "testing" ) -// MetricResponse represents the structure of the API response -type MetricResponse struct { - Status string `json:"status"` - Data struct { - ResultType string `json:"resultType"` - Result []struct { - Metric map[string]string `json:"metric"` - Value []interface{} `json:"value"` - } `json:"result"` - } `json:"data"` -} - -func TestSpmMetrics(t *testing.T) { +func TestSpmMetricsApm(t *testing.T) { requiredMetrics := map[string][]string{ "calls_total": {"k8s_node_name", "k8s_namespace_name", "k8s_pod_name", "span_kind", "operation"}, "latency_sum": {"k8s_node_name", "k8s_namespace_name", "k8s_pod_name", "span_kind", "operation"}, @@ -36,7 +18,7 @@ func TestSpmMetrics(t *testing.T) { testMetrics(t, requiredMetrics, query) } -func TestServiceGraphMetrics(t *testing.T) { +func TestServiceGraphMetricsApm(t *testing.T) { requiredMetrics := map[string][]string{ "traces_service_graph_request_total": {"client", "server"}, "traces_service_graph_request_failed_total": {"client", "server"}, @@ -51,104 +33,3 @@ func TestServiceGraphMetrics(t *testing.T) { query := fmt.Sprintf(`{client_env_id='%s'}`, envId) testMetrics(t, requiredMetrics, query) } - -func testMetrics(t *testing.T, requiredMetrics map[string][]string, query string) { - metricsApiKey := os.Getenv("LOGZIO_METRICS_API_KEY") - if metricsApiKey == "" { - t.Fatalf("LOGZIO_METRICS_API_KEY environment variable not set") - } - - metricResponse, err := fetchMetrics(metricsApiKey, query) - if err != nil { - t.Fatalf("Failed to fetch metrics: %v", err) - } - - if metricResponse.Status != "success" { - t.Errorf("No metrics found") - } - logger.Info("Found metrics", zap.Int("metrics_count", len(metricResponse.Data.Result))) - // Verify required metrics - missingMetrics := verifyMetrics(metricResponse, requiredMetrics) - if len(missingMetrics) > 0 { - var sb strings.Builder - for _, metric := range missingMetrics { - sb.WriteString(metric + "\n") - } - t.Errorf("Missing metrics or labels:\n%s", sb.String()) - } -} - -// fetchMetrics fetches the metrics from the logz.io API -func fetchMetrics(metricsApiKey string, query string) (*MetricResponse, error) { - url := fmt.Sprintf("%s/metrics/prometheus/api/v1/query?query=%s", BaseLogzioApiUrl, query) - client := &http.Client{} - logger.Info("sending api request", zap.String("url", url)) - req, err := http.NewRequest("GET", url, nil) - if err != nil { - return nil, err - } - req.Header.Set("Accept", "application/json") - req.Header.Set("X-API-TOKEN", metricsApiKey) - - resp, err := client.Do(req) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) - } - - body, err := io.ReadAll(resp.Body) - if err != nil { - return nil, err - } - - var metricResponse MetricResponse - err = json.Unmarshal(body, &metricResponse) - if err != nil { - return nil, err - } - - return &metricResponse, nil -} - -// verifyMetrics checks if the required metrics and their labels are present in the response -func verifyMetrics(metricResponse *MetricResponse, requiredMetrics map[string][]string) []string { - missingMetrics := []string{} - - for metricName, requiredLabels := range requiredMetrics { - found := false - for _, result := range metricResponse.Data.Result { - if result.Metric["__name__"] == metricName { - found = true - for _, label := range requiredLabels { - if _, exists := result.Metric[label]; !exists { - missingMetrics = append(missingMetrics, fmt.Sprintf("%s (missing label: %s)", metricName, label)) - } - } - } - } - if !found { - missingMetrics = append(missingMetrics, metricName+" (not found)") - } - } - return deduplicate(missingMetrics) -} - -// deduplicate removes duplicate strings from the input array. -func deduplicate(data []string) []string { - uniqueMap := make(map[string]bool) - var uniqueList []string - - for _, item := range data { - trimmedItem := strings.TrimSpace(item) - if _, exists := uniqueMap[trimmedItem]; !exists { - uniqueMap[trimmedItem] = true - uniqueList = append(uniqueList, trimmedItem) - } - } - - return uniqueList -}