github.com/wangyougui/gf/v2@v2.6.5/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/wangyougui/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.4.0"
    17  	"go.opentelemetry.io/otel/trace"
    18  
    19  	"github.com/wangyougui/gf/v2/net/gtrace"
    20  )
    21  
    22  const (
    23  	traceInstrumentName       = "github.com/wangyougui/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  	traceEventDbExecutionSql  = "db.execution.sql"
    33  	traceEventDbExecutionCost = "db.execution.cost"
    34  	traceEventDbExecutionRows = "db.execution.rows"
    35  	traceEventDbExecutionTxID = "db.execution.txid"
    36  	traceEventDbExecutionType = "db.execution.type"
    37  )
    38  
    39  // addSqlToTracing adds sql information to tracer if it's enabled.
    40  func (c *Core) traceSpanEnd(ctx context.Context, span trace.Span, sql *Sql) {
    41  	if gtrace.IsUsingDefaultProvider() || !gtrace.IsTracingInternal() {
    42  		return
    43  	}
    44  	if sql.Error != nil {
    45  		span.SetStatus(codes.Error, fmt.Sprintf(`%+v`, sql.Error))
    46  	}
    47  	labels := make([]attribute.KeyValue, 0)
    48  	labels = append(labels, gtrace.CommonLabels()...)
    49  	labels = append(labels,
    50  		attribute.String(traceAttrDbType, c.db.GetConfig().Type),
    51  		semconv.DBStatementKey.String(sql.Format),
    52  	)
    53  	if c.db.GetConfig().Host != "" {
    54  		labels = append(labels, attribute.String(traceAttrDbHost, c.db.GetConfig().Host))
    55  	}
    56  	if c.db.GetConfig().Port != "" {
    57  		labels = append(labels, attribute.String(traceAttrDbPort, c.db.GetConfig().Port))
    58  	}
    59  	if c.db.GetConfig().Name != "" {
    60  		labels = append(labels, attribute.String(traceAttrDbName, c.db.GetConfig().Name))
    61  	}
    62  	if c.db.GetConfig().User != "" {
    63  		labels = append(labels, attribute.String(traceAttrDbUser, c.db.GetConfig().User))
    64  	}
    65  	if filteredLink := c.db.GetCore().FilteredLink(); filteredLink != "" {
    66  		labels = append(labels, attribute.String(traceAttrDbLink, c.db.GetCore().FilteredLink()))
    67  	}
    68  	if group := c.db.GetGroup(); group != "" {
    69  		labels = append(labels, attribute.String(traceAttrDbGroup, group))
    70  	}
    71  	span.SetAttributes(labels...)
    72  	events := []attribute.KeyValue{
    73  		attribute.String(traceEventDbExecutionSql, sql.Format),
    74  		attribute.String(traceEventDbExecutionCost, fmt.Sprintf(`%d ms`, sql.End-sql.Start)),
    75  		attribute.String(traceEventDbExecutionRows, fmt.Sprintf(`%d`, sql.RowsAffected)),
    76  	}
    77  	if sql.IsTransaction {
    78  		if v := ctx.Value(transactionIdForLoggerCtx); v != nil {
    79  			events = append(events, attribute.String(
    80  				traceEventDbExecutionTxID, fmt.Sprintf(`%d`, v.(uint64)),
    81  			))
    82  		}
    83  	}
    84  	events = append(events, attribute.String(traceEventDbExecutionType, sql.Type))
    85  	span.AddEvent(traceEventDbExecution, trace.WithAttributes(events...))
    86  }