Skip to content

Commit 92cc106

Browse files
authored
Don't emit higher cardinality attributes for metrics (#14)
This ones follows up #11. I said in the pull request that higher cardinality attributes aren't emitted with metrics, but I got that wrong as I'd forgotten I was feeding all span attributes into metrics as well. Here, bifurcate attributes so that high cardinality attributes are added to spans, but not to metrics.
1 parent cdb104d commit 92cc106

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

otelriver/middleware.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ func (m *Middleware) InsertMany(ctx context.Context, manyParams []*rivertype.Job
126126
defer func() {
127127
duration := m.durationInPreferredUnit(time.Since(begin))
128128

129-
setAttributeAndSpanStatus(attrs, statusIndex, span, panicked, err)
129+
setStatus(attrs, statusIndex, span, panicked, err)
130+
span.SetAttributes(attrs...) // set after finalizing status
130131

131132
// This allocates a new slice, so make sure to do it as few times as possible.
132133
measurementOpt := metric.WithAttributes(attrs...)
@@ -148,17 +149,14 @@ func (m *Middleware) Work(ctx context.Context, job *rivertype.JobRow, doInner fu
148149
defer span.End()
149150

150151
attrs := []attribute.KeyValue{
151-
attribute.Int64("id", job.ID),
152152
attribute.Int("attempt", job.Attempt),
153-
attribute.String("created_at", job.CreatedAt.Format(time.RFC3339)),
154153
attribute.String("kind", job.Kind),
155154
attribute.Int("priority", job.Priority),
156155
attribute.String("queue", job.Queue),
157-
attribute.String("scheduled_at", job.ScheduledAt.Format(time.RFC3339)),
158156
attribute.String("status", ""), // replaced below
159157
attribute.StringSlice("tag", job.Tags),
160158
}
161-
const statusIndex = 7
159+
const statusIndex = 4
162160

163161
var (
164162
begin = time.Now()
@@ -181,7 +179,19 @@ func (m *Middleware) Work(ctx context.Context, job *rivertype.JobRow, doInner fu
181179
}
182180
}
183181

184-
setAttributeAndSpanStatus(attrs, statusIndex, span, panicked, err)
182+
setStatus(attrs, statusIndex, span, panicked, err)
183+
184+
{
185+
// Add some higher cardinality attributes to spans, but keep them
186+
// out of metrics given it's been traditional wisdom that metric
187+
// attribute sets shouldn't be too large.
188+
attrs := append(attrs,
189+
attribute.Int64("id", job.ID),
190+
attribute.String("created_at", job.CreatedAt.Format(time.RFC3339)),
191+
attribute.String("scheduled_at", job.ScheduledAt.Format(time.RFC3339)),
192+
)
193+
span.SetAttributes(attrs...) // set after finalizing status
194+
}
185195

186196
// This allocates a new slice, so make sure to do it as few times as possible.
187197
measurementOpt := metric.WithAttributes(attrs...)
@@ -234,7 +244,7 @@ func mustInt64Counter(meter metric.Meter, name string, options ...metric.Int64Co
234244
// Sets success status on the given span and within the set of attributes. The
235245
// index of the status attribute is required ahead of time as a minor
236246
// optimization.
237-
func setAttributeAndSpanStatus(attrs []attribute.KeyValue, statusIndex int, span trace.Span, panicked bool, err error) {
247+
func setStatus(attrs []attribute.KeyValue, statusIndex int, span trace.Span, panicked bool, err error) {
238248
if attrs[statusIndex].Key != "status" {
239249
panic("status attribute not at expected index; bug?") // protect against future regression
240250
}
@@ -250,5 +260,4 @@ func setAttributeAndSpanStatus(attrs []attribute.KeyValue, statusIndex int, span
250260
attrs[statusIndex] = attribute.String("status", "ok")
251261
span.SetStatus(codes.Ok, "")
252262
}
253-
span.SetAttributes(attrs...) // set after finalizing status
254263
}

0 commit comments

Comments
 (0)