github.com/gogf/gf/v2@v2.7.4/database/gdb/gdb_core_trace.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  //
     7  
     8  package gdb
     9  
    10  import (
    11  	"context"
    12  	"fmt"
    13  
    14  	"go.opentelemetry.io/otel/attribute"
    15  	"go.opentelemetry.io/otel/codes"
    16  	semconv "go.opentelemetry.io/otel/semconv/v1.18.0"
    17  	"go.opentelemetry.io/otel/trace"
    18  
    19  	"github.com/gogf/gf/v2/net/gtrace"
    20  )
    21  
    22  const (
    23  	traceInstrumentName       = "github.com/gogf/gf/v2/database/gdb"
    24  	traceAttrDbType           = "db.type"
    25  	traceAttrDbHost           = "db.host"
    26  	traceAttrDbPort           = "db.port"
    27  	traceAttrDbName           = "db.name"
    28  	traceAttrDbUser           = "db.user"
    29  	traceAttrDbLink           = "db.link"
    30  	traceAttrDbGroup          = "db.group"
    31  	traceEventDbExecution     = "db.execution"
    32  	traceEventDbExecutionCost = "db.execution.cost"
    33  	traceEventDbExecutionRows = "db.execution.rows"
    34  	traceEventDbExecutionTxID = "db.execution.txid"
    35  	traceEventDbExecutionType = "db.execution.type"
    36  )
    37  
    38  // addSqlToTracing adds sql information to tracer if it's enabled.
    39  func (c *Core) traceSpanEnd(ctx context.Context, span trace.Span, sql *Sql) {
    40  	if gtrace.IsUsingDefaultProvider() || !gtrace.IsTracingInternal() {
    41  		return
    42  	}
    43  	if sql.Error != nil {
    44  		span.SetStatus(codes.Error, fmt.Sprintf(`%+v`, sql.Error))
    45  	}
    46  	labels := make([]attribute.KeyValue, 0)
    47  	labels = append(labels, gtrace.CommonLabels()...)
    48  	labels = append(labels,
    49  		attribute.String(traceAttrDbType, c.db.GetConfig().Type),
    50  		semconv.DBStatement(sql.Format),
    51  	)
    52  	if c.db.GetConfig().Host != "" {
    53  		labels = append(labels, attribute.String(traceAttrDbHost, c.db.GetConfig().Host))
    54  	}
    55  	if c.db.GetConfig().Port != "" {
    56  		labels = append(labels, attribute.String(traceAttrDbPort, c.db.GetConfig().Port))
    57  	}
    58  	if c.db.GetConfig().Name != "" {
    59  		labels = append(labels, attribute.String(traceAttrDbName, c.db.GetConfig().Name))
    60  	}
    61  	if c.db.GetConfig().User != "" {
    62  		labels = append(labels, attribute.String(traceAttrDbUser, c.db.GetConfig().User))
    63  	}
    64  	if filteredLink := c.db.GetCore().FilteredLink(); filteredLink != "" {
    65  		labels = append(labels, attribute.String(traceAttrDbLink, c.db.GetCore().FilteredLink()))
    66  	}
    67  	if group := c.db.GetGroup(); group != "" {
    68  		labels = append(labels, attribute.String(traceAttrDbGroup, group))
    69  	}
    70  	span.SetAttributes(labels...)
    71  	events := []attribute.KeyValue{
    72  		attribute.String(traceEventDbExecutionCost, fmt.Sprintf(`%d ms`, sql.End-sql.Start)),
    73  		attribute.String(traceEventDbExecutionRows, fmt.Sprintf(`%d`, sql.RowsAffected)),
    74  	}
    75  	if sql.IsTransaction {
    76  		if v := ctx.Value(transactionIdForLoggerCtx); v != nil {
    77  			events = append(events, attribute.String(
    78  				traceEventDbExecutionTxID, fmt.Sprintf(`%d`, v.(uint64)),
    79  			))
    80  		}
    81  	}
    82  	events = append(events, attribute.String(traceEventDbExecutionType, string(sql.Type)))
    83  	span.AddEvent(traceEventDbExecution, trace.WithAttributes(events...))
    84  }