forked from dcos/demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
199 lines (185 loc) · 5.28 KB
/
main.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
192
193
194
195
196
197
198
199
// Copyright 2016 Mesosphere. All Rights Reserved.
//
// Licensed 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 main
import (
"flag"
"fmt"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/Shopify/sarama"
log "github.com/Sirupsen/logrus"
"github.com/influxdata/influxdb/client/v2"
)
const (
onversion string = "0.1.0"
prodInfluxAPI string = "http://influxdb.marathon.l4lb.thisdcos.directory:8086"
)
var (
version bool
wg sync.WaitGroup
cities []string
// FQDN/IP + port of a Kafka broker:
broker string
// the URL for the InfluxDB HTTP API:
influxAPI string
// into which InfluxDB database to ingest transactions:
targetdb string
// how many seconds to wait between ingesting transactions:
ingestwaitsec time.Duration
// the ingestion queue
iqueue chan Transaction
)
// Transaction defines a single transaction:
// in which City it originated, the Source
// account the money came from as well as
// the Target account the money goes to and
// last but not least the Amount that was
// transferred in the transaction.
type Transaction struct {
City string
Source string
Target string
Amount int
}
func about() {
fmt.Printf("\nThis is the fintrans InfluxDB ingestion consumer in version %s\n", onversion)
}
func init() {
cities = []string{
"London",
"NYC",
"SF",
"Moscow",
"Tokyo",
}
// the commend line parameters:
flag.BoolVar(&version, "version", false, "Display version information")
flag.StringVar(&broker, "broker", "", "The FQDN or IP address and port of a Kafka broker. Example: broker-1.kafka.mesos:9382 or 10.0.3.178:9398")
flag.Usage = func() {
fmt.Printf("Usage: %s [args]\n\n", os.Args[0])
fmt.Println("Arguments:")
flag.PrintDefaults()
}
flag.Parse()
// the optional environment variables:
influxAPI = prodInfluxAPI
if ia := os.Getenv("INFLUX_API"); ia != "" {
influxAPI = ia
}
targetdb = "fintrans"
if td := os.Getenv("INFLUX_TARGET_DB"); td != "" {
targetdb = td
}
ingestwaitsec = 1
if iw := os.Getenv("INGEST_WAIT_SEC"); iw != "" {
if iwi, err := strconv.Atoi(iw); err == nil {
ingestwaitsec = time.Duration(iwi)
}
}
// creating the buffered channel holding up to 100 transactions:
iqueue = make(chan Transaction, 100)
}
func ingest() {
if c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: influxAPI,
Username: "root",
Password: "root",
}); err != nil {
log.WithFields(log.Fields{"func": "ingest"}).Error(err)
} else {
defer c.Close()
log.WithFields(log.Fields{"func": "ingest"}).Info("Connected to ", fmt.Sprintf("%#v", c))
for {
t := <-iqueue
log.WithFields(log.Fields{"func": "ingest"}).Info(fmt.Sprintf("Dequeued %#v", t))
bp, _ := client.NewBatchPoints(client.BatchPointsConfig{
Database: targetdb,
Precision: "s", // second resultion
})
log.WithFields(log.Fields{"func": "ingest"}).Info(fmt.Sprintf("Preparing batch %#v", bp))
tags := map[string]string{
"source": t.Source,
"target": t.Target,
}
fields := map[string]interface{}{
"amount": t.Amount,
}
pt, _ := client.NewPoint(t.City, tags, fields, time.Now())
bp.AddPoint(pt)
log.WithFields(log.Fields{"func": "ingest"}).Info(fmt.Sprintf("Added point %#v", pt))
if err := c.Write(bp); err != nil {
log.WithFields(log.Fields{"func": "ingest"}).Error("Could not ingest transaction: ", err.Error())
} else {
log.WithFields(log.Fields{"func": "ingest"}).Info(fmt.Sprintf("Ingested %#v", bp))
}
time.Sleep(ingestwaitsec * time.Second)
log.WithFields(log.Fields{"func": "ingest"}).Info(fmt.Sprintf("Current queue length: %d", len(iqueue)))
}
}
}
func consume(topic string) {
var consumer sarama.Consumer
defer wg.Done()
if c, err := sarama.NewConsumer([]string{broker}, nil); err != nil {
log.WithFields(log.Fields{"func": "consume"}).Error(err)
return
} else {
consumer = c
}
defer func() {
if err := consumer.Close(); err != nil {
log.WithFields(log.Fields{"func": "consume"}).Error(err)
}
}()
if partitionConsumer, err := consumer.ConsumePartition(topic, 0, sarama.OffsetNewest); err != nil {
log.WithFields(log.Fields{"func": "consume"}).Error(err)
return
} else {
defer func() {
if err := partitionConsumer.Close(); err != nil {
log.WithFields(log.Fields{"func": "consume"}).Error(err)
}
}()
for {
msg := <-partitionConsumer.Messages()
traw := strings.Split(string(msg.Value), " ")
amount := 0
if a, err := strconv.Atoi(traw[2]); err == nil {
amount = a
}
t := Transaction{City: msg.Topic, Source: traw[0], Target: traw[1], Amount: amount}
iqueue <- t
log.WithFields(log.Fields{"func": "consume"}).Info(fmt.Sprintf("Queued %#v", t))
}
}
}
func main() {
if version {
about()
os.Exit(0)
}
if broker == "" {
flag.Usage()
os.Exit(1)
}
wg.Add(len(cities))
go ingest()
for _, city := range cities {
go consume(city)
}
wg.Wait()
}