Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
8naama committed Dec 10, 2024
1 parent d2ed9da commit a115efc
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 122 deletions.
2 changes: 1 addition & 1 deletion charts/logzio-apm-collector/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
123 changes: 2 additions & 121 deletions tests/apm_metrics_e2e_test.go
Original file line number Diff line number Diff line change
@@ -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"},
Expand All @@ -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"},
Expand All @@ -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
}

0 comments on commit a115efc

Please sign in to comment.