Skip to content

Commit 96a61ae

Browse files
ajay.kManohar Akula
authored andcommitted
introduce context for event
add newrelic instrumentation
1 parent 1408be6 commit 96a61ae

File tree

13 files changed

+163
-38
lines changed

13 files changed

+163
-38
lines changed

Gopkg.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,7 @@
5252
[[constraint]]
5353
name = "google.golang.org/grpc"
5454
version = "1.8.2"
55+
56+
[[constraint]]
57+
name = "github.com/newrelic/go-agent"
58+
version = "~1.11.0"

Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ setup:
1717
go get github.com/DATA-DOG/godog/cmd/godog
1818
go get -u github.com/go-playground/overalls
1919
go get -u github.com/golang/dep/cmd/dep
20-
dep ensure
20+
dep ensure -v
2121
mkdir -p out/
2222
go build -o $(APP_EXECUTABLE)
23-
cp application.yml.sample application.yml
2423
@echo "consul-envoy-xds is setup!! Run make test to run tests"
2524

25+
copy-config:
26+
cp application.yml.sample application.yml
27+
2628
build-deps:
2729
go install
2830

@@ -50,7 +52,7 @@ lint:
5052
golint $$p | { grep -vwE "exported (var|function|method|type|const) \S+ should have comment" || true; } \
5153
done
5254

53-
test: compile
55+
test: compile copy-config
5456
ENVIRONMENT=test go test $(UNIT_TEST_PACKAGES) -p=1
5557

5658
test-coverage: compile

application.yml.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ CONSUL_CLIENT_HOST: localhost
55
CONSUL_DC: dc1
66
CONSUL_TOKEN: ""
77
WATCHED_SERVICE: foo-service
8+
NEW_RELIC_LICENSE_KEY: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

config/config.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ func (cfg *Config) WatchedServices() []string {
5050
return strings.Split(cfg.GetValue("WATCHED_SERVICE"), ",")
5151
}
5252

53+
func (cfg *Config) AppName() string {
54+
return cfg.GetOptionalValue("APP_NAME", "consul-envoy-xds")
55+
}
56+
57+
func (cfg *Config) NewRelicLicenseKey() string {
58+
return cfg.GetValue("NEW_RELIC_LICENSE_KEY")
59+
}
60+
61+
func (cfg *Config) NewRelicEnabled() bool {
62+
return cfg.GetFeature("NEW_RELIC_ENABLED")
63+
}
64+
5365
func (cfg *Config) WhitelistedRoutes(svc string) []string {
5466
canonicalName := strings.Replace(svc, "-", "_", -1)
5567
whitelist := cfg.GetOptionalValue(strings.ToUpper(canonicalName)+"_WHITELISTED_ROUTES", "/")

eds/endpoint.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package eds
22

33
import (
4+
"context"
45
"fmt"
5-
"time"
6-
7-
"github.com/gojektech/consul-envoy-xds/agent"
8-
"github.com/gojektech/consul-envoy-xds/pubsub"
9-
106
"log"
7+
"time"
118

129
cp "github.com/envoyproxy/go-control-plane/envoy/api/v2"
1310
cpcore "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
1411
eds "github.com/envoyproxy/go-control-plane/envoy/api/v2/endpoint"
1512
"github.com/envoyproxy/go-control-plane/envoy/api/v2/route"
13+
"github.com/gojektech/consul-envoy-xds/agent"
14+
"github.com/gojektech/consul-envoy-xds/eventctx"
15+
"github.com/gojektech/consul-envoy-xds/instrument"
16+
"github.com/gojektech/consul-envoy-xds/pubsub"
1617
"github.com/hashicorp/consul/api"
1718
"github.com/hashicorp/consul/watch"
1819
)
@@ -126,8 +127,11 @@ func (s *service) WatchPlan(publish func(*pubsub.Event)) (*watch.Plan, error) {
126127
return nil, err
127128
}
128129
plan.Handler = func(idx uint64, data interface{}) {
130+
txn := instrument.NewRelicApp().StartTransaction("watch", nil, nil)
131+
defer txn.End()
129132
log.Println(fmt.Sprintf("consul watch triggerred: %v", data))
130-
publish(&pubsub.Event{CLA: s.CLA(), Clusters: s.Clusters(), Routes: s.Routes()})
133+
ctx := eventctx.SetNewRelicTxn(context.Background(), txn)
134+
publish(&pubsub.Event{Context: ctx, CLA: s.CLA(), Clusters: s.Clusters(), Routes: s.Routes()})
131135
}
132136
return plan, nil
133137
}

