Skip to content

Commit

Permalink
small refactors following review
Browse files Browse the repository at this point in the history
  • Loading branch information
wildum committed Oct 25, 2023
1 parent 29b8c8b commit b8d34cc
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 53 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,4 @@ jobs:
- name: Set OTEL Exporter Endpoint
run: echo "OTEL_EXPORTER_ENDPOINT=http://172.17.0.1:8080" >> $GITHUB_ENV
- name: Run tests
run: |
cd integration-tests
go run .
run: make integration-tests
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
##
## Targets for running tests:
##
## test Run tests
## lint Lint code
## test Run tests
## lint Lint code
## integration-tests Run integration tests
##
## Targets for building binaries:
##
Expand Down Expand Up @@ -179,6 +180,10 @@ test-packages:
docker pull $(BUILD_IMAGE)
go test -tags=packaging ./packaging

.PHONY: integration-tests
integration-test:
cd integration-tests && $(GO_ENV) go run .

#
# Targets for building binaries
#
Expand Down
10 changes: 7 additions & 3 deletions integration-tests/common/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package common
import "encoding/json"

type LogResponse struct {
Status string `json:"status"`
Data []LogData `json:"data"`
Status string `json:"status"`
Data struct {
ResultType string `json:"resultType"`
Result []LogData `json:"result"`
} `json:"data"`
}

type LogData struct {
Filename string `json:"filename"`
Stream map[string]string `json:"stream"`
Values [][2]string `json:"values"`
}

