Skip to content

Commit 6fa1ca0

Browse files
authored
feat(emitter): add infra sdk emitter (#67)
* feat(emitter): add infra sdk emitter the sdk emitter will output to stdout in the SDK v4 format that the Infra agent recognizes
1 parent 339c78b commit 6fa1ca0

File tree

23 files changed

+1958
-360
lines changed

23 files changed

+1958
-360
lines changed

.golangci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ misspell:
2424

2525
lll:
2626
line-length: 100
27+
28+
run:
29+
timeout: 2m

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ INTEGRATION := nri-prometheus
66
BINARY_NAME = $(INTEGRATION)
77
IMAGE_NAME ?= newrelic/nri-prometheus
88
GOPATH := $(shell go env GOPATH)
9-
GOLANGCI_LINT_VERSION := v1.29.0
9+
GOLANGCI_LINT_VERSION := v1.30.0
1010
GOLANGCI_LINT_BIN = $(GOPATH)/bin/golangci-lint
1111
GORELEASER_VERSION := v0.138.0
1212
GORELEASER_SHA256 := 60cd594e1413483e5728398f861e34834530e0fb1de842312d62ba9ccd57e5f8

cmd/nri-prometheus/main.go

+7-12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package main
44

55
import (
66
"github.com/newrelic/nri-prometheus/internal/cmd/scraper"
7+
"github.com/newrelic/nri-prometheus/internal/integration"
78
"github.com/sirupsen/logrus"
89
)
910

@@ -14,17 +15,11 @@ func main() {
1415
logrus.WithError(err).Fatal("while loading configuration")
1516
}
1617

17-
if cfg.Standalone {
18-
err = scraper.Run(cfg)
19-
if err != nil {
20-
logrus.WithError(err).Fatal("error occurred while running scraper")
21-
}
22-
} else {
23-
// todo create a proper emitter that add metrics to an integration and prints them to stdout once scraping and processing is performed
24-
cfg.Emitters = []string{"integrationSDK4"}
25-
err = scraper.RunOnce(cfg)
26-
if err != nil {
27-
logrus.WithError(err).Fatal("error occurred while running scraper")
28-
}
18+
logrus.Infof("Starting New Relic's Prometheus OpenMetrics Integration version %s", integration.Version)
19+
logrus.Debugf("Config: %#v", cfg)
20+
21+
err = scraper.Run(cfg)
22+
if err != nil {
23+
logrus.WithError(err).Fatal("error occurred while running scraper")
2924
}
3025
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/googleapis/gnostic v0.2.3-0.20181019180348-e2aafd60c944 // indirect
88
github.com/hashicorp/hcl v1.0.1-0.20190611123218-cf7d376da96d // indirect
99
github.com/imdario/mergo v0.3.8 // indirect
10-
github.com/newrelic/infra-integrations-sdk v3.6.4+incompatible
10+
github.com/newrelic/infra-integrations-sdk v1.0.1-0.20200903141127-7191d0d048a1
1111
github.com/newrelic/newrelic-telemetry-sdk-go v0.4.0
1212
github.com/onsi/ginkgo v1.10.1 // indirect
1313
github.com/onsi/gomega v1.7.0 // indirect

go.sum

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ cloud.google.com/go/firestore v1.1.0/go.mod h1:ulACoGHTpvq5r8rxGJ4ddJZBZqakUQqCl
1111
cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2kNxGRt3I=
1212
cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw=
1313
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
14+
github.com/AlekSi/gocov-xml v0.0.0-20190121064608-3a14fb1c4737/go.mod h1:w1KSuh2JgIL3nyRiZijboSUwbbxOrTzWwyWVFUHtXBQ=
1415
github.com/Azure/go-autorest v11.1.2+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24=
1516
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
1617
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
@@ -24,6 +25,7 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF
2425
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
2526
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
2627
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
28+
github.com/axw/gocov v1.0.0/go.mod h1:LvQpEYiwwIb2nYkXY2fDWhg9/AsYqkhmrCshjlUJECE=
2729
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
2830
github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0=
2931
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
@@ -188,8 +190,8 @@ github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3Rllmb
188190
github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
189191
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
190192
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
191-
github.com/newrelic/infra-integrations-sdk v3.6.4+incompatible h1:pYjT0FCzUpwArkbcaEvt/2R2AoY8WEcbXui9+Aa1Q60=
192-
github.com/newrelic/infra-integrations-sdk v3.6.4+incompatible/go.mod h1:tMUHRMq6mJS0YyBnbWrTXAnREnQqC1AGO6Lu45u5xAM=
193+
github.com/newrelic/infra-integrations-sdk v1.0.1-0.20200903141127-7191d0d048a1 h1:LWCPynX6+O4tAxOAhVkK7vqTVQ5doMiaUA7Q+0N4AkA=
194+
github.com/newrelic/infra-integrations-sdk v1.0.1-0.20200903141127-7191d0d048a1/go.mod h1:pkt52iL985phO3d+kREkalNFJdjYyccUDTD+IK/Pno8=
193195
github.com/newrelic/newrelic-telemetry-sdk-go v0.4.0 h1:x9e37QJxmEX1659zmaeOh9dlUKQTmWHIJVRsFTtfDL4=
194196
github.com/newrelic/newrelic-telemetry-sdk-go v0.4.0/go.mod h1:G9MqE/cHGv3Hx3qpYhfuyFUsGx2DpVcGi1iJIqTg+JQ=
195197
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
@@ -264,6 +266,7 @@ github.com/stretchr/objx v0.1.2-0.20180626195558-9e1dfc121bca/go.mod h1:HFkY916I
264266
github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
265267
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
266268
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
269+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
267270
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
268271
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
269272
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
@@ -372,6 +375,7 @@ golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3
372375
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
373376
golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
374377
golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
378+
golang.org/x/tools v0.0.0-20190617190820-da514acc4774/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
375379
golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
376380
golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
377381
golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
@@ -419,6 +423,7 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkep
419423
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
420424
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
421425
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
426+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
422427
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
423428
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
424429
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

internal/cmd/scraper/scraper.go

+12-23
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ func validateConfig(cfg *Config) error {
101101

102102
// RunWithEmitters runs the scraper with preselected emitters.
103103
func RunWithEmitters(cfg *Config, emitters []integration.Emitter) error {
104-
logrus.Infof("Starting New Relic's Prometheus OpenMetrics Integration version %s", integration.Version)
105-
logrus.Debugf("Config: %#v", cfg)
106104

107105
if len(emitters) == 0 {
108106
return fmt.Errorf("you need to configure at least one valid emitter")
@@ -174,9 +172,6 @@ func RunWithEmitters(cfg *Config, emitters []integration.Emitter) error {
174172

175173
// RunOnceWithEmitters runs the scraper with preselected emitters once.
176174
func RunOnceWithEmitters(cfg *Config, emitters []integration.Emitter) error {
177-
logrus.Infof("Starting New Relic's Prometheus OpenMetrics Integration version %s", integration.Version)
178-
logrus.Debugf("Config: %#v", cfg)
179-
180175
if len(emitters) == 0 {
181176
return fmt.Errorf("you need to configure at least one valid emitter")
182177
}
@@ -221,23 +216,7 @@ func RunOnceWithEmitters(cfg *Config, emitters []integration.Emitter) error {
221216
return nil
222217
}
223218

224-
// RunOnce runs the scraper only once
225-
func RunOnce(cfg *Config) error {
226-
err := validateConfig(cfg)
227-
if err != nil {
228-
return fmt.Errorf("while getting configuration options: %w", err)
229-
}
230-
if cfg.Verbose {
231-
logrus.SetLevel(logrus.DebugLevel)
232-
}
233-
var emitters []integration.Emitter
234-
//todo Implement an actual emitter we are currently ignoring cfg.Emitters
235-
emitters = append(emitters, integration.NewStdoutEmitter())
236-
237-
return RunOnceWithEmitters(cfg, emitters)
238-
}
239-
240-
// Run runs the scraper
219+
// Run runs the scraper. If Standalone=true it keeps running otherwise runs once and exits
241220
func Run(cfg *Config) error {
242221
err := validateConfig(cfg)
243222
if err != nil {
@@ -313,11 +292,21 @@ func Run(cfg *Config) error {
313292
return errors.Wrap(err, "could not create new TelemetryEmitter")
314293
}
315294
emitters = append(emitters, emitter)
295+
case "infra-sdk":
296+
emitter, _ := integration.NewInfraSdkEmitter()
297+
emitters = append(emitters, emitter)
316298
default:
317299
logrus.Debugf("unknown emitter: %s", e)
318300
continue
319301
}
320302
}
321303

322-
return RunWithEmitters(cfg, emitters)
304+
if cfg.Standalone {
305+
logrus.Info("Running in standalone mode...")
306+
err = RunWithEmitters(cfg, emitters)
307+
} else {
308+
logrus.Info("Running in run-once mode...")
309+
err = RunOnceWithEmitters(cfg, emitters)
310+
}
311+
return err
323312
}

internal/cmd/scraper/scraper_test.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,10 @@ func TestRunIntegrationOnce(t *testing.T) {
115115
Verbose: true,
116116
ScrapeDuration: "500ms",
117117
}
118-
err = RunOnce(c)
118+
err = Run(c)
119119
require.NoError(t, err)
120120
require.Equal(t, 2, counter, "the scraper should have hit the mock exactly twice")
121121

122-
//todo once the emitter works properly we should test that the scraped data is the expected one
123122
}
124123

125124
func TestScrapingAnsweringWithError(t *testing.T) {
@@ -144,7 +143,7 @@ func TestScrapingAnsweringWithError(t *testing.T) {
144143
Verbose: true,
145144
ScrapeDuration: "500ms",
146145
}
147-
err := RunOnce(c)
146+
err := Run(c)
148147
// Currently no error is returned in case a scraper does not return any data / err status code
149148
require.NoError(t, err)
150149
require.Equal(t, 2, counter, "the scraper should have hit the mock exactly twice")
@@ -173,7 +172,7 @@ func TestScrapingAnsweringUnexpectedData(t *testing.T) {
173172
Verbose: true,
174173
ScrapeDuration: "500ms",
175174
}
176-
err := RunOnce(c)
175+
err := Run(c)
177176
// Currently no error is returned in case a scraper does not return any data / err status code
178177
require.NoError(t, err)
179178
require.Equal(t, 2, counter, "the scraper should have hit the mock exactly twice")
@@ -194,8 +193,11 @@ func TestScrapingNotAnswering(t *testing.T) {
194193
ScrapeDuration: "500ms",
195194
ScrapeTimeout: time.Duration(500) * time.Millisecond,
196195
}
197-
err := RunOnce(c)
198-
// Currently no error is returned in case a scraper does not return any data / err status code
196+
197+
// when
198+
err := Run(c)
199+
200+
//then
199201
require.NoError(t, err)
200202

201203
}

0 commit comments

Comments
 (0)