|
| 1 | +// Copyright 2024 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package quickstarttest |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "net/http" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "strings" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | + |
| 27 | + dto "github.com/prometheus/client_model/go" |
| 28 | + "github.com/prometheus/common/expfmt" |
| 29 | + "github.com/stretchr/testify/assert" |
| 30 | + "github.com/stretchr/testify/require" |
| 31 | + "github.com/testcontainers/testcontainers-go/modules/compose" |
| 32 | + "github.com/testcontainers/testcontainers-go/wait" |
| 33 | +) |
| 34 | + |
| 35 | +const ( |
| 36 | + sentItemsThreshold = 50.0 |
| 37 | +) |
| 38 | + |
| 39 | +type testCase struct { |
| 40 | + metricName string |
| 41 | + exporter string |
| 42 | + threshold float64 |
| 43 | +} |
| 44 | + |
| 45 | +var testCases = []testCase{ |
| 46 | + {metricName: "otelcol_exporter_sent_spans", exporter: "googlecloud", threshold: sentItemsThreshold}, |
| 47 | + {metricName: "otelcol_exporter_sent_log_records", exporter: "googlecloud", threshold: sentItemsThreshold}, |
| 48 | + {metricName: "otelcol_exporter_sent_metric_points", exporter: "googlemanagedprometheus", threshold: sentItemsThreshold}, |
| 49 | +} |
| 50 | + |
| 51 | +// InstrumentationQuickstartTest runs the instrumentation quickstart docker compose setup in |
| 52 | +// the quickstartRoot directory and verifies that metrics, logs, and traces are successfully |
| 53 | +// sent from the collector to GCP. |
| 54 | +// |
| 55 | +// Respects a COMPOSE_OVERRIDE_FILE environment variable set to a comma-separated list of paths |
| 56 | +// to additional compose files to include. |
| 57 | +func InstrumentationQuickstartTest(t *testing.T, quickstartRoot string) { |
| 58 | + ctx := context.Background() |
| 59 | + composeStack := composeUp(ctx, t, quickstartRoot) |
| 60 | + |
| 61 | + // Let the docker compose app run until some spans/logs/metrics are sent to GCP |
| 62 | + t.Logf("Compose stack is up, waiting for prometheus metrics indicating successful export") |
| 63 | + |
| 64 | + // Check the collector's self-observability prometheus metrics to see that exports to GCP were successful. |
| 65 | + for _, tc := range testCases { |
| 66 | + t.Run(tc.metricName, func(t *testing.T) { |
| 67 | + require.EventuallyWithT( |
| 68 | + t, |
| 69 | + func(collect *assert.CollectT) { |
| 70 | + promMetrics, err := getPromMetrics(ctx, composeStack) |
| 71 | + if !assert.NoError(collect, err) { |
| 72 | + return |
| 73 | + } |
| 74 | + verifyPromMetric(collect, promMetrics, tc) |
| 75 | + }, |
| 76 | + time.Minute*2, // wait for up to |
| 77 | + time.Second, // check at interval |
| 78 | + ) |
| 79 | + }) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func composeUp(ctx context.Context, t *testing.T, quickstartRoot string) compose.ComposeStack { |
| 84 | + composeFiles := []string{filepath.Join(quickstartRoot, "docker-compose.yaml")} |
| 85 | + if composeOverrideFile := os.Getenv("COMPOSE_OVERRIDE_FILE"); composeOverrideFile != "" { |
| 86 | + composeFiles = append(composeFiles, strings.Split(composeOverrideFile, ",")...) |
| 87 | + } |
| 88 | + |
| 89 | + var ( |
| 90 | + composeStack compose.ComposeStack |
| 91 | + err error |
| 92 | + ) |
| 93 | + composeStack, err = compose.NewDockerCompose(composeFiles...) |
| 94 | + require.NoError(t, err) |
| 95 | + |
| 96 | + require.NoError(t, err) |
| 97 | + composeStack = composeStack.WithOsEnv(). |
| 98 | + WaitForService("app", wait.ForHTTP("/single").WithPort("8080")). |
| 99 | + WaitForService("otelcol", wait.ForHTTP("/metrics").WithPort("8888")) |
| 100 | + |
| 101 | + t.Cleanup(func() { |
| 102 | + require.NoError(t, composeStack.Down(ctx, compose.RemoveOrphans(true))) |
| 103 | + }) |
| 104 | + require.NoError(t, composeStack.Up(ctx)) |
| 105 | + return composeStack |
| 106 | +} |
| 107 | + |
| 108 | +func getPromMetrics(ctx context.Context, composeStack compose.ComposeStack) (map[string]*dto.MetricFamily, error) { |
| 109 | + promUri, err := getPromEndpoint(ctx, composeStack) |
| 110 | + if err != nil { |
| 111 | + return nil, err |
| 112 | + } |
| 113 | + |
| 114 | + resp, err := http.Get(promUri) |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + defer resp.Body.Close() |
| 119 | + |
| 120 | + var parser expfmt.TextParser |
| 121 | + parsed, err := parser.TextToMetricFamilies(resp.Body) |
| 122 | + if err != nil { |
| 123 | + return nil, err |
| 124 | + } |
| 125 | + return parsed, nil |
| 126 | +} |
| 127 | + |
| 128 | +func getPromEndpoint(ctx context.Context, composeStack compose.ComposeStack) (string, error) { |
| 129 | + collectorContainer, err := composeStack.ServiceContainer(ctx, "otelcol") |
| 130 | + if err != nil { |
| 131 | + return "", err |
| 132 | + } |
| 133 | + collectorHost, err := collectorContainer.Host(ctx) |
| 134 | + if err != nil { |
| 135 | + return "", err |
| 136 | + } |
| 137 | + collectorPort, err := collectorContainer.MappedPort(ctx, "8888") |
| 138 | + if err != nil { |
| 139 | + return "", err |
| 140 | + } |
| 141 | + return fmt.Sprintf("http://%s:%s/metrics", collectorHost, collectorPort.Port()), nil |
| 142 | +} |
| 143 | + |
| 144 | +func verifyPromMetric(t assert.TestingT, promMetrics map[string]*dto.MetricFamily, tc testCase) { |
| 145 | + if !assert.Contains(t, promMetrics, tc.metricName, "prometheus metrics do not contain %v:\n%v", tc.metricName, promMetrics) { |
| 146 | + return |
| 147 | + } |
| 148 | + mf := promMetrics[tc.metricName] |
| 149 | + |
| 150 | + for _, metric := range mf.Metric { |
| 151 | + for _, labelPair := range metric.GetLabel() { |
| 152 | + if labelPair.GetName() == "exporter" && labelPair.GetValue() == tc.exporter { |
| 153 | + value := metric.GetCounter().GetValue() |
| 154 | + assert.Greater(t, value, tc.threshold, "Metric %v was expected to have value > %v, got %v", metric, sentItemsThreshold, value) |
| 155 | + return |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + assert.Fail(t, "Could not find a metric sample for exporter=%v, got metrics %v", tc.exporter, mf) |
| 160 | +} |
0 commit comments