diff --git a/contrib/opbot/botmd/middleware.go b/contrib/opbot/botmd/middleware.go index 48af04c303..88de5e8809 100644 --- a/contrib/opbot/botmd/middleware.go +++ b/contrib/opbot/botmd/middleware.go @@ -23,7 +23,7 @@ func (b *Bot) tracingMiddleware() slacker.CommandMiddlewareHandler { return func(cmdCtx *slacker.CommandContext) { ctx, span := b.handler.Tracer().Start(cmdCtx.Context(), fmt.Sprintf("command.%s", cmdCtx.Definition().Command), trace.WithAttributes( attribute.String("user_id", cmdCtx.Event().UserID), - attribute.String("channel_id", cmdCtx.Event().Channel.ID), + attribute.String("channel_id", retrieveChannelIfExists(cmdCtx.Event())), )) cmdCtx.WithContext(ctx) @@ -37,6 +37,15 @@ func (b *Bot) tracingMiddleware() slacker.CommandMiddlewareHandler { } } +const unknownChannel = "unknown" + +func retrieveChannelIfExists(event *slacker.MessageEvent) string { + if event != nil && event.Channel != nil { + return event.Channel.ID + } + return unknownChannel +} + // assumes method is only called once. type otelRecorder struct { attemptsCounter metric.Int64UpDownCounter