forked from grafana/loki
/
target.go
147 lines (122 loc) · 3.44 KB
/
target.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package kafka
import (
"fmt"
"time"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/relabel"
"github.com/Shopify/sarama"
"github.com/prometheus/common/model"
"github.com/grafana/loki/clients/pkg/promtail/api"
"github.com/grafana/loki/clients/pkg/promtail/targets/target"
"github.com/grafana/loki/pkg/logproto"
)
type runnableDroppedTarget struct {
target.Target
runFn func()
}
func (d *runnableDroppedTarget) run() {
d.runFn()
}
type Target struct {
discoveredLabels model.LabelSet
lbs model.LabelSet
details ConsumerDetails
claim sarama.ConsumerGroupClaim
session sarama.ConsumerGroupSession
client api.EntryHandler
relabelConfig []*relabel.Config
useIncomingTimestamp bool
}
func NewTarget(
session sarama.ConsumerGroupSession,
claim sarama.ConsumerGroupClaim,
discoveredLabels, lbs model.LabelSet,
relabelConfig []*relabel.Config,
client api.EntryHandler,
useIncomingTimestamp bool,
) *Target {
return &Target{
discoveredLabels: discoveredLabels,
lbs: lbs,
details: newDetails(session, claim),
claim: claim,
session: session,
client: client,
relabelConfig: relabelConfig,
useIncomingTimestamp: useIncomingTimestamp,
}
}
const (
defaultKafkaMessageKey = "none"
labelKeyKafkaMessageKey = "__meta_kafka_message_key"
)
func (t *Target) run() {
defer t.client.Stop()
for message := range t.claim.Messages() {
mk := string(message.Key)
if len(mk) == 0 {
mk = defaultKafkaMessageKey
}
// TODO: Possibly need to format after merging with discovered labels because we can specify multiple labels in source labels
// https://github.com/grafana/loki/pull/4745#discussion_r750022234
lbs := format([]labels.Label{{
Name: labelKeyKafkaMessageKey,
Value: mk,
}}, t.relabelConfig)
out := t.lbs.Clone()
if len(lbs) > 0 {
out = out.Merge(lbs)
}
t.client.Chan() <- api.Entry{
Entry: logproto.Entry{
Line: string(message.Value),
Timestamp: timestamp(t.useIncomingTimestamp, message.Timestamp),
},
Labels: out,
}
t.session.MarkMessage(message, "")
}
}
func timestamp(useIncoming bool, incoming time.Time) time.Time {
if useIncoming {
return incoming
}
return time.Now()
}
func (t *Target) Type() target.TargetType {
return target.KafkaTargetType
}
func (t *Target) Ready() bool {
return true
}
func (t *Target) DiscoveredLabels() model.LabelSet {
return t.discoveredLabels
}
func (t *Target) Labels() model.LabelSet {
return t.lbs
}
// Details returns target-specific details.
func (t *Target) Details() interface{} {
return t.details
}
type ConsumerDetails struct {
// MemberID returns the cluster member ID.
MemberID string
// GenerationID returns the current generation ID.
GenerationID int32
Topic string
Partition int32
InitialOffset int64
}
func (c ConsumerDetails) String() string {
return fmt.Sprintf("member_id=%s generation_id=%d topic=%s partition=%d initial_offset=%d", c.MemberID, c.GenerationID, c.Topic, c.Partition, c.InitialOffset)
}
func newDetails(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) ConsumerDetails {
return ConsumerDetails{
MemberID: session.MemberID(),
GenerationID: session.GenerationID(),
Topic: claim.Topic(),
Partition: claim.Partition(),
InitialOffset: claim.InitialOffset(),
}
}