-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriber.go
183 lines (141 loc) · 4.07 KB
/
subscriber.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package evs
import (
"github.com/apache/pulsar-client-go/pulsar"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"log"
"os"
"time"
)
// Users of the Analytic API implement the Handler interface.
type Handler interface {
Handle(msg pulsar.Message) error
}
// Describes Users of the Analytic API implement the Handler interface.
type Subscriber struct {
// Analytic name
name string
// Handler, interface is invoked when events are received.
handler Handler
// Communication channel used to deliver messages.
ch chan pulsar.ConsumerMessage
// Pulsar client
// FIXME: Should be shared across publisher/subscriber
client pulsar.Client
// Pulsar Consumer
consumer pulsar.Consumer
// Prometheus metrics
request_time *prometheus.SummaryVec
event_size *prometheus.HistogramVec
events *prometheus.CounterVec
// Am I running?
running bool
}
// Initialise the Analytic.
func NewSubscriber(c HasInputTopics, h Handler) (*Subscriber, error) {
name := c.GetName()
s := &Subscriber{name: name}
s.running = true
// Summary metric keeps track of request duration
s.request_time = prometheus.NewSummaryVec(prometheus.SummaryOpts{
Name: "event_processing_time",
Help: "Time spent processing event",
}, []string{"analytic"})
// Histogram metric keeps track of event sizes
s.event_size = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "event_size",
Help: "Size of event message",
Buckets: []float64{25, 50, 100, 250, 500, 1000, 2500, 5000,
10000, 25000, 50000, 100000, 250000, 500000,
1000000, 2500000},
}, []string{"analytic"})
// Counter metric, keeps track of success/failure counts.
s.events = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "event_total",
Help: "Events processed total",
}, []string{"analytic", "state"})
// Register prometheus metrics
prometheus.MustRegister(s.request_time)
prometheus.MustRegister(s.event_size)
prometheus.MustRegister(s.events)
s.handler = h
CheckAndStartMetricsService()
// Get Pulsar broker location
svc_addr, ok := os.LookupEnv("PULSAR_BROKER")
if !ok {
svc_addr = "pulsar://localhost:6650"
}
// Create Pulsar client
var err error
s.client, err = pulsar.NewClient(pulsar.ClientOptions{
URL: svc_addr,
})
if err != nil {
return nil, err
}
// Create Pulsar incoming message queue, 1000 messages backlog
s.ch = make(chan pulsar.ConsumerMessage, 1000)
// Subscriber name is a new UUID
subs := name + "-" + uuid.New().String()
// Consumer options
consumerOpts := pulsar.ConsumerOptions{
Topic: c.GetInputTopic(),
SubscriptionName: subs,
Type: pulsar.Shared,
MessageChannel: s.ch,
}
// Create consumer by subscribing to the topic
s.consumer, err = s.client.Subscribe(consumerOpts)
if err != nil {
return nil, err
}
return s, nil
}
func (s *Subscriber) Close() {
s.consumer.Close()
s.client.Close()
}
// Go into the 'run' state getting messages from the consumer and delivering
// to Handler.
func (s *Subscriber) Run() {
for s.running {
select {
case cm := <-s.ch:
// Message from queue
msg := cm.Message
// Update metric with payload length
lbls := prometheus.Labels{"analytic": s.name}
s.event_size.With(lbls).
Observe(float64(len(cm.Message.Payload())))
// Create a timer to time request duration
lbls = prometheus.Labels{"analytic": s.name}
timer := prometheus.NewTimer(s.request_time.With(lbls))
// Delegate message handling to the Handler interface.
err := s.handler.Handle(msg)
// Update metric with duration
timer.ObserveDuration()
// Record error state
if err == nil {
lbls := prometheus.Labels{
"analytic": s.name,
"state": "success",
}
s.events.With(lbls).Inc()
} else {
lbls := prometheus.Labels{
"analytic": s.name,
"state": "failure",
}
s.events.With(lbls).Inc()
log.Printf("Error: %v\n", err)
}
s.consumer.Ack(msg)
case <-time.After(100 * time.Millisecond):
}
}
// Will return immediately and leave a set of unack'd
// messages in the queue.
}
func (s *Subscriber) Stop() {
s.running = false
}