go.opentelemetry.io/contrib/instrumentation/github.com/Shopify/sarama/otelsarama@v0.43.0/consumer_group.go (about) 1 // Copyright The OpenTelemetry Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package otelsarama // import "go.opentelemetry.io/contrib/instrumentation/github.com/Shopify/sarama/otelsarama" 16 17 import ( 18 "github.com/Shopify/sarama" 19 ) 20 21 type consumerGroupHandler struct { 22 sarama.ConsumerGroupHandler 23 24 cfg config 25 } 26 27 // ConsumeClaim wraps the session and claim to add instruments for messages. 28 // It implements parts of `ConsumerGroupHandler`. 29 func (h *consumerGroupHandler) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error { 30 // Wrap claim 31 dispatcher := newConsumerMessagesDispatcherWrapper(claim, h.cfg) 32 go dispatcher.Run() 33 claim = &consumerGroupClaim{ 34 ConsumerGroupClaim: claim, 35 dispatcher: dispatcher, 36 } 37 38 return h.ConsumerGroupHandler.ConsumeClaim(session, claim) 39 } 40 41 // WrapConsumerGroupHandler wraps a sarama.ConsumerGroupHandler causing each received 42 // message to be traced. 43 func WrapConsumerGroupHandler(handler sarama.ConsumerGroupHandler, opts ...Option) sarama.ConsumerGroupHandler { 44 cfg := newConfig(opts...) 45 46 return &consumerGroupHandler{ 47 ConsumerGroupHandler: handler, 48 cfg: cfg, 49 } 50 } 51 52 type consumerGroupClaim struct { 53 sarama.ConsumerGroupClaim 54 dispatcher consumerMessagesDispatcher 55 } 56 57 func (c *consumerGroupClaim) Messages() <-chan *sarama.ConsumerMessage { 58 return c.dispatcher.Messages() 59 }