-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreplicator.go
More file actions
777 lines (639 loc) · 20.4 KB
/
replicator.go
File metadata and controls
777 lines (639 loc) · 20.4 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
package replicator
import (
"crypto/aes"
"crypto/cipher"
"crypto/tls"
"database/sql"
"encoding/base64"
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"sort"
"strconv"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/armon/go-metrics"
"github.com/aws/aws-sdk-go/service/kms"
"golang.org/x/tools/blog/atom"
)
const (
replicatedCount = "replicated-events"
errorCount = "replication-errors"
)
const (
sqlInsertEvent = `insert into t_aeev_events (aggregate_id, version, typecode, event_time, payload) values(:1,:2,:3,:4,:5)`
sqlInsertPublish = `insert into t_aepb_publish (aggregate_id, version) values(:1,:2)`
sqlLockTable = `lock table t_aerl_replicator_lock in exclusive mode nowait`
)
//Locker defines the locking interface needed for processing feed events.
//The goal is to be able to deploy multiple instances of the feed processor,
//with only a single processor processing a set of events at a time.
type Locker interface {
GetLock(lockerArgs ...interface{}) (bool, error)
ReleaseLock() error
}
//FeedReader defines the interface used to read the feed.
type FeedReader interface {
GetRecent() (*atom.Feed, error)
GetFeed(feedid string) (*atom.Feed, error)
IsEventPresentInFeed(string, int) (bool, error)
}
//Replicator defines the interface replicators implement
type Replicator interface {
ProcessFeed() (bool, error)
}
//ReplicatorFactory defines the interface for instantiating replicators.
type ReplicatorFactory interface {
New(locker Locker, feedReader FeedReader, extras ...interface{}) (Replicator, error)
}
//OraEventStoreReplicator defines a replicator instance that replicates a feed
//to an Oracle event store
type OraEventStoreReplicator struct {
db *sql.DB
locker Locker
feedReader FeedReader
}
func formLastReplicatedEventQuery() string {
return `select aggregate_id, version from t_aeev_events where id = (select max(id) from t_aeev_events)`
}
// If we encounter a non-existent entity as the latest in our feed, we need to remove it
// from the database or we might not be able to make progress in reading later events if we
// keep searching the feed for the entity.
func deleteNonexistentEntity(tx *sql.Tx, aggregate_id string, version int) error {
log.Warnf("Deleting non-existent aggregate %s %d from database", aggregate_id, version)
_, err := tx.Exec(`delete from t_aepb_publish where aggregate_id = :1 and version = :2`, aggregate_id, version)
if err != nil {
return err
}
_, err = tx.Exec(`delete from t_aeev_events where aggregate_id = :1 and version = :2`, aggregate_id, version)
return err
}
func lastReplicatedEvent(feedReader FeedReader, tx *sql.Tx) (string, int, error) {
found := false
for {
//What's the last event seen?
log.Info("Select last event observed in replicated event feed")
var aggregateID string
var version int
start := time.Now()
sql := formLastReplicatedEventQuery()
err := tx.QueryRow(sql).Scan(&aggregateID, &version)
logTimingStats("sqlLastObservedEvent", start, err)
if err != nil {
return "", -1, err
}
//Does it exist?
log.Infof("Check existance of latest aggregate/version is %s - %d in source feed", aggregateID, version)
found, err = feedReader.IsEventPresentInFeed(aggregateID, version)
if err != nil {
return "", -1, err
}
if found == true {
log.Info("Found latest agrgregate/version in source")
return aggregateID, version, nil
}
//If the aggregate does not exist, delete it.
err = deleteNonexistentEntity(tx, aggregateID, version)
if err != nil {
return "", -1, err
}
}
}
//ProcessFeed processes the atom feed based on the current state of
//the replicated events
func (r *OraEventStoreReplicator) ProcessFeed() (bool, error) {
//Do the work in a transaction
log.Info("ProcessFeed start transaction")
tx, err := r.db.Begin()
if err != nil {
incrementCounts(errorCount, 1)
log.Warnf("Error starting transaction: %s", err.Error())
return false, err
}
//Get lock within the context of a transaction
log.Info("Get table lock")
locked, err := r.locker.GetLock(tx)
if err != nil {
incrementCounts(errorCount, 1)
log.Warnf("Error obtaining lock: %s", err.Error())
tx.Rollback()
return false, err
}
if !locked {
log.Info("ProcessFeed did not get lock... returning.")
tx.Rollback()
return false, nil
}
//What's the last event seen?
log.Info("Select last event observed in replicated event feed")
aggregateID, version, err := lastReplicatedEvent(r.feedReader, tx)
if err != nil && err != sql.ErrNoRows {
incrementCounts(errorCount, 1)
log.Warnf("Error querying for last event: %s", err.Error())
tx.Rollback()
return true, err
}
//Find the feed with the event
var feed *atom.Feed
var findFeedErr error
if aggregateID != "" {
feed, findFeedErr = r.findFeedByEvent(aggregateID, version)
} else {
feed, findFeedErr = r.getFirstFeed()
}
if findFeedErr != nil {
incrementCounts(errorCount, 1)
log.Warnf("Unable to retrieve feed data: %s", findFeedErr.Error())
tx.Rollback()
return true, findFeedErr
}
//Add all the events in this feed that have not been added before
if feed != nil {
log.Info("Feed with events to process has been found")
err = r.addFeedEvents(aggregateID, version, feed, tx)
if err != nil {
incrementCounts(errorCount, 1)
log.Warnf("Unable to add feed events: %s", err.Error())
tx.Rollback()
return true, err
}
} else {
log.Info("No events found in feed")
}
//Unlock
err = tx.Commit()
if err != nil {
incrementCounts(errorCount, 1)
log.Warnf("Error commiting work: %s", err.Error())
}
return true, nil
}
func findAggregateIndex(id string, entries []*atom.Entry) int {
for idx, entry := range entries {
if entry.ID == id {
return idx
}
}
return -1
}
//ByTimestamp defines a type for sorting atom entries by their timestamp
type ByTimestamp []*atom.Entry
func (t ByTimestamp) Len() int {
return len(t)
}
func (t ByTimestamp) Swap(i, j int) {
t[i], t[j] = t[j], t[i]
}
func (t ByTimestamp) Less(i, j int) bool {
tsI, err := time.Parse(time.RFC3339Nano, string(t[i].Published))
if err != nil {
return false
}
tsJ, err := time.Parse(time.RFC3339Nano, string(t[j].Published))
if err != nil {
return false
}
return tsJ.After(tsI)
}
func isUniqueConstraintViolation(errText string) bool {
return strings.Contains(errText, "ORA-00001")
}
//addFeedEvents adds events to the replicated feed store based on the offset into the event feed of the
//last observed event. The scope of the events added is a single page, multiple calls of process events
//are needed to get a feed fully synced up
func (r *OraEventStoreReplicator) addFeedEvents(aggregateID string, version int, feed *atom.Feed, tx *sql.Tx) error {
var idx int
//Sort by timestamp
sort.Sort(ByTimestamp(feed.Entry))
//Find offset into the events
if aggregateID == "" {
idx = 0
} else {
id := fmt.Sprintf("urn:esid:%s:%d", aggregateID, version)
idx = findAggregateIndex(id, feed.Entry)
if idx == -1 {
//If not found, it means the previous feed contained the event
//as the last entry. Therefore we add all the events in this feed.
idx = 0
}
}
//Add the events from the feed
for i := idx; i < len(feed.Entry); i++ {
entry := feed.Entry[i]
idParts := strings.SplitN(entry.ID, ":", 4)
payload, err := base64.StdEncoding.DecodeString(entry.Content.Body)
if err != nil {
log.Errorf("Unable to decode payload for entry %-v", entry, "Skip processing of event")
continue
}
ts, err := time.Parse(time.RFC3339Nano, string(entry.Published))
if err != nil {
log.Errorf("Unable to parse timestamp for entry %-v", entry, "Skip processing of event")
continue
}
ver, err := strconv.Atoi(idParts[3])
if err != nil {
log.Errorf("Unable to convert version integer for entry %-v", entry, "Skip processing of event")
continue
}
if idParts[2] == aggregateID && ver == version {
//Already have this aggregate
log.Debugf("Event with aggregrateId %s and version %s not being replicated because it's already replicated.", aggregateID, ver)
continue
}
log.Infof("insert event for %v", entry.ID)
start := time.Now()
_, err = tx.Exec(sqlInsertEvent,
idParts[2], idParts[3], entry.Content.Type, ts, payload)
logTimingStats("sqlInsertEvent", start, err)
if err != nil {
log.Warnf("Replication insert failed: %s", err.Error())
if isUniqueConstraintViolation(err.Error()) {
log.Warnf("Unique constraint violation - Skip processing of event")
continue
}
return err
}
start = time.Now()
_, err = tx.Exec(sqlInsertPublish,
idParts[2], idParts[3])
logTimingStats("sqlInsertPublish", start, err)
if err != nil {
log.Warnf("Replication publish insert failed: %s", err.Error())
if isUniqueConstraintViolation(err.Error()) {
log.Warnf("Unique constraint violation - Skip duplicate publishing of event")
continue
}
return err
}
}
incrementCounts(replicatedCount, len(feed.Entry))
return nil
}
//findFeedByEvent finds the feed containing the given event, or, if the event is the last event in a feed, the
//next feed.
func (r *OraEventStoreReplicator) findFeedByEvent(aggregateID string, version int) (*atom.Feed, error) {
log.Infof("findFeedByEvent - %s %d", aggregateID, version)
var feedReadError error
var feed *atom.Feed
var found bool
//We need to find the feed containing a specific aggregate. If there are events 'later' in the
//feed, we return that feed, otherwise we return the 'next' feed (unless we are the recent feed
id := fmt.Sprintf("urn:esid:%s:%d", aggregateID, version)
//Is the event in the recent feed?
feed, err := r.feedReader.GetRecent()
if err != nil {
return nil, err
}
idx := findAggregateIndex(id, feed.Entry)
found = (idx != -1)
log.Infof("...event not found in recent feed, look in previous feeds")
if !found {
for {
prev := getLink("prev-archive", feed)
if prev == nil {
log.Info("...feed history exhausted")
break
}
//Extract feed id from prev
feedID := feedIdFromResource(*prev)
log.Infof("Prev archive feed id is %s", feedID)
feed, feedReadError = r.feedReader.GetFeed(feedID)
if feedReadError != nil {
return nil, feedReadError
}
idx = findAggregateIndex(id, feed.Entry)
if idx != -1 {
found = true
break
}
}
}
if found {
log.Infof("Event found in feed %s", feed.ID)
sort.Sort(ByTimestamp(feed.Entry))
idx := findAggregateIndex(id, feed.Entry)
log.Infof("Index in sorted feed: %d", idx)
if idx == len(feed.Entry)-1 {
log.Info("Return next feed as current aggregate is last entry in feed")
next := getLink("next-archive", feed)
if next == nil {
return nil, nil //We're at the end of recent
}
feedID := feedIdFromResource(*next)
feed, feedReadError = r.feedReader.GetFeed(feedID)
if feedReadError != nil {
return nil, feedReadError
}
}
}
log.Infof("returning feed %s", feed.ID)
return feed, nil
}
//Get link extracts the given link relationship from the given feed's
//link collection
func getLink(linkRelationship string, feed *atom.Feed) *string {
if feed == nil {
return nil
}
for _, l := range feed.Link {
if l.Rel == linkRelationship {
return &l.Href
}
}
return nil
}
//Grab the feed id as the component of a uri
func feedIdFromResource(feedURL string) string {
url, _ := url.Parse(feedURL)
parts := strings.Split(url.RequestURI(), "/")
return parts[len(parts)-1]
}
//Get first feed navigates a feed set from the recent feed all the way back
//to the first acchived feed
func (r *OraEventStoreReplicator) getFirstFeed() (*atom.Feed, error) {
log.Info("Looking for first feed")
//Start with recent
var feed *atom.Feed
var feedReadError error
feed, feedReadError = r.feedReader.GetRecent()
if feedReadError != nil {
return nil, feedReadError
}
if feed == nil {
//Nothing in the feed if there's no recent available...
log.Info("Nothing in the feed")
return nil, nil
}
log.Info("Got feed - navigate prev-archive link")
for {
prev := getLink("prev-archive", feed)
if prev == nil {
break
}
//Extract feed id from prev
feedID := feedIdFromResource(*prev)
log.Infof("Prev archive feed id is %s", feedID)
feed, feedReadError = r.feedReader.GetFeed(feedID)
if feedReadError != nil {
return nil, feedReadError
}
}
return feed, nil
}
//OraEventStoreReplicatorFactory defines a type implementing the ReplicatorFactory interface.
type OraEventStoreReplicatorFactory struct{}
//New instantiates an OraEventStoreReplicator
func (oesFact *OraEventStoreReplicatorFactory) New(locker Locker, feedReader FeedReader, db *sql.DB) (Replicator, error) {
return &OraEventStoreReplicator{
db: db,
locker: locker,
feedReader: feedReader,
}, nil
}
//TableLocker defines a type for implmenting the Locker interface based on table locking
type TableLocker struct{}
var errGetLockArgument = errors.New("Table locker GetLock expects a single argument of type *sql.Tx")
//GetLock obtains a table lock. If the lock cannot be obtained it returns immediately (it
//does not block)
func (tl *TableLocker) GetLock(args ...interface{}) (bool, error) {
if len(args) != 1 {
return false, errGetLockArgument
}
tx, ok := args[0].(*sql.Tx)
if !ok {
return false, errGetLockArgument
}
log.Info("locking table replicator_lock")
start := time.Now()
_, err := tx.Exec(sqlLockTable)
logTimingStats("sqlLockTable", start, err)
if err == nil {
log.Info("Acquired lock")
logTimingStats("sqlLockTable", start, nil)
return true, nil
}
if strings.Contains(err.Error(), "ORA-00054") {
log.Info("Did not acquire lock")
logTimingStats("sqlLockTable", start, nil)
return false, nil
} else {
log.Warnf("Error locking table: %s", err.Error())
logTimingStats("sqlLockTable", start, err)
return false, err
}
}
//ReleaseLock - the enclosing transaction for the table locker
//releases the table lock on commit or rollback
func (tl *TableLocker) ReleaseLock() error {
return nil //Release done in transaction commit/rollback
}
//HttpFeedReader defines a type for an Http Feed reader
type HttpFeedReader struct {
endpoint string
client *http.Client
proto string
keyAlias string
kmsSvc *kms.KMS
}
//NewHttpFeedReader is a factory for instantiating HttpFeedReaders
func NewHttpFeedReader(endpoint, feedProto, keyAlias string, kmsSvc *kms.KMS) *HttpFeedReader {
client := http.DefaultClient
if feedProto == "https" {
tr := http.DefaultTransport
defTransAsTransPort := tr.(*http.Transport)
defTransAsTransPort.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
client = &http.Client{Transport: tr}
}
return &HttpFeedReader{
endpoint: endpoint,
client: client,
proto: feedProto,
keyAlias: keyAlias,
kmsSvc: kmsSvc,
}
}
//GetRecent returns the recent notifications
func (hr *HttpFeedReader) GetRecent() (*atom.Feed, error) {
url := fmt.Sprintf("%s://%s/notifications/recent", hr.proto, hr.endpoint)
return hr.getResource(url)
}
//GetFeed returns the specific feed
func (hr *HttpFeedReader) GetFeed(feedid string) (*atom.Feed, error) {
url := fmt.Sprintf("%s://%s/notifications/%s", hr.proto, hr.endpoint, feedid)
return hr.getResource(url)
}
func (hr *HttpFeedReader) IsEventPresentInFeed(aggregateID string, version int) (bool, error) {
url := fmt.Sprintf("%s://%s/events", hr.proto, hr.endpoint)
return hr.isPresentInSource(url, aggregateID, version)
}
//IsFeedEncrypted indicates if we use a key alias for decrypting the feed
func (hr *HttpFeedReader) IsFeedEncrypted() bool {
return hr.keyAlias != ""
}
//Decrypt from cryptopasta commit bc3a108a5776376aa811eea34b93383837994340
//used via the CC0 license. See https://github.com/gtank/cryptopasta
func (hr *HttpFeedReader) decrypt(ciphertext []byte, key *[32]byte) (plaintext []byte, err error) {
block, err := aes.NewCipher(key[:])
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
if len(ciphertext) < gcm.NonceSize() {
return nil, errors.New("malformed ciphertext")
}
return gcm.Open(nil,
ciphertext[:gcm.NonceSize()],
ciphertext[gcm.NonceSize():],
nil,
)
}
//DecryptFeed uses the AWS KMS to decrypt the feed text.
func (hr *HttpFeedReader) DecryptFeed(feedBytes []byte) ([]byte, error) {
//Message is encrypted encryption key + :: + encrypted message
parts := strings.Split(string(feedBytes), "::")
if len(parts) != 2 {
err := errors.New(fmt.Sprintf("Expected two parts, got %d", len(parts)))
return nil, err
}
//Decode the key and the text
keyBytes, err := base64.StdEncoding.DecodeString(parts[0])
if err != nil {
return nil, err
}
//Get the encrypted bytes
msgBytes, err := base64.StdEncoding.DecodeString(parts[1])
if err != nil {
return nil, err
}
//Decrypt the encryption key
di := &kms.DecryptInput{
CiphertextBlob: keyBytes,
}
decryptedKey, err := hr.kmsSvc.Decrypt(di)
if err != nil {
return nil, err
}
//Use the decrypted key to decrypt the message text
decryptKey := [32]byte{}
copy(decryptKey[:], decryptedKey.Plaintext[0:32])
return hr.decrypt(msgBytes, &decryptKey)
}
func (hr *HttpFeedReader) isPresentInSource(url, aggregateID string, version int) (bool, error) {
var start time.Time
resource := fmt.Sprintf("%s/%s/%d", url, aggregateID, version)
log.Info("check aggregate via ", resource)
req, err := http.NewRequest("GET", resource, nil)
if err != nil {
return false, err
}
start = time.Now()
resp, err := hr.client.Do(req)
logTimingStats("isPresentInSource client.Do", start, err)
if err != nil {
return false, err
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK:
return true, nil
case http.StatusNotFound:
return false, nil
default:
return false, errors.New(fmt.Sprintf("Unexpected status code verifying event in source: %d", resp.StatusCode))
}
}
//getResource does a git on the specified feed resource
func (hr *HttpFeedReader) getResource(url string) (*atom.Feed, error) {
var start time.Time
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
start = time.Now()
resp, err := hr.client.Do(req)
logTimingStats("getResource client.Do", start, err)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
responseBytes, readErr := ioutil.ReadAll(resp.Body)
if readErr == nil {
log.Warnf("Error reading feed: %d %s", resp.StatusCode, string([]byte(responseBytes)))
}
return nil, errors.New(fmt.Sprintf("Error retrieving resource: %d", resp.StatusCode))
}
start = time.Now()
responseBytes, err := ioutil.ReadAll(resp.Body)
logTimingStats("getResource response ReadAll", start, err)
if err != nil {
return nil, err
}
//Are we using a key to decrypt the feed?
if hr.IsFeedEncrypted() {
responseBytes, err = hr.DecryptFeed(responseBytes)
if err != nil {
return nil, err
}
}
var feed atom.Feed
start = time.Now()
err = xml.Unmarshal(responseBytes, &feed)
logTimingStats("getResource xml.Unmarshall", start, err)
if err != nil {
return nil, err
}
return &feed, nil
}
//Configure where telemery data goes. Currently this can be send via UDP to a listener, or can be buffered
//internally and dumped via a signal.
func ConfigureStatsD() {
statsdEndpoint := os.Getenv("STATSD_ENDPOINT")
log.Infof("STATSD_ENDPOINT: %s", statsdEndpoint)
if statsdEndpoint != "" {
log.Info("Using vanilla statsd client to send telemetry to ", statsdEndpoint)
sink, err := metrics.NewStatsdSink(statsdEndpoint)
if err != nil {
log.Warn("Unable to configure statds sink", err.Error())
return
}
metrics.NewGlobal(metrics.DefaultConfig(statsdEndpoint), sink)
} else {
log.Info("Using in memory metrics accumulator - dump via USR1 signal")
inm := metrics.NewInmemSink(10*time.Second, 5*time.Minute)
metrics.DefaultInmemSignal(inm)
metrics.NewGlobal(metrics.DefaultConfig("xavi"), inm)
}
}
//Update counters and stats for timings, discriminating errors from non-errors
func logTimingStats(svc string, start time.Time, err error) {
duration := time.Now().Sub(start)
go func(svc string, duration time.Duration, err error) {
ms := float32(duration.Nanoseconds()) / 1000.0 / 1000.0
if err != nil {
key := []string{"es-atom-replicator", fmt.Sprintf("%s-error", svc)}
metrics.AddSample(key, float32(ms))
metrics.IncrCounter(key, 1)
} else {
key := []string{"es-atom-replicator", svc}
metrics.AddSample(key, float32(ms))
metrics.IncrCounter(key, 1)
}
}(svc, duration, err)
}
//Counters
func incrementCounts(counter string, increment int) {
go func(counter string, increment int) {
key := []string{"es-atom-replicator", counter}
metrics.IncrCounter(key, float32(increment))
}(counter, increment)
}