@@ -100,22 +100,36 @@ func (e *KafkaBrokerDriver) TopicExists(ctx context.Context, topic string) (bool
100100}
101101
102102// EnsureTopics creates topics if they don't already exist (idempotent).
103- func (e * KafkaBrokerDriver ) EnsureTopics (ctx context.Context , topics []string ) error {
104- resp , err := e .admin .CreateTopics (ctx , 1 , 1 , nil , topics ... )
105- if err != nil {
106- return fmt .Errorf ("failed to create topics: %w" , err )
107- }
103+ // metadata is an optional map of topic name -> key-value pairs for logging/tracking purposes.
104+ // For WebSubAPI topics, metadata typically includes: apiName, apiVersion, channelName.
105+ // Note: metadata is logged but not stored in Kafka (Kafka only accepts specific configuration keys).
106+ func (e * KafkaBrokerDriver ) EnsureTopics (ctx context.Context , topics []string , metadata map [string ]map [string ]string ) error {
107+ // Create topics one by one to allow logging metadata per topic
108+ for _ , topic := range topics {
109+ resp , err := e .admin .CreateTopics (ctx , 1 , 1 , nil , topic )
110+ if err != nil {
111+ return fmt .Errorf ("failed to create topic %s: %w" , topic , err )
112+ }
108113
109- for _ , t := range resp .Sorted () {
110- if t .Err != nil {
111- // "TOPIC_ALREADY_EXISTS" is not a real failure for idempotent creates.
112- if isTopicAlreadyExistsErr (t .Err ) {
113- slog .Debug ("Topic already exists" , "topic" , t .Topic )
114- continue
114+ for _ , t := range resp .Sorted () {
115+ if t .Err != nil {
116+ // "TOPIC_ALREADY_EXISTS" is not a real failure for idempotent creates.
117+ if isTopicAlreadyExistsErr (t .Err ) {
118+ slog .Debug ("Topic already exists" , "topic" , t .Topic )
119+ continue
120+ }
121+ return fmt .Errorf ("failed to create topic %s: %w" , t .Topic , t .Err )
122+ }
123+ slog .Info ("Created topic" , "topic" , t .Topic )
124+ // Log metadata for debugging/tracking purposes
125+ if metadata != nil && metadata [t .Topic ] != nil {
126+ slog .Info ("Topic metadata" ,
127+ "topic_hash" , t .Topic ,
128+ "apiName" , metadata [t .Topic ]["apiName" ],
129+ "apiVersion" , metadata [t .Topic ]["apiVersion" ],
130+ "channelName" , metadata [t .Topic ]["channelName" ])
115131 }
116- return fmt .Errorf ("failed to create topic %s: %w" , t .Topic , t .Err )
117132 }
118- slog .Info ("Created topic" , "topic" , t .Topic )
119133 }
120134
121135 return nil
0 commit comments