eventctx/ctx.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package eventctx
2+
3+
import (
4+
"context"
5+
"github.com/newrelic/go-agent"
6+
"net/http"
7+
)
8+
9+
const newRelicTxnKey = "CTX_NEW_RELIC_TXN_KEY"
10+
11+
type noOpTransaction struct {
12+
http.ResponseWriter
13+
}
14+
15+
func (t noOpTransaction) End() error {
16+
return nil
17+
}
18+
19+
func (t noOpTransaction) Ignore() error {
20+
return nil
21+
}
22+
23+
func (t noOpTransaction) SetName(name string) error {
24+
return nil
25+
}
26+
27+
func (t noOpTransaction) NoticeError(err error) error {
28+
return nil
29+
}
30+
31+
func (t noOpTransaction) AddAttribute(key string, value interface{}) error {
32+
return nil
33+
}
34+
35+
func (t noOpTransaction) StartSegmentNow() newrelic.SegmentStartTime {
36+
return newrelic.SegmentStartTime{}
37+
}
38+
39+
func NewRelicTxn(ctx context.Context) newrelic.Transaction {
40+
val := ctx.Value(newRelicTxnKey)
41+
if val == nil {
42+
return noOpTransaction{}
43+
}
44+
return val.(newrelic.Transaction)
45+
}
46+
47+
func SetNewRelicTxn(ctx context.Context, txn newrelic.Transaction) context.Context {
48+
return context.WithValue(ctx, newRelicTxnKey, txn)
49+
}

instrument/newrelic.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package instrument
2+
3+
import (
4+
"log"
5+
6+
"github.com/gojektech/consul-envoy-xds/config"
7+
"github.com/newrelic/go-agent"
8+
)
9+
10+
var newRelicApp newrelic.Application
11+
12+
func init() {
13+
cfg := config.Load()
14+
newRelicCfg := newrelic.NewConfig(cfg.AppName(), cfg.NewRelicLicenseKey())
15+
newRelicCfg.Enabled = cfg.NewRelicEnabled()
16+
var err error
17+
newRelicApp, err = newrelic.NewApplication(newRelicCfg)
18+
if err != nil {
19+
log.Fatal("failed to initialize new relic application", err)
20+
}
21+
}
22+
23+
func NewRelicApp() newrelic.Application {
24+
return newRelicApp
25+
}

pubsub/hub.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"log"
55
"sync"
66

7+
"context"
78
cp "github.com/envoyproxy/go-control-plane/envoy/api/v2"
89
"github.com/satori/go.uuid"
910
)
@@ -21,6 +22,7 @@ type Event struct {
2122
CLA []*cp.ClusterLoadAssignment
2223
Clusters []*cp.Cluster
2324
Routes []*cp.RouteConfiguration
25+
Context context.Context
2426
}
2527

2628
type hub struct {

pubsub/hub_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package pubsub
33
import (
44
"testing"
55

6+
"context"
67
cp "github.com/envoyproxy/go-control-plane/envoy/api/v2"
78
"github.com/stretchr/testify/assert"
89
)
@@ -12,7 +13,7 @@ func TestShouldAddSubscriptionToListOfSubscribers(t *testing.T) {
1213
subscription := hub.Subscribe()
1314
cla := []*cp.ClusterLoadAssignment{}
1415
cluster := &cp.Cluster{}
15-
event := &Event{cla, []*cp.Cluster{cluster}, nil}
16+
event := &Event{Context: context.Background(), CLA: cla, Clusters: []*cp.Cluster{cluster}, Routes: nil}
1617
hub.Publish(event)
1718
a := <-subscription.Events
1819
assert.Equal(t, 1, hub.Size())

0 commit comments

Comments
 (0)