|
| 1 | +// Copyright (c) 2018 The Jaeger 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 builder |
| 16 | + |
| 17 | +import ( |
| 18 | + "strings" |
| 19 | + |
| 20 | + "github.com/spf13/viper" |
| 21 | + "github.com/uber/jaeger-lib/metrics" |
| 22 | + "go.uber.org/zap" |
| 23 | + |
| 24 | + "github.com/jaegertracing/jaeger/cmd/ingester/app/consumer" |
| 25 | + "github.com/jaegertracing/jaeger/cmd/ingester/app/processor" |
| 26 | + kafkaConsumer "github.com/jaegertracing/jaeger/pkg/kafka/consumer" |
| 27 | + "github.com/jaegertracing/jaeger/plugin/storage/kafka" |
| 28 | + "github.com/jaegertracing/jaeger/storage/spanstore" |
| 29 | +) |
| 30 | + |
| 31 | +const ( |
| 32 | + // EncodingJSON indicates spans are encoded as a json byte array |
| 33 | + EncodingJSON = "json" |
| 34 | + // EncodingProto indicates spans are encoded as a protobuf byte array |
| 35 | + EncodingProto = "protobuf" |
| 36 | + |
| 37 | + // ConfigPrefix is a prefix fro the ingester flags |
| 38 | + ConfigPrefix = "ingester" |
| 39 | + // SuffixBrokers is a suffix for the brokers flag |
| 40 | + SuffixBrokers = ".brokers" |
| 41 | + // SuffixTopic is a suffix for the topic flag |
| 42 | + SuffixTopic = ".topic" |
| 43 | + // SuffixGroupID is a suffix for the group-id flag |
| 44 | + SuffixGroupID = ".group-id" |
| 45 | + // SuffixParallelism is a suffix for the parallelism flag |
| 46 | + SuffixParallelism = ".parallelism" |
| 47 | + // SuffixEncoding is a suffix for the encoding flag |
| 48 | + SuffixEncoding = ".encoding" |
| 49 | + |
| 50 | + // DefaultBroker is the default kafka broker |
| 51 | + DefaultBroker = "127.0.0.1:9092" |
| 52 | + // DefaultTopic is the default kafka topic |
| 53 | + DefaultTopic = "jaeger-spans" |
| 54 | + // DefaultGroupID is the default consumer Group ID |
| 55 | + DefaultGroupID = "jaeger-ingester" |
| 56 | + // DefaultParallelism is the default parallelism for the span processor |
| 57 | + DefaultParallelism = 1000 |
| 58 | + // DefaultEncoding is the default span encoding |
| 59 | + DefaultEncoding = EncodingJSON |
| 60 | +) |
| 61 | + |
| 62 | +// Builder stores the configuration options for the Ingester |
| 63 | +type Builder struct { |
| 64 | + kafkaConsumer.Configuration |
| 65 | + Parallelism int |
| 66 | + Encoding string |
| 67 | +} |
| 68 | + |
| 69 | +// CreateConsumer creates a new span consumer for the ingester |
| 70 | +func (b *Builder) CreateConsumer(logger *zap.Logger, metricsFactory metrics.Factory, spanWriter spanstore.Writer) (*consumer.Consumer, error) { |
| 71 | + var unmarshaller kafka.Unmarshaller |
| 72 | + if b.Encoding == EncodingJSON { |
| 73 | + unmarshaller = kafka.NewJSONUnmarshaller() |
| 74 | + } else { |
| 75 | + unmarshaller = kafka.NewProtobufUnmarshaller() |
| 76 | + } |
| 77 | + |
| 78 | + spParams := processor.SpanProcessorParams{ |
| 79 | + Writer: spanWriter, |
| 80 | + Unmarshaller: unmarshaller, |
| 81 | + } |
| 82 | + spanProcessor := processor.NewSpanProcessor(spParams) |
| 83 | + |
| 84 | + consumerConfig := kafkaConsumer.Configuration{ |
| 85 | + Brokers: b.Brokers, |
| 86 | + Topic: b.Topic, |
| 87 | + GroupID: b.GroupID, |
| 88 | + } |
| 89 | + saramaConsumer, err := consumerConfig.NewConsumer() |
| 90 | + if err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + |
| 94 | + factoryParams := consumer.ProcessorFactoryParams{ |
| 95 | + Topic: b.Topic, |
| 96 | + Parallelism: b.Parallelism, |
| 97 | + SaramaConsumer: saramaConsumer, |
| 98 | + BaseProcessor: spanProcessor, |
| 99 | + Logger: logger, |
| 100 | + Factory: metricsFactory, |
| 101 | + } |
| 102 | + processorFactory, err := consumer.NewProcessorFactory(factoryParams) |
| 103 | + if err != nil { |
| 104 | + return nil, err |
| 105 | + } |
| 106 | + |
| 107 | + consumerParams := consumer.Params{ |
| 108 | + InternalConsumer: saramaConsumer, |
| 109 | + ProcessorFactory: *processorFactory, |
| 110 | + Factory: metricsFactory, |
| 111 | + Logger: logger, |
| 112 | + } |
| 113 | + return consumer.New(consumerParams) |
| 114 | +} |
| 115 | + |
| 116 | +// InitFromViper initializes Builder with properties from viper |
| 117 | +func (b *Builder) InitFromViper(v *viper.Viper) { |
| 118 | + b.Brokers = strings.Split(v.GetString(ConfigPrefix+SuffixBrokers), ",") |
| 119 | + b.Topic = v.GetString(ConfigPrefix + SuffixTopic) |
| 120 | + b.GroupID = v.GetString(ConfigPrefix + SuffixGroupID) |
| 121 | + b.Parallelism = v.GetInt(ConfigPrefix + SuffixParallelism) |
| 122 | + b.Encoding = v.GetString(ConfigPrefix + SuffixEncoding) |
| 123 | +} |
0 commit comments