|
| 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 main |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "flag" |
| 20 | + "log" |
| 21 | + "os" |
| 22 | + "strings" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/Shopify/sarama" |
| 26 | + |
| 27 | + "go.opentelemetry.io/otel/api/global" |
| 28 | + "go.opentelemetry.io/otel/api/propagation" |
| 29 | + "go.opentelemetry.io/otel/api/standard" |
| 30 | + "go.opentelemetry.io/otel/api/trace" |
| 31 | + |
| 32 | + saramatrace "go.opentelemetry.io/contrib/instrumentation/github.com/Shopify/sarama" |
| 33 | + "go.opentelemetry.io/contrib/instrumentation/github.com/Shopify/sarama/example" |
| 34 | +) |
| 35 | + |
| 36 | +var ( |
| 37 | + brokers = flag.String("brokers", os.Getenv("KAFKA_PEERS"), "The Kafka brokers to connect to, as a comma separated list") |
| 38 | +) |
| 39 | + |
| 40 | +func main() { |
| 41 | + example.InitTracer() |
| 42 | + flag.Parse() |
| 43 | + |
| 44 | + if *brokers == "" { |
| 45 | + flag.PrintDefaults() |
| 46 | + os.Exit(1) |
| 47 | + } |
| 48 | + |
| 49 | + brokerList := strings.Split(*brokers, ",") |
| 50 | + log.Printf("Kafka brokers: %s", strings.Join(brokerList, ", ")) |
| 51 | + |
| 52 | + startConsumerGroup(brokerList) |
| 53 | + |
| 54 | + select {} |
| 55 | +} |
| 56 | + |
| 57 | +func startConsumerGroup(brokerList []string) { |
| 58 | + consumerGroupHandler := Consumer{} |
| 59 | + // Wrap instrumentation |
| 60 | + handler := saramatrace.WrapConsumerGroupHandler("example-consumer", &consumerGroupHandler) |
| 61 | + |
| 62 | + config := sarama.NewConfig() |
| 63 | + config.Version = sarama.V2_5_0_0 |
| 64 | + config.Consumer.Offsets.Initial = sarama.OffsetOldest |
| 65 | + |
| 66 | + // Create consumer group |
| 67 | + consumerGroup, err := sarama.NewConsumerGroup(brokerList, "example", config) |
| 68 | + if err != nil { |
| 69 | + log.Fatalln("Failed to start sarama consumer group:", err) |
| 70 | + } |
| 71 | + |
| 72 | + err = consumerGroup.Consume(context.Background(), []string{example.KafkaTopic}, handler) |
| 73 | + if err != nil { |
| 74 | + log.Fatalln("Failed to consume via handler:", err) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func printMessage(msg *sarama.ConsumerMessage) { |
| 79 | + // Extract tracing info from message |
| 80 | + ctx := propagation.ExtractHTTP(context.Background(), global.Propagators(), saramatrace.NewConsumerMessageCarrier(msg)) |
| 81 | + |
| 82 | + tr := global.Tracer("consumer") |
| 83 | + _, span := tr.Start(ctx, "consume message", trace.WithAttributes( |
| 84 | + standard.MessagingOperationProcess, |
| 85 | + )) |
| 86 | + defer span.End() |
| 87 | + |
| 88 | + // Emulate Work loads |
| 89 | + time.Sleep(1 * time.Second) |
| 90 | + |
| 91 | + log.Println("Successful to read message: ", string(msg.Value)) |
| 92 | +} |
| 93 | + |
| 94 | +// Consumer represents a Sarama consumer group consumer |
| 95 | +type Consumer struct { |
| 96 | +} |
| 97 | + |
| 98 | +// Setup is run at the beginning of a new session, before ConsumeClaim |
| 99 | +func (consumer *Consumer) Setup(sarama.ConsumerGroupSession) error { |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +// Cleanup is run at the end of a session, once all ConsumeClaim goroutines have exited |
| 104 | +func (consumer *Consumer) Cleanup(sarama.ConsumerGroupSession) error { |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +// ConsumeClaim must start a consumer loop of ConsumerGroupClaim's Messages(). |
| 109 | +func (consumer *Consumer) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error { |
| 110 | + // NOTE: |
| 111 | + // Do not move the code below to a goroutine. |
| 112 | + // The `ConsumeClaim` itself is called within a goroutine, see: |
| 113 | + // https://github.com/Shopify/sarama/blob/master/consumer_group.go#L27-L29 |
| 114 | + for message := range claim.Messages() { |
| 115 | + printMessage(message) |
| 116 | + session.MarkMessage(message, "") |
| 117 | + } |
| 118 | + |
| 119 | + return nil |
| 120 | +} |
0 commit comments