func (m *LogResponse) Unmarshal(data []byte) error {
Expand Down
29 changes: 25 additions & 4 deletions integration-tests/common/metric.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package common

import "encoding/json"
import (
"encoding/json"
"fmt"
)

type MetricResponse struct {
Status string `json:"status"`
Expand All @@ -14,12 +17,30 @@ type MetricData struct {

type MetricResult struct {
Metric Metric `json:"metric"`
Value Value `json:"value"`
}

type Value struct {
Timestamp int64
Value string
}

func (v *Value) UnmarshalJSON(b []byte) error {
var arr []interface{}
if err := json.Unmarshal(b, &arr); err != nil {
return err
}
if len(arr) != 2 {
return fmt.Errorf("expected 2 values, got %d", len(arr))
}
v.Timestamp, _ = arr[0].(int64)
v.Value, _ = arr[1].(string)
return nil
}

type Metric struct {
Job string `json:"job"`
TestLabel string `json:"test"`
Name string `json:"__name__"`
TestName string `json:"test_name"`
Name string `json:"__name__"`
}

func (m *MetricResponse) Unmarshal(data []byte) error {
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/configs/otel-gen-client/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ COPY ./integration-tests/configs/otel-gen-client/ ./
RUN CGO_ENABLED=0 go build -o main main.go
FROM alpine:3.18
COPY --from=build /app/main /app/main
CMD ["/app/main"]
CMD ["/app/main"]
14 changes: 9 additions & 5 deletions integration-tests/configs/otel-gen-client/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// This file was copied with minor modifications from https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/b0be5c98325ec71f35b82e278a3fc3e6f3fe4954/examples/demo/client/main.go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

Expand All @@ -7,7 +9,6 @@ package main

import (
"context"
"fmt"
"log"
"math/rand"
"net/http"
Expand All @@ -30,6 +31,11 @@ import (
"google.golang.org/grpc"
)

const (
otelExporterOtlpEndpoint = "OTEL_EXPORTER_OTLP_ENDPOINT"
demoServerEndpoint = "DEMO_SERVER_ENDPOINT"
)

// Initializes an OTLP exporter, and configures the corresponding trace and
// metric providers.
func initProvider() func() {
Expand All @@ -47,7 +53,7 @@ func initProvider() func() {
)
handleErr(err, "failed to create resource")

otelAgentAddr, ok := os.LookupEnv("OTEL_EXPORTER_OTLP_ENDPOINT")
otelAgentAddr, ok := os.LookupEnv(otelExporterOtlpEndpoint)
if !ok {
otelAgentAddr = "0.0.0.0:4317"
}
Expand Down Expand Up @@ -164,19 +170,17 @@ func main() {
randLineLength := rng.Int63n(999)
lineCounts.Add(ctx, 1, metric.WithAttributes(commonLabels...))
lineLengths.Record(ctx, randLineLength, metric.WithAttributes(commonLabels...))
fmt.Printf("#%d: LineLength: %dBy\n", i, randLineLength)
}

requestLatency.Record(ctx, latencyMs, metric.WithAttributes(commonLabels...))
requestCount.Add(ctx, 1, metric.WithAttributes(commonLabels...))

fmt.Printf("Latency: %.3fms\n", latencyMs)
time.Sleep(time.Duration(1) * time.Second)
}
}

func makeRequest(ctx context.Context) {
demoServerAddr, ok := os.LookupEnv("DEMO_SERVER_ENDPOINT")
demoServerAddr, ok := os.LookupEnv(demoServerEndpoint)
if !ok {
demoServerAddr = "http://0.0.0.0:7080/hello"
}
Expand Down
24 changes: 5 additions & 19 deletions integration-tests/configs/otel-gen-server/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// This file was copied with minor modifications from https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/b0be5c98325ec71f35b82e278a3fc3e6f3fe4954/examples/demo/server/main.go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

Expand All @@ -8,7 +10,6 @@ package main
import (
"context"
"log"
"math/rand"
"net/http"
"os"
"time"
Expand All @@ -30,7 +31,7 @@ import (
"google.golang.org/grpc"
)

var rng = rand.New(rand.NewSource(time.Now().UnixNano()))
const otelExporterOtlpEndpoint = "OTEL_EXPORTER_OTLP_ENDPOINT"

// Initializes an OTLP exporter, and configures the corresponding trace and
// metric providers.
Expand All @@ -49,7 +50,7 @@ func initProvider() func() {
)
handleErr(err, "failed to create resource")

otelAgentAddr, ok := os.LookupEnv("OTEL_EXPORTER_OTLP_ENDPOINT")
otelAgentAddr, ok := os.LookupEnv(otelExporterOtlpEndpoint)
if !ok {
otelAgentAddr = "0.0.0.0:4317"
}
Expand Down Expand Up @@ -122,22 +123,7 @@ func main() {

// create a handler wrapped in OpenTelemetry instrumentation
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// random sleep to simulate latency
var sleep int64

switch modulus := time.Now().Unix() % 5; modulus {
case 0:
sleep = rng.Int63n(2000)
case 1:
sleep = rng.Int63n(15)
case 2:
sleep = rng.Int63n(917)
case 3:
sleep = rng.Int63n(87)
case 4:
sleep = rng.Int63n(1173)
}
time.Sleep(time.Duration(sleep) * time.Millisecond)
time.Sleep(time.Duration(100) * time.Millisecond)
ctx := req.Context()
requestCount.Add(ctx, 1, metric.WithAttributes(commonLabels...))
span := trace.SpanFromContext(ctx)
Expand Down
7 changes: 3 additions & 4 deletions integration-tests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func main() {
}

func runIntegrationTests(cmd *cobra.Command, args []string) {
defer reportResults()
defer cleanUpEnvironment()

if !skipBuild {
buildAgent()
}
Expand All @@ -49,8 +52,4 @@ func runIntegrationTests(cmd *cobra.Command, args []string) {
logChan = make(chan TestLog, len(testDirs))
runAllTests()
}

cleanUpEnvironment()

reportResults()
}
4 changes: 2 additions & 2 deletions integration-tests/tests/otlp-metrics/config.river
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ otelcol.receiver.otlp "otlp_metrics" {

otelcol.processor.attributes "otlp_metrics" {
action {
key = "test"
key = "test_name"
value = "otlp_metrics"
action = "insert"
}
Expand All @@ -28,4 +28,4 @@ otelcol.exporter.otlphttp "otlp_metrics" {
insecure_skip_verify = true
}
}
}
}
5 changes: 3 additions & 2 deletions integration-tests/tests/otlp-metrics/otlp_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
)

const query = "http://localhost:9009/prometheus/api/v1/query?query=span_metrics_duration_bucket{test='otlp_metrics'}"
const query = "http://localhost:9009/prometheus/api/v1/query?query=span_metrics_duration_bucket{test_name='otlp_metrics'}"

func TestOtlpMetrics(t *testing.T) {
var metricResponse common.MetricResponse
Expand All @@ -16,7 +16,8 @@ func TestOtlpMetrics(t *testing.T) {
assert.NoError(c, err)
if assert.NotEmpty(c, metricResponse.Data.Result) {
assert.Equal(c, metricResponse.Data.Result[0].Metric.Name, "span_metrics_duration_bucket")
assert.Equal(c, metricResponse.Data.Result[0].Metric.TestLabel, "otlp_metrics")
assert.Equal(c, metricResponse.Data.Result[0].Metric.TestName, "otlp_metrics")
assert.NotEmpty(c, metricResponse.Data.Result[0].Value.Value)
}
}, common.DefaultTimeout, common.DefaultRetryInterval, "Data did not satisfy the conditions within the time limit")
}
5 changes: 4 additions & 1 deletion integration-tests/tests/read-log-file/config.river
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ loki.write "test" {
endpoint {
url = "http://localhost:3100/loki/api/v1/push"
}
}
external_labels = {
test_name = "read_log_file",
}
}
12 changes: 9 additions & 3 deletions integration-tests/tests/read-log-file/read_log_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ import (
"github.com/stretchr/testify/assert"
)

const query = "http://localhost:3100/loki/api/v1/series"
const query = "http://localhost:3100/loki/api/v1/query?query={test_name=%22read_log_file%22}"

func TestReadLogFile(t *testing.T) {
var logResponse common.LogResponse
assert.EventuallyWithT(t, func(c *assert.CollectT) {
err := common.FetchDataFromURL(query, &logResponse)
assert.NoError(c, err)
assert.NotEmpty(c, logResponse.Data)
assert.Equal(c, logResponse.Data[0].Filename, "logs.txt")
if assert.NotEmpty(c, logResponse.Data.Result) {
assert.Equal(c, logResponse.Data.Result[0].Stream["filename"], "logs.txt")
logs := make([]string, len(logResponse.Data.Result[0].Values))
for i, valuePair := range logResponse.Data.Result[0].Values {
logs[i] = valuePair[1]
}
assert.Contains(c, logs, "[2023-10-02 14:25:43] INFO: Starting the web application...")
}
}, common.DefaultTimeout, common.DefaultRetryInterval, "Data did not satisfy the conditions within the time limit")
}
5 changes: 4 additions & 1 deletion integration-tests/tests/scrape-prom-metrics/config.river
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ prometheus.remote_write "scrape_prom_metrics" {
max_samples_per_send = 100
}
}
}
external_labels = {
test_name = "scrape_prom_metrics",
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
)

const query = "http://localhost:9009/prometheus/api/v1/query?query=avalanche_metric_mmmmm_0_0{job='prometheus.scrape.scrape_prom_metrics'}"
const query = "http://localhost:9009/prometheus/api/v1/query?query=avalanche_metric_mmmmm_0_0{test_name='scrape_prom_metrics'}"

func TestScrapePromMetrics(t *testing.T) {
var metricResponse common.MetricResponse
Expand All @@ -16,7 +16,8 @@ func TestScrapePromMetrics(t *testing.T) {
assert.NoError(c, err)
if assert.NotEmpty(c, metricResponse.Data.Result) {
assert.Equal(c, metricResponse.Data.Result[0].Metric.Name, "avalanche_metric_mmmmm_0_0")
assert.Equal(c, metricResponse.Data.Result[0].Metric.Job, "prometheus.scrape.scrape_prom_metrics")
assert.Equal(c, metricResponse.Data.Result[0].Metric.TestName, "scrape_prom_metrics")
assert.NotEmpty(c, metricResponse.Data.Result[0].Value.Value)
}
}, common.DefaultTimeout, common.DefaultRetryInterval, "Data did not satisfy the conditions within the time limit")
}
2 changes: 1 addition & 1 deletion integration-tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func runAllTests() {

func cleanUpEnvironment() {
fmt.Println("Cleaning up Docker environment...")
err := exec.Command(dockerComposeCmd, "down").Run()
err := exec.Command(dockerComposeCmd, "down", "--volumes", "--rmi", "all").Run()
if err != nil {
panic(err)
}
Expand Down

0 comments on commit b8d34cc

Please sign in to comment.