mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
add common tracing labels
This commit is contained in:
@ -23,18 +23,21 @@ func (c *Core) addSqlToTracing(ctx context.Context, sql *Sql) {
|
||||
if gtrace.IsActivated(ctx) {
|
||||
return
|
||||
}
|
||||
|
||||
tr := otel.GetTracerProvider().Tracer(
|
||||
"github.com/gogf/gf/database/gdb",
|
||||
trace.WithInstrumentationVersion(fmt.Sprintf(`%s`, gf.VERSION)),
|
||||
)
|
||||
ctx, span := tr.Start(ctx, sql.Type)
|
||||
defer span.End()
|
||||
|
||||
if sql.Error != nil {
|
||||
span.SetStatus(codes.Error, fmt.Sprintf(`%+v`, sql.Error))
|
||||
}
|
||||
labels := make([]label.KeyValue, 0)
|
||||
labels = append(labels, label.String("db.type", c.DB.GetConfig().Type))
|
||||
labels = append(labels, gtrace.CommonLabels()...)
|
||||
labels = append(labels,
|
||||
label.String("db.type", c.DB.GetConfig().Type),
|
||||
)
|
||||
if c.DB.GetConfig().Host != "" {
|
||||
labels = append(labels, label.String("db.host", c.DB.GetConfig().Host))
|
||||
}
|
||||
|
||||
@ -82,6 +82,7 @@ func (c *Conn) do(timeout time.Duration, commandName string, args ...interface{}
|
||||
if err != nil {
|
||||
span.SetStatus(codes.Error, fmt.Sprintf(`%+v`, err))
|
||||
}
|
||||
span.SetAttributes(gtrace.CommonLabels()...)
|
||||
span.SetAttributes(
|
||||
label.String("redis.host", c.redis.config.Host),
|
||||
label.Int("redis.port", c.redis.config.Port),
|
||||
|
||||
@ -12,6 +12,7 @@ import (
|
||||
"github.com/gogf/gf/internal/utils"
|
||||
"github.com/gogf/gf/net/ghttp/internal/client"
|
||||
"github.com/gogf/gf/net/ghttp/internal/httputil"
|
||||
"github.com/gogf/gf/net/gtrace"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/label"
|
||||
@ -45,6 +46,8 @@ func MiddlewareServerTracing(r *Request) {
|
||||
ctx, span := tr.Start(ctx, r.URL.String())
|
||||
defer span.End()
|
||||
|
||||
span.SetAttributes(gtrace.CommonLabels()...)
|
||||
|
||||
// Inject tracing context.
|
||||
r.SetCtx(ctx)
|
||||
|
||||
|
||||
@ -11,6 +11,7 @@ import (
|
||||
"github.com/gogf/gf"
|
||||
"github.com/gogf/gf/internal/utils"
|
||||
"github.com/gogf/gf/net/ghttp/internal/httputil"
|
||||
"github.com/gogf/gf/net/gtrace"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/label"
|
||||
@ -34,6 +35,8 @@ func MiddlewareTracing(c *Client, r *http.Request) (response *Response, err erro
|
||||
ctx, span := tr.Start(r.Context(), r.URL.String())
|
||||
defer span.End()
|
||||
|
||||
span.SetAttributes(gtrace.CommonLabels()...)
|
||||
|
||||
// Inject tracing content into http header.
|
||||
propagator := propagation.NewCompositeTextMapPropagator(
|
||||
propagation.TraceContext{},
|
||||
|
||||
@ -10,10 +10,17 @@ package gtrace
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/container/gvar"
|
||||
"github.com/gogf/gf/net/gipv4"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/baggage"
|
||||
"go.opentelemetry.io/otel/label"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
intranetIps, _ = gipv4.GetIntranetIpArray()
|
||||
hostname, _ = os.Hostname()
|
||||
)
|
||||
|
||||
// IsActivated checks and returns if tracing feature is activated.
|
||||
@ -21,6 +28,15 @@ func IsActivated(ctx context.Context) bool {
|
||||
return GetTraceId(ctx) != ""
|
||||
}
|
||||
|
||||
// CommonLabels returns common used attribute labels:
|
||||
// ip.intranet, hostname.
|
||||
func CommonLabels() []label.KeyValue {
|
||||
return []label.KeyValue{
|
||||
label.Array(`ip.intranet`, intranetIps),
|
||||
label.String(`hostname`, hostname),
|
||||
}
|
||||
}
|
||||
|
||||
// Tracer is a short function for retrieve Tracer.
|
||||
func Tracer(name ...string) trace.Tracer {
|
||||
tracerName := ""
|
||||
|
||||
Reference in New Issue
Block a user