From dabb95542dd95300d06931332e2a7b6ce2ec1a78 Mon Sep 17 00:00:00 2001 From: Rick <1450685+LinuxSuRen@users.noreply.github.com> Date: Wed, 19 Jun 2024 14:43:33 +0800 Subject: [PATCH] feat: add http requests metrics (#495) * feat: add http requests metrics * add more error log * move the api-testing-scema.json * copy api-testing-schema.json during the docs build process * do not ignore helm directory --------- Co-authored-by: rick --- .dockerignore | 8 ++++++++ .github/workflows/docs.yaml | 4 +++- cmd/server.go | 1 + e2e/test-suite-common.yaml | 1 + pkg/runner/http_reverse.go | 2 +- pkg/server/http.go | 13 +++++++++++++ 6 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..683a6703c --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +bin/ +extensions/ +dist/ +console/atest-desktop/ +console/atest-ui/node_modules/ +docs/site/node_modules/ +docs/site/public/ +docs/site/resources/ diff --git a/.github/workflows/docs.yaml b/.github/workflows/docs.yaml index 73bae457a..a9ca1a3d6 100644 --- a/.github/workflows/docs.yaml +++ b/.github/workflows/docs.yaml @@ -62,7 +62,9 @@ jobs: node-version: '18' - name: Install Site Dependencies and Build Site - run: make docs # docs-check-links + run: | + cp docs/api-testing-schema.json docs/site/static/api-testing-schema.json + make docs # docs-check-links # Upload docs for GitHub Pages - name: Upload GitHub Pages artifact diff --git a/cmd/server.go b/cmd/server.go index 500a079d7..7ac9f05d1 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -336,6 +336,7 @@ func (o *serverOption) runE(cmd *cobra.Command, args []string) (err error) { collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}), collectors.NewBuildInfoCollector(), server.ExecutionCountNum, server.ExecutionSuccessNum, server.ExecutionFailNum, + server.RequestCounter, runner.RunnersNum, ) mux.HandlePath(http.MethodGet, "/metrics", func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) { diff --git a/e2e/test-suite-common.yaml b/e2e/test-suite-common.yaml index f62fb8985..dac2afb27 100644 --- a/e2e/test-suite-common.yaml +++ b/e2e/test-suite-common.yaml @@ -376,3 +376,4 @@ items: - indexOf(data, "atest_execution_fail") != -1 - indexOf(data, "atest_execution_success") != -1 - indexOf(data, "atest_runners_count") != -1 + - indexOf(data, "http_requests_total") != -1 diff --git a/pkg/runner/http_reverse.go b/pkg/runner/http_reverse.go index 5d606caea..e877efd25 100644 --- a/pkg/runner/http_reverse.go +++ b/pkg/runner/http_reverse.go @@ -141,7 +141,7 @@ func (r *reverseHTTPRunner) RunTestCase(testcase *testing.TestCase, dataContext mutationCase := mutator.Render(testcase) _, reverseErr := r.TestCaseRunner.RunTestCase(mutationCase, dataContext, ctx) if reverseErr == nil { - err = fmt.Errorf("failed when: %q", mutator.Message()) + err = fmt.Errorf("testcase %q failed when: %q", testcase.Name, mutator.Message()) return } } diff --git a/pkg/server/http.go b/pkg/server/http.go index 52fa79191..bb12c5d47 100644 --- a/pkg/server/http.go +++ b/pkg/server/http.go @@ -17,6 +17,8 @@ package server import ( context "context" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" "net" "net/http" "strings" @@ -93,7 +95,18 @@ func (s *defaultCombineHandler) GetHandler() http.Handler { return s } +var RequestCounter = promauto.NewCounterVec(prometheus.CounterOpts{ + Name: "http_requests_total", + Help: "The total number of HTTP requests", +}, []string{"method", "source", "path"}) + func (s *defaultCombineHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + sourceIP := r.RemoteAddr + if len(strings.Split(sourceIP, ":")) > 1 { + sourceIP = strings.Split(sourceIP, ":")[0] + } + RequestCounter.WithLabelValues(r.Method, sourceIP, r.RequestURI).Inc() + for prefix, handler := range s.handlerMapping { if strings.HasPrefix(r.URL.Path, prefix) { handler.ServeHTTP(w, r)