You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by ma...@apache.org on 2023/09/23 09:59:25 UTC
[dubbo-go-pixiu] branch develop updated: add metrics (#573)
This is an automated email from the ASF dual-hosted git repository.
mark4z pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/dubbo-go-pixiu.git
The following commit(s) were added to refs/heads/develop by this push:
new b9366be9 add metrics (#573)
b9366be9 is described below
commit b9366be9051ea076e7015ef21a79abc9648e66f8
Author: ma642 <ma...@126.com>
AuthorDate: Sat Sep 23 17:59:20 2023 +0800
add metrics (#573)
* foramtter
* fix review issue
---------
Co-authored-by: mazhihui <ma...@baimaohui.net>
---
pixiu/pkg/filter/metric/metric.go | 89 +++++++++++++++++++++++++++++++++++++-
pixiu/pkg/prometheus/prometheus.go | 4 +-
2 files changed, 88 insertions(+), 5 deletions(-)
diff --git a/pixiu/pkg/filter/metric/metric.go b/pixiu/pkg/filter/metric/metric.go
index aa52fd84..13d6b848 100644
--- a/pixiu/pkg/filter/metric/metric.go
+++ b/pixiu/pkg/filter/metric/metric.go
@@ -18,16 +18,21 @@
package metric
import (
+ "fmt"
+ stdhttp "net/http"
"time"
)
import (
+ "github.com/pkg/errors"
+ "go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/instrument/syncint64"
)
import (
+ "github.com/apache/dubbo-go-pixiu/pixiu/pkg/client"
"github.com/apache/dubbo-go-pixiu/pixiu/pkg/common/constant"
"github.com/apache/dubbo-go-pixiu/pixiu/pkg/common/extension/filter"
"github.com/apache/dubbo-go-pixiu/pixiu/pkg/context/http"
@@ -42,6 +47,10 @@ var (
totalElapsed syncint64.Counter
totalCount syncint64.Counter
totalError syncint64.Counter
+
+ sizeRequest syncint64.Counter
+ sizeResponse syncint64.Counter
+ durationHist syncint64.Histogram
)
func init() {
@@ -93,17 +102,71 @@ func (f *Filter) Decode(c *http.HttpContext) filter.FilterStatus {
}
func (f *Filter) Encode(c *http.HttpContext) filter.FilterStatus {
+
+ commonAttrs := []attribute.KeyValue{
+ attribute.String("code", fmt.Sprintf("%d", c.GetStatusCode())),
+ attribute.String("method", c.Request.Method),
+ attribute.String("url", c.GetUrl()),
+ attribute.String("host", c.Request.Host),
+ }
+
latency := time.Since(f.start)
- totalCount.Add(c.Ctx, 1)
- totalElapsed.Add(c.Ctx, latency.Nanoseconds())
+ totalCount.Add(c.Ctx, 1, commonAttrs...)
+ latencyMilli := latency.Milliseconds()
+ totalElapsed.Add(c.Ctx, latencyMilli, commonAttrs...)
if c.LocalReply() {
totalError.Add(c.Ctx, 1)
}
+ durationHist.Record(c.Ctx, latencyMilli, commonAttrs...)
+ size, err := computeApproximateRequestSize(c.Request)
+ if err != nil {
+ logger.Warn("can not compute request size", err)
+ } else {
+ sizeRequest.Add(c.Ctx, int64(size), commonAttrs...)
+ }
+
+ size, err = computeApproximateResponseSize(c.TargetResp)
+ if err != nil {
+ logger.Warn("can not compute response size", err)
+ } else {
+ sizeResponse.Add(c.Ctx, int64(size), commonAttrs...)
+ }
+
logger.Debugf("[Metric] [UPSTREAM] receive request | %d | %s | %s | %s | ", c.GetStatusCode(), latency, c.GetMethod(), c.GetUrl())
return filter.Continue
}
+func computeApproximateResponseSize(res *client.Response) (int, error) {
+ if res == nil {
+ return 0, errors.New("client.Response is null pointer ")
+ }
+ return len(res.Data), nil
+}
+
+func computeApproximateRequestSize(r *stdhttp.Request) (int, error) {
+ if r == nil {
+ return 0, errors.New("http.Request is null pointer ")
+ }
+ s := 0
+ if r.URL != nil {
+ s = len(r.URL.Path)
+ }
+ s += len(r.Method)
+ s += len(r.Proto)
+ for name, values := range r.Header {
+ s += len(name)
+ for _, value := range values {
+ s += len(value)
+ }
+ }
+ s += len(r.Host)
+ if r.ContentLength != -1 {
+ s += int(r.ContentLength)
+ }
+ return s, nil
+}
+
func registerOtelMetric() error {
meter := global.MeterProvider().Meter("pixiu")
@@ -127,5 +190,27 @@ func registerOtelMetric() error {
return err
}
totalError = errorCounter
+
+ sizeRequest, err = meter.SyncInt64().Counter("pixiu_request_content_length", instrument.WithDescription("request total content length in pixiu"))
+ if err != nil {
+ logger.Errorf("register pixiu_request_content_length metric failed, err: %v", err)
+ return err
+ }
+
+ sizeResponse, err = meter.SyncInt64().Counter("pixiu_response_content_length", instrument.WithDescription("request total content length response in pixiu"))
+ if err != nil {
+ logger.Errorf("register pixiu_response_content_length metric failed, err: %v", err)
+ return err
+ }
+
+ durationHist, err = meter.SyncInt64().Histogram(
+ "pixiu_process_time_millicec",
+ instrument.WithDescription("request process time response in pixiu"),
+ )
+ if err != nil {
+ logger.Errorf("register pixiu_process_time_millisec metric failed, err: %v", err)
+ return err
+ }
+
return nil
}
diff --git a/pixiu/pkg/prometheus/prometheus.go b/pixiu/pkg/prometheus/prometheus.go
index 6d28819e..9ef02904 100644
--- a/pixiu/pkg/prometheus/prometheus.go
+++ b/pixiu/pkg/prometheus/prometheus.go
@@ -386,7 +386,5 @@ func computeApproximateResponseSize(res *client.Response) (int, error) {
if res == nil {
return 0, errors.New("client.Response is null pointer ")
}
- s := 0
- s += len(res.Data)
- return s, nil
+ return len(res.Data), nil
}