master
nikkiing 10 months ago
parent 3bf07e0b28
commit 62a894e89e
  1. 72
      glog/es6/es.go
  2. 1
      go.mod
  3. 2
      go.sum

@ -0,0 +1,72 @@
package es6
import (
"bytes"
"context"
"fmt"
"gitea.drugeyes.vip/pharnexbase/utils/glog/v1"
"github.com/elastic/go-elasticsearch/v6/estransport"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
"go.opentelemetry.io/otel/trace"
"net/http"
"time"
)
var _ estransport.Logger = (*Logger)(nil)
type Logger struct {
EnableRequestBody bool
EnableResponseBody bool
}
func (l *Logger) LogRoundTrip(request *http.Request, response *http.Response, err error, time time.Time, duration time.Duration) error {
ctx := request.Context()
index := fmt.Sprintf("%s?%s", request.URL.Path, request.URL.RawQuery)
method := request.Method
if l.RequestBodyEnabled() && request != nil && request.Body != nil && request.Body != http.NoBody {
var buf bytes.Buffer
if request.GetBody != nil {
b, _ := request.GetBody()
_, _ = buf.ReadFrom(b)
} else {
_, _ = buf.ReadFrom(request.Body)
}
glog.Glog.WithContext(ctx).Info("url:", method, index, " request:", buf.String())
}
if l.ResponseBodyEnabled() && response != nil && response.Body != nil && response.Body != http.NoBody {
defer response.Body.Close()
var buf bytes.Buffer
_, _ = buf.ReadFrom(response.Body)
glog.Glog.WithContext(ctx).Info("response:", buf.String())
}
l.Tracer(ctx, time, method, index)
return nil
}
func (l *Logger) Tracer(ctx context.Context, begin time.Time, method, index string) {
tracer := otel.Tracer("elasticsearch-v6.7.0")
kind := trace.SpanKindInternal
ctx, span := tracer.Start(ctx,
index,
trace.WithSpanKind(kind),
trace.WithTimestamp(begin),
)
var attrs []attribute.KeyValue
attrs = append(attrs, semconv.DBSystemElasticsearch)
attrs = append(attrs, attribute.Key("es.index").String(method+" "+index))
span.SetAttributes(attrs...)
span.End(trace.WithTimestamp(time.Now()))
}
func (l *Logger) RequestBodyEnabled() bool {
return l.EnableRequestBody
}
func (l *Logger) ResponseBodyEnabled() bool {
return l.EnableResponseBody
}

@ -6,6 +6,7 @@ require (
gitea.drugeyes.vip/pharnexbase/tools v1.0.0
github.com/aliyun-sls/opentelemetry-go-provider-sls v0.9.0
github.com/aliyun/aliyun-log-go-sdk v0.1.43
github.com/elastic/go-elasticsearch/v6 v6.8.10
github.com/elastic/go-elasticsearch/v7 v7.10.0
github.com/go-kratos/kratos/v2 v2.5.4
github.com/tidwall/gjson v1.14.4

@ -604,6 +604,8 @@ github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5m
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
github.com/elastic/go-elasticsearch/v6 v6.8.10 h1:2lN0gJ93gMBXvkhwih5xquldszpm8FlUwqG5sPzr6a8=
github.com/elastic/go-elasticsearch/v6 v6.8.10/go.mod h1:UwaDJsD3rWLM5rKNFzv9hgox93HoX8utj1kxD9aFUcI=
github.com/elastic/go-elasticsearch/v7 v7.10.0 h1:vYRwqgFM46ZUHFMRdvKr+y1WA4ehJO6WqAGV9Btbl2o=
github.com/elastic/go-elasticsearch/v7 v7.10.0/go.mod h1:OJ4wdbtDNk5g503kvlHLyErCgQwwzmDtaFC4XyOxXA4=
github.com/emicklei/go-restful/v3 v3.9.0 h1:XwGDlfxEnQZzuopoqxwSEllNcCOM9DhhFyhFIIGKwxE=

Loading…
Cancel
Save