forked from elodina/go_kafka_client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics_emitters.go
191 lines (164 loc) · 5.6 KB
/
metrics_emitters.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
184
185
186
187
188
189
190
191
/* Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
package go_kafka_client
import (
"encoding/json"
"fmt"
"github.com/elodina/go-avro"
kafkaavro "github.com/elodina/go-kafka-avro"
avroline "github.com/elodina/go_kafka_client/avro"
"github.com/elodina/siesta"
"github.com/elodina/siesta-producer"
"regexp"
"strings"
)
type CodahaleKafkaReporter struct {
topic string
producer producer.Producer
}
func NewCodahaleKafkaReporter(topic string, schemaRegistryUrl string, producerConfig *producer.ProducerConfig, connectorConfig *siesta.ConnectorConfig) (*CodahaleKafkaReporter, error) {
encoder := kafkaavro.NewKafkaAvroEncoder(schemaRegistryUrl)
connector, err := siesta.NewDefaultConnector(connectorConfig)
if err != nil {
return nil, err
}
return &CodahaleKafkaReporter{
topic: topic,
producer: producer.NewKafkaProducer(producerConfig, producer.ByteSerializer, encoder.Encode, connector),
}, nil
}
func (c *CodahaleKafkaReporter) Write(bytes []byte) (n int, err error) {
metrics := make(map[string]interface{})
if err := json.Unmarshal(bytes, &metrics); err != nil {
return -1, err
}
schema, lookupNames := c.parseSchema(metrics)
record := avro.NewGenericRecord(schema)
c.fillRecord(record, schema.(*avro.RecordSchema), metrics, lookupNames)
c.producer.Send(&producer.ProducerRecord{
Topic: c.topic,
Value: record,
})
return 0, nil
}
func (c *CodahaleKafkaReporter) parseSchema(metrics map[string]interface{}) (avro.Schema, map[string]string) {
lookupNames := make(map[string]string)
schema := &avro.RecordSchema{
Name: "Metrics",
Namespace: "net.elodina",
Fields: make([]*avro.SchemaField, 0),
}
for name, v := range metrics {
lookupName := c.getLookupName(name)
lookupNames[lookupName] = name
field := &avro.SchemaField{
Name: lookupName,
Default: nil,
Type: &avro.UnionSchema{
Types: []avro.Schema{&avro.NullSchema{}, c.parseRecordField(name, v.(map[string]interface{}), lookupNames)},
},
}
schema.Fields = append(schema.Fields, field)
}
schema.Fields = append(schema.Fields, &avro.SchemaField{
Name: "logLine",
Type: avroline.NewLogLine().Schema(),
})
return schema, lookupNames
}
func (c *CodahaleKafkaReporter) getLookupName(name string) string {
lookupName := name
pattern := regexp.MustCompile("[0-9]+")
if pattern.MatchString(name) {
lookupName = fmt.Sprintf("metric%s", name)
}
lookupName = strings.Replace(lookupName, ".", "", -1)
lookupName = strings.Replace(lookupName, "%", "", -1)
lookupName = strings.Replace(lookupName, "-", "", -1)
return lookupName
}
func (c *CodahaleKafkaReporter) parseRecordField(name string, v map[string]interface{}, lookupNames map[string]string) *avro.RecordSchema {
lookupRecordName := c.getLookupName(name)
lookupNames[lookupRecordName] = name
record := &avro.RecordSchema{
Name: lookupRecordName,
Namespace: "net.elodina",
Fields: make([]*avro.SchemaField, 0),
}
for name, value := range v {
lookupName := c.getLookupName(name)
lookupNames[lookupName] = name
var valueSchema avro.Schema
switch value.(type) {
case string:
valueSchema = &avro.StringSchema{}
case float64:
valueSchema = &avro.DoubleSchema{}
}
field := &avro.SchemaField{
Name: lookupName,
Default: nil,
Type: &avro.UnionSchema{
Types: []avro.Schema{&avro.NullSchema{}, valueSchema},
},
}
record.Fields = append(record.Fields, field)
}
return record
}
func (c *CodahaleKafkaReporter) fillRecord(record *avro.GenericRecord, schema *avro.RecordSchema, metrics map[string]interface{}, lookupNames map[string]string) {
for _, field := range schema.Fields {
if field.Name == "logLine" {
//TODO probably something more?
logLine := avro.NewGenericRecord(field.Type)
logLine.Set("source", "metrics")
logLine.Set("logtypeid", MetricsLogTypeId)
record.Set("logLine", logLine)
} else {
name := lookupNames[field.Name]
switch fieldSchema := field.Type.(*avro.UnionSchema).Types[1].(type) {
case *avro.RecordSchema:
{
fieldRecord := avro.NewGenericRecord(fieldSchema)
c.fillRecord(fieldRecord, fieldSchema, metrics[name].(map[string]interface{}), lookupNames)
record.Set(field.Name, fieldRecord)
}
default:
{
record.Set(field.Name, metrics[field.Name])
}
}
}
}
}
type KafkaMetricReporter struct {
topic string
producer producer.Producer
}
func NewKafkaMetricReporter(topic string, producerConfig *producer.ProducerConfig, connectorConfig *siesta.ConnectorConfig) (*KafkaMetricReporter, error) {
connector, err := siesta.NewDefaultConnector(connectorConfig)
if err != nil {
return nil, err
}
return &KafkaMetricReporter{
topic: topic,
producer: producer.NewKafkaProducer(producerConfig, producer.ByteSerializer, producer.ByteSerializer, connector),
}, nil
}
func (k *KafkaMetricReporter) Write(bytes []byte) (n int, err error) {
k.producer.Send(&producer.ProducerRecord{
Topic: k.topic,
Value: bytes,
})
return len(bytes), nil
}