Skip to content

Commit 1e23eb3

Browse files
authoredJul 15, 2020
Merge pull request #19 from dsrhub/zz/add-datadog-apm
Add statsd and apm metrics
2 parents 4134f0e + f014e0b commit 1e23eb3

File tree

7 files changed

+223
-112
lines changed

7 files changed

+223
-112
lines changed
 

‎go.mod

+9-7
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ module github.com/dsrhub/dsrhub
33
go 1.14
44

55
require (
6-
github.com/gin-gonic/gin v1.6.2
6+
github.com/DataDog/datadog-go v3.7.2+incompatible // indirect
7+
github.com/caarlos0/env v3.5.0+incompatible
8+
github.com/gin-gonic/gin v1.6.3
79
github.com/golang/protobuf v1.4.0
810
github.com/grpc-ecosystem/grpc-gateway v1.14.6
911
github.com/kr/text v0.2.0 // indirect
10-
github.com/loopfz/gadgeto v0.10.0
12+
github.com/loopfz/gadgeto v0.10.1
1113
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
12-
github.com/onsi/ginkgo v1.11.0 // indirect
13-
github.com/onsi/gomega v1.8.1 // indirect
14-
github.com/ovh/utask v1.6.0
15-
github.com/sirupsen/logrus v1.5.0
14+
github.com/opentracing/opentracing-go v1.2.0 // indirect
15+
github.com/ovh/utask v1.8.0
16+
github.com/philhofer/fwd v1.0.0 // indirect
17+
github.com/sirupsen/logrus v1.6.0
1618
github.com/stretchr/testify v1.5.1
1719
github.com/wI2L/fizz v0.13.4
18-
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b // indirect
1920
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884
2021
google.golang.org/grpc v1.29.1
22+
gopkg.in/DataDog/dd-trace-go.v1 v1.25.0
2123
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
2224
)

‎go.sum

+112-79
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import (
4+
"github.com/caarlos0/env"
5+
"github.com/ovh/utask/pkg/plugins"
6+
)
7+
8+
var Plugin = NewDSRHubInitPlugin() // nolint
9+
10+
type DSRHubInitPlugin struct {
11+
// Environment variables
12+
HTTPCallbackBaseURL string `env:"DSRHUB_HTTP_CALLBACK_BASE_URL" envDefault:"/dsrhub/callback"`
13+
StatsdEnabled bool `env:"STATSD_ENABLED" envDefault:"false"`
14+
StatsdHost string `env:"STATSD_HOST" envDefault:"127.0.0.1"`
15+
StatsdPort string `env:"STATSD_PORT" envDefault:"8125"`
16+
StatsdPrefix string `env:"STATSD_PREFIX" envDefault:"dsrhub."`
17+
StatsdAPMEnabled bool `env:"STATSD_APM_ENABLED" envDefault:"false"`
18+
StatsdAPMPort string `env:"STATSD_APM_PORT" envDefault:"8126"`
19+
StatsdAPMServiceName string `env:"STATSD_APM_SERVICE_NAME" envDefault:"dsrhub"`
20+
21+
// utask init plugin's Service entrypoint
22+
service *plugins.Service
23+
}
24+
25+
func NewDSRHubInitPlugin() plugins.InitializerPlugin {
26+
return &DSRHubInitPlugin{}
27+
}
28+
29+
func (p *DSRHubInitPlugin) Description() string {
30+
return "DSRHubInitPlugin"
31+
}
32+
33+
func (p *DSRHubInitPlugin) Init(service *plugins.Service) error {
34+
if err := env.Parse(p); err != nil {
35+
return err
36+
}
37+
38+
p.service = service
39+
40+
if err := p.setupHTTPCallback(); err != nil {
41+
return err
42+
}
43+
if err := p.setupMetrics(); err != nil {
44+
return err
45+
}
46+
47+
return nil
48+
}

‎init/dsrhub_callback/main.go ‎init/dsrhub_init_plugin/http_callback.go

+18-23
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,11 @@ import (
99
"github.com/loopfz/gadgeto/zesty"
1010
"github.com/ovh/utask"
1111
"github.com/ovh/utask/models/resolution"
12-
"github.com/ovh/utask/pkg/plugins"
1312
"github.com/sirupsen/logrus"
1413
"github.com/wI2L/fizz"
1514
)
1615

17-
var Plugin = NewDSRHubCallbackPlugin() // nolint
18-
19-
type DSRHubCallbackPlugin struct{}
20-
21-
func NewDSRHubCallbackPlugin() plugins.InitializerPlugin { return &DSRHubCallbackPlugin{} }
22-
func (p *DSRHubCallbackPlugin) Description() string { return "DSRHub Callback Plugin" }
23-
24-
func (p *DSRHubCallbackPlugin) Init(service *plugins.Service) error {
25-
router, ok := service.Server.Handler(context.Background()).(*fizz.Fizz)
26-
if !ok {
27-
return fmt.Errorf("failed to load router in plugin: %s", p.Description())
28-
}
29-
router.POST("/dsrhub/callback/:resolution_id/:step_name",
30-
[]fizz.OperationOption{fizz.Summary("Handle dsrhub webhook callback.")},
31-
tonic.Handler(p.handleCallbackFunc(), 200))
32-
return nil
33-
}
34-
35-
type inCallback struct {
16+
type httpCallback struct {
3617
// dsrhub related fields
3718
ResolutionID string `path:"resolution_id" validate:"required"`
3819
StepName string `path:"step_name" validate:"required"`
@@ -48,8 +29,22 @@ type inCallback struct {
4829
IdentityValue string `json:"identity_value"`
4930
}
5031

51-
func (p *DSRHubCallbackPlugin) handleCallbackFunc() func(c *gin.Context, in *inCallback) error {
52-
return func(c *gin.Context, in *inCallback) error {
32+
func (p *DSRHubInitPlugin) setupHTTPCallback() error {
33+
router, ok := p.service.Server.Handler(context.Background()).(*fizz.Fizz)
34+
if !ok {
35+
return fmt.Errorf("failed to load router in plugin: %s", p.Description())
36+
}
37+
38+
router.POST(
39+
fmt.Sprintf("%s/:resolution_id/:step_name", p.HTTPCallbackBaseURL),
40+
[]fizz.OperationOption{fizz.Summary("Handle dsrhub webhook callback.")},
41+
tonic.Handler(p.handleCallbackFunc(), 200),
42+
)
43+
return nil
44+
}
45+
46+
func (p *DSRHubInitPlugin) handleCallbackFunc() func(c *gin.Context, in *httpCallback) error {
47+
return func(c *gin.Context, in *httpCallback) error {
5348
dbp, err := zesty.NewDBProvider(utask.DBName)
5449
if err != nil {
5550
return err
@@ -76,7 +71,7 @@ func (p *DSRHubCallbackPlugin) handleCallbackFunc() func(c *gin.Context, in *inC
7671
logrus.WithFields(logrus.Fields{
7772
"resolution_id": in.ResolutionID,
7873
"controller_id": in.ControllerID,
79-
}).Debug("update resolution resolver_input from DSRHubCallbackPlugin")
74+
}).Debug("update resolution resolver_input from DSRHubInitPlugin")
8075

8176
if err := r.Update(dbp); err != nil {
8277
dbp.Rollback()

‎init/dsrhub_init_plugin/metric.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/wI2L/fizz"
8+
gintrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/gin-gonic/gin"
9+
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
10+
)
11+
12+
func (p *DSRHubInitPlugin) setupMetrics() error {
13+
if !p.StatsdEnabled || !p.StatsdAPMEnabled {
14+
return nil
15+
}
16+
17+
router, ok := p.service.Server.Handler(context.Background()).(*fizz.Fizz)
18+
if !ok {
19+
return fmt.Errorf("failed to load router in plugin: %s", p.Description())
20+
}
21+
22+
tracer.Start(
23+
tracer.WithAgentAddr(fmt.Sprintf("%s:%s", p.StatsdHost, p.StatsdAPMPort)),
24+
tracer.WithServiceName(p.StatsdAPMServiceName),
25+
)
26+
27+
ginEngine := router.Engine()
28+
ginEngine.Use(gintrace.Middleware(
29+
fmt.Sprintf("%s-http-server", p.StatsdAPMServiceName),
30+
))
31+
32+
return nil
33+
}

‎mocks/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.14
44

55
require (
66
github.com/checkr/openmock v0.3.2
7-
github.com/dsrhub/dsrhub v0.0.4
7+
github.com/dsrhub/dsrhub v0.0.5
88
github.com/go-openapi/loads v0.19.5
99
github.com/golang/protobuf v1.4.2
1010
github.com/jessevdk/go-flags v1.4.0

‎mocks/go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8
7676
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
7777
github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw=
7878
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
79-
github.com/dsrhub/dsrhub v0.0.4 h1:TJBkUbiUTqEPDusxcv2SMOASZIoni6SuIyMiHDmPmtw=
80-
github.com/dsrhub/dsrhub v0.0.4/go.mod h1:5z/TMxnohVer6ePmQHlEmIBejEBUxnJUt966Pol9bT8=
79+
github.com/dsrhub/dsrhub v0.0.5 h1:pZyowubaL74fHXWHWv4CuUJOfinqzwhvGluET+UawQU=
80+
github.com/dsrhub/dsrhub v0.0.5/go.mod h1:5z/TMxnohVer6ePmQHlEmIBejEBUxnJUt966Pol9bT8=
8181
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
8282
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21 h1:YEetp8/yCZMuEPMUDHG0CW/brkkEp8mzqk2+ODEitlw=
8383
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=

0 commit comments

Comments
 (0)