From 687d4358123d8202d57f07e3f2dab7e5fc167133 Mon Sep 17 00:00:00 2001 From: martinyonatann Date: Mon, 20 Jan 2025 18:26:29 +0700 Subject: [PATCH] echo metrics test --- test/echo/v4.0.0/go.mod | 2 +- test/echo/v4.0.0/test_echo_metrics.go | 64 +++++++++++++++++++++++++++ test/echo_tests.go | 7 +++ 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 test/echo/v4.0.0/test_echo_metrics.go diff --git a/test/echo/v4.0.0/go.mod b/test/echo/v4.0.0/go.mod index d79d8475..c6079f76 100644 --- a/test/echo/v4.0.0/go.mod +++ b/test/echo/v4.0.0/go.mod @@ -8,6 +8,7 @@ require ( github.com/alibaba/opentelemetry-go-auto-instrumentation/test/verifier v0.0.0-00010101000000-000000000000 github.com/labstack/echo/v4 v4.12.0 go.opentelemetry.io/otel/sdk v1.31.0 + go.opentelemetry.io/otel/sdk/metric v1.30.0 ) require ( @@ -23,7 +24,6 @@ require ( github.com/valyala/fasttemplate v1.2.2 // indirect go.opentelemetry.io/otel v1.31.0 // indirect go.opentelemetry.io/otel/metric v1.31.0 // indirect - go.opentelemetry.io/otel/sdk/metric v1.30.0 // indirect go.opentelemetry.io/otel/trace v1.31.0 // indirect golang.org/x/crypto v0.26.0 // indirect golang.org/x/net v0.28.0 // indirect diff --git a/test/echo/v4.0.0/test_echo_metrics.go b/test/echo/v4.0.0/test_echo_metrics.go new file mode 100644 index 00000000..4f5ed2ad --- /dev/null +++ b/test/echo/v4.0.0/test_echo_metrics.go @@ -0,0 +1,64 @@ +// Copyright (c) 2024 Alibaba Group Holding Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "net/http" + "strconv" + "time" + + "github.com/alibaba/opentelemetry-go-auto-instrumentation/test/verifier" + echo "github.com/labstack/echo/v4" + "github.com/labstack/echo/v4/middleware" + "go.opentelemetry.io/otel/sdk/metric/metricdata" +) + +func setup() { + engine := echo.New() + engine.Use(middleware.Logger()) + engine.GET("/test", func(c echo.Context) error { + return c.JSON(http.StatusOK, map[string]interface{}{ + "code": 1, + "msg": c.Path(), + }) + }) + + // Start server + engine.Logger.Fatal(engine.Start(":8080")) +} + +func main() { + go setup() + time.Sleep(5 * time.Second) + client := &http.Client{} + resp, err := client.Get("http://127.0.0.1:8080/test") + defer resp.Body.Close() + if err != nil { + panic(err) + } + time.Sleep(3 * time.Second) + verifier.WaitAndAssertMetrics(map[string]func(metricdata.ResourceMetrics){ + "http.server.request.duration": func(mrs metricdata.ResourceMetrics) { + if len(mrs.ScopeMetrics) <= 0 { + panic("No http.server.request.duration metrics received!") + } + point := mrs.ScopeMetrics[0].Metrics[0].Data.(metricdata.Histogram[float64]) + if point.DataPoints[0].Count <= 0 { + panic("http.server.request.duration metrics count is not positive, actually " + strconv.Itoa(int(point.DataPoints[0].Count))) + } + verifier.VerifyHttpServerMetricsAttributes(point.DataPoints[0].Attributes.ToSlice(), "GET", "/test", "", "http", "1.1", "http", 200) + }, + }) +} diff --git a/test/echo_tests.go b/test/echo_tests.go index 6e87bc90..84d2ce82 100644 --- a/test/echo_tests.go +++ b/test/echo_tests.go @@ -22,6 +22,7 @@ const echo_module_name = "echo" func init() { TestCases = append(TestCases, NewGeneralTestCase("echo-basic-test", echo_module_name, "v4.0.0", "", "1.18", "", TestBasicEcho), + NewGeneralTestCase("echo-metrics-test", echo_module_name, "v4.0.0", "", "1.18", "", TestMetricsEcho), NewGeneralTestCase("echo-middleware-test", echo_module_name, "v4.0.0", "", "1.18", "", TestEchoMiddleware), NewGeneralTestCase("echo-pattern-test", echo_module_name, "v4.0.0", "", "1.18", "", TestEchoPattern), NewMuzzleTestCase("echo-muzzle-test", echo_dependency_name, echo_module_name, "v4.0.0", "v4.9.1", "1.18", "", []string{"go", "build", "test_echo_basic.go"}), @@ -47,3 +48,9 @@ func TestEchoMiddleware(t *testing.T, env ...string) { RunGoBuild(t, "go", "build", "test_echo_middleware.go") RunApp(t, "test_echo_middleware", env...) } + +func TestMetricsEcho(t *testing.T, env ...string) { + UseApp("echo/v4.0.0") + RunGoBuild(t, "go", "build", "test_echo_metrics.go") + RunApp(t, "test_echo_metrics", env...) +}