Skip to content

Commit 34b5f7e

Browse files
committed
Preallocate slice length instead of append
1 parent e498c9a commit 34b5f7e

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

extra/redisotel/metrics.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,10 @@ func (mh *metricsHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
214214

215215
dur := time.Since(start)
216216

217-
attrs := make([]attribute.KeyValue, 0, len(mh.attrs)+2)
218-
attrs = append(attrs, mh.attrsFunc(ctx)...)
217+
customAttrs := mh.attrsFunc(ctx)
218+
219+
attrs := make([]attribute.KeyValue, 0, len(mh.attrs) + len(customAttrs) + 2)
220+
attrs = append(attrs, customAttrs...)
219221
attrs = append(attrs, mh.attrs...)
220222
attrs = append(attrs, attribute.String("type", "command"))
221223
attrs = append(attrs, statusAttr(err))
@@ -236,8 +238,10 @@ func (mh *metricsHook) ProcessPipelineHook(
236238

237239
dur := time.Since(start)
238240

239-
attrs := make([]attribute.KeyValue, 0, len(mh.attrs)+2)
240-
attrs = append(attrs, mh.attrsFunc(ctx)...)
241+
customAttrs := mh.attrsFunc(ctx)
242+
243+
attrs := make([]attribute.KeyValue, 0, len(mh.attrs) + len(customAttrs) + 2)
244+
attrs = append(attrs, customAttrs...)
241245
attrs = append(attrs, mh.attrs...)
242246
attrs = append(attrs, attribute.String("type", "pipeline"))
243247
attrs = append(attrs, statusAttr(err))

extra/redisotel/tracing.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ func (th *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
103103
return func(ctx context.Context, cmd redis.Cmder) error {
104104
fn, file, line := funcFileLine("github.com/redis/go-redis")
105105

106-
attrs := make([]attribute.KeyValue, 0, 8)
106+
107+
customAttrs := th.conf.attrsFunc(ctx)
108+
109+
attrs := make([]attribute.KeyValue, 0, len(customAttrs) + 8)
107110
attrs = append(attrs,
108111
semconv.CodeFunction(fn),
109112
semconv.CodeFilepath(file),
@@ -116,7 +119,7 @@ func (th *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
116119
}
117120

118121
opts := th.spanOpts
119-
opts = append(opts, trace.WithAttributes(th.conf.attrsFunc(ctx)...))
122+
opts = append(opts, trace.WithAttributes(customAttrs...))
120123
opts = append(opts, trace.WithAttributes(attrs...))
121124

122125
ctx, span := th.conf.tracer.Start(ctx, cmd.FullName(), opts...)
@@ -136,7 +139,9 @@ func (th *tracingHook) ProcessPipelineHook(
136139
return func(ctx context.Context, cmds []redis.Cmder) error {
137140
fn, file, line := funcFileLine("github.com/redis/go-redis")
138141

139-
attrs := make([]attribute.KeyValue, 0, 8)
142+
customAttrs := th.conf.attrsFunc(ctx)
143+
144+
attrs := make([]attribute.KeyValue, 0, len(customAttrs) + 8)
140145
attrs = append(attrs,
141146
semconv.CodeFunction(fn),
142147
semconv.CodeFilepath(file),
@@ -150,7 +155,7 @@ func (th *tracingHook) ProcessPipelineHook(
150155
}
151156

152157
opts := th.spanOpts
153-
opts = append(opts, trace.WithAttributes(th.conf.attrsFunc(ctx)...))
158+
opts = append(opts, trace.WithAttributes(customAttrs...))
154159
opts = append(opts, trace.WithAttributes(attrs...))
155160

156161
ctx, span := th.conf.tracer.Start(ctx, "redis.pipeline "+summary, opts...)

0 commit comments

Comments
 (0)