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, )
| 61 | |
| 62 | // RecordOperationDuration records db.client.operation.duration metric |
| 63 | func (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)) |
nothing calls this directly
no test coverage detected