MCPcopy
hub / github.com/redis/go-redis / RecordOperationDuration

Method RecordOperationDuration

extra/redisotel-native/metrics.go:63–126  ·  view source on GitHub ↗

RecordOperationDuration records db.client.operation.duration metric

(
	ctx context.Context,
	duration time.Duration,
	cmd redis.Cmder,
	attempts int,
	err error,
	cn redis.ConnInfo,
	dbIndex int,
)

Source from the content-addressed store, hash-verified

61
62// RecordOperationDuration records db.client.operation.duration metric
63func (r *metricsRecorder) RecordOperationDuration(
64 ctx context.Context,
65 duration time.Duration,
66 cmd redis.Cmder,
67 attempts int,
68 err error,
69 cn redis.ConnInfo,
70 dbIndex int,
71) {
72 if r.operationDuration == nil {
73 return
74 }
75
76 // Check if command should be included
77 if r.cfg != nil && !r.cfg.isCommandIncluded(cmd.Name()) {
78 return
79 }
80
81 // Convert duration to seconds (OTel convention for duration metrics)
82 durationSeconds := duration.Seconds()
83
84 serverAddr, serverPort := extractServerInfo(cn)
85
86 // Build attributes
87 attrs := []attribute.KeyValue{
88 // Required attributes
89 attribute.String(AttrDBOperationName, cmd.FullName()),
90 getLibraryVersionAttr(),
91 attribute.Int(AttrRedisClientOperationRetryAttempts, attempts-1), // attempts-1 = retry count
92
93 // Recommended attributes
94 attribute.String(AttrDBSystemName, DBSystemRedis),
95 attribute.String(AttrServerAddress, serverAddr),
96 attribute.String(AttrDBNamespace, strconv.Itoa(dbIndex)),
97 }
98
99 // Add server.port if not default
100 attrs = addServerPortIfNonDefault(attrs, serverPort)
101
102 // Add network.peer.address and network.peer.port from connection
103 if cn != nil {
104 remoteAddr := cn.RemoteAddr()
105 if remoteAddr != nil {
106 peerAddr, peerPort := splitHostPort(remoteAddr.String())
107 if peerAddr != "" {
108 attrs = append(attrs, attribute.String(AttrNetworkPeerAddress, peerAddr))
109 }
110 if peerPort != "" {
111 attrs = append(attrs, attribute.String(AttrNetworkPeerPort, peerPort))
112 }
113 }
114 }
115
116 if err != nil {
117 attrs = append(attrs, attribute.String(AttrErrorType, classifyError(err)))
118 attrs = append(attrs, attribute.String(AttrRedisClientErrorsCategory, getErrorCategory(err)))
119 if statusCode := extractRedisErrorPrefix(err); statusCode != "" {
120 attrs = append(attrs, attribute.String(AttrDBResponseStatusCode, statusCode))

Callers

nothing calls this directly

Calls 14

extractServerInfoFunction · 0.85
getLibraryVersionAttrFunction · 0.85
splitHostPortFunction · 0.85
classifyErrorFunction · 0.85
getErrorCategoryFunction · 0.85
extractRedisErrorPrefixFunction · 0.85
isCommandIncludedMethod · 0.80
RecordMethod · 0.80
NameMethod · 0.65
StringMethod · 0.65
FullNameMethod · 0.65

Tested by

no test coverage detected