go.opentelemetry.io/contrib/instrumentation/github.com/Shopify/sarama/otelsarama@v0.43.0/consumer.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 partitionConsumer struct { 22 sarama.PartitionConsumer 23 dispatcher consumerMessagesDispatcher 24 } 25 26 // Messages returns the read channel for the messages that are returned by 27 // the broker. 28 func (pc *partitionConsumer) Messages() <-chan *sarama.ConsumerMessage { 29 return pc.dispatcher.Messages() 30 } 31 32 // WrapPartitionConsumer wraps a sarama.PartitionConsumer causing each received 33 // message to be traced. 34 func WrapPartitionConsumer(pc sarama.PartitionConsumer, opts ...Option) sarama.PartitionConsumer { 35 cfg := newConfig(opts...) 36 37 dispatcher := newConsumerMessagesDispatcherWrapper(pc, cfg) 38 go dispatcher.Run() 39 wrapped := &partitionConsumer{ 40 PartitionConsumer: pc, 41 dispatcher: dispatcher, 42 } 43 return wrapped 44 } 45 46 type consumer struct { 47 sarama.Consumer 48 49 opts []Option 50 } 51 52 // ConsumePartition invokes Consumer.ConsumePartition and wraps the resulting 53 // PartitionConsumer. 54 func (c *consumer) ConsumePartition(topic string, partition int32, offset int64) (sarama.PartitionConsumer, error) { 55 pc, err := c.Consumer.ConsumePartition(topic, partition, offset) 56 if err != nil { 57 return nil, err 58 } 59 return WrapPartitionConsumer(pc, c.opts...), nil 60 } 61 62 // WrapConsumer wraps a sarama.Consumer wrapping any PartitionConsumer created 63 // via Consumer.ConsumePartition. 64 func WrapConsumer(c sarama.Consumer, opts ...Option) sarama.Consumer { 65 return &consumer{ 66 Consumer: c, 67 opts: opts, 68 } 69 }