-
Notifications
You must be signed in to change notification settings - Fork 4
/
metrics.go
119 lines (94 loc) · 2.8 KB
/
metrics.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
package porthos
import (
"encoding/json"
"fmt"
"github.com/streadway/amqp"
"log"
"time"
)
const metricsQueueName = "porthos.metrics"
type metricEntry struct {
ServiceName string `json:"serviceName"`
MethodName string `json:"methodName"`
ResponseTime time.Duration `json:"responsetime"`
StatusCode int32 `json:"statusCode"`
}
type metricsCollector struct {
channel *amqp.Channel
index int
buffer []*metricEntry
}
func (mc *metricsCollector) append(entry *metricEntry) {
mc.buffer[mc.index] = entry
mc.index++
}
func (mc *metricsCollector) reset() {
for i := range mc.buffer {
mc.buffer[i] = nil
}
mc.index = 0
}
func (mc *metricsCollector) isFull() bool {
return mc.index == len(mc.buffer)
}
func (mc *metricsCollector) ship() error {
payload, err := json.Marshal(mc.buffer)
if err != nil {
return fmt.Errorf("Error json encoding metrics payload. Error: %s", err)
}
err = mc.channel.Publish(
"",
metricsQueueName,
false,
false,
amqp.Publishing{
ContentType: "application/json",
Body: payload,
})
if err != nil {
return fmt.Errorf("Error publishing metrics to the broker. Error: %s", err)
}
return nil
}
// MetricsShipperConfig defines config params for the NewMetricsShipperExtension.
type MetricsShipperConfig struct {
BufferSize int
}
// MetricsShipperExtension logs incoming requests and outgoing responses.
type MetricsShipperExtension struct {
mc *metricsCollector
}
// ServerListening this is not implemented in this extension.
func (a *MetricsShipperExtension) ServerListening(server Server) error {
return nil
}
// IncomingRequest this is not implemented in this extension.
func (a *MetricsShipperExtension) IncomingRequest(req Request) {}
// OutgoingResponse ships metrics based on responses to the broker.
func (a *MetricsShipperExtension) OutgoingResponse(req Request, res Response, resTime time.Duration, statusCode int32) {
a.mc.append(&metricEntry{req.GetServiceName(), req.GetMethodName(), resTime, statusCode})
if a.mc.isFull() {
a.mc.ship()
a.mc.reset()
}
}
// NewMetricsShipperExtension creates a new extension that logs everything to stdout.
func NewMetricsShipperExtension(b *Broker, config MetricsShipperConfig) (Extension, error) {
ch, err := b.openChannel()
if err != nil {
return nil, fmt.Errorf("Error creating metrics broker channel. Error: %s", err)
}
_, err = ch.QueueDeclare(
metricsQueueName, // name
true, // durable
false, // delete when usused
false, // exclusive
false, // noWait
nil, // arguments
)
log.Printf("[PORTHOS] Metrics shipper extension is waiting for outgoing events...")
return &MetricsShipperExtension{&metricsCollector{
channel: ch,
buffer: make([]*metricEntry, config.BufferSize, config.BufferSize),
}}, nil
}