Documentation ¶
Overview ¶
Package otgorm allows for the wrapping of GORM calls to databases with OpenTelemetry tracing spans. You only need to create your GORM db client and pass that into otgorm.WithContext along with context.Context(). If there is a parent span referenced within the context the GORM call will be a child span.
Index ¶
Constants ¶
const ( TableKey = attribute.Key("gorm.table") // The table the GORM query is acting upon QueryKey = attribute.Key("gorm.query") // The GORM query itself )
Attributes that may or may not be added to a span based on Options used
Variables ¶
var LogFormatter = func(values ...interface{}) string { var ( sql string formattedValues []string ) for _, value := range values[1].([]interface{}) { indirectValue := reflect.Indirect(reflect.ValueOf(value)) if indirectValue.IsValid() { value = indirectValue.Interface() if t, ok := value.(time.Time); ok { if t.IsZero() { formattedValues = append(formattedValues, fmt.Sprintf("'%v'", "0000-00-00 00:00:00")) } else { formattedValues = append(formattedValues, fmt.Sprintf("'%v'", t.Format("2006-01-02 15:04:05"))) } } else if b, ok := value.([]byte); ok { if str := string(b); isPrintable(str) { formattedValues = append(formattedValues, fmt.Sprintf("'%v'", str)) } else { formattedValues = append(formattedValues, "'<binary>'") } } else if r, ok := value.(driver.Valuer); ok { if value, err := r.Value(); err == nil && value != nil { formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value)) } else { formattedValues = append(formattedValues, "NULL") } } else { switch value.(type) { case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, bool: formattedValues = append(formattedValues, fmt.Sprintf("%v", value)) default: formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value)) } } } else { formattedValues = append(formattedValues, "NULL") } } if numericPlaceHolderRegexp.MatchString(values[0].(string)) { sql = values[0].(string) for index, value := range formattedValues { placeholder := fmt.Sprintf(`\$%d([^\d]|$)`, index+1) sql = regexp.MustCompile(placeholder).ReplaceAllString(sql, value+"$1") } } else { formattedValuesLength := len(formattedValues) for index, value := range sqlRegexp.Split(values[0].(string), -1) { sql += value if index < formattedValuesLength { sql += formattedValues[index] } } } return sql }
Functions ¶
func RegisterCallbacks ¶
RegisterCallbacks registers the necessary callbacks in Gorm's hook system for instrumentation with OpenTelemetry Spans.
Types ¶
type AllowRoot ¶
type AllowRoot bool
AllowRoot allows creating root spans in the absence of existing spans.
type DefaultAttributes ¶
DefaultAttributes sets attributes to each span.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option allows for managing otgorm configuration using functional options.
type OptionFunc ¶
type OptionFunc func(c *callbacks)
OptionFunc converts a regular function to an Option if it's definition is compatible.
func WithSpanOptions ¶
func WithSpanOptions(opts ...trace.SpanStartOption) OptionFunc
WithSpanOptions configures the db callback functions with an additional set of trace.StartOptions which will be applied to each new span
func WithTracer ¶
func WithTracer(tracer trace.Tracer) OptionFunc
WithTracer configures the tracer to use when starting spans. Otherwise the global tracer is used with a default name