Skip to content

Commit

Permalink
Merge pull request #19 from dsrhub/zz/add-datadog-apm
Browse files Browse the repository at this point in the history
Add statsd and apm metrics
  • Loading branch information
zhouzhuojie authored Jul 15, 2020
2 parents 4134f0e + f014e0b commit 1e23eb3
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 112 deletions.
16 changes: 9 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ module github.com/dsrhub/dsrhub
go 1.14

require (
github.com/gin-gonic/gin v1.6.2
github.com/DataDog/datadog-go v3.7.2+incompatible // indirect
github.com/caarlos0/env v3.5.0+incompatible
github.com/gin-gonic/gin v1.6.3
github.com/golang/protobuf v1.4.0
github.com/grpc-ecosystem/grpc-gateway v1.14.6
github.com/kr/text v0.2.0 // indirect
github.com/loopfz/gadgeto v0.10.0
github.com/loopfz/gadgeto v0.10.1
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/onsi/ginkgo v1.11.0 // indirect
github.com/onsi/gomega v1.8.1 // indirect
github.com/ovh/utask v1.6.0
github.com/sirupsen/logrus v1.5.0
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/ovh/utask v1.8.0
github.com/philhofer/fwd v1.0.0 // indirect
github.com/sirupsen/logrus v1.6.0
github.com/stretchr/testify v1.5.1
github.com/wI2L/fizz v0.13.4
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b // indirect
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884
google.golang.org/grpc v1.29.1
gopkg.in/DataDog/dd-trace-go.v1 v1.25.0
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
)
191 changes: 112 additions & 79 deletions go.sum

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions init/dsrhub_init_plugin/dsrhub_init_plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"github.com/caarlos0/env"
"github.com/ovh/utask/pkg/plugins"
)

var Plugin = NewDSRHubInitPlugin() // nolint

type DSRHubInitPlugin struct {
// Environment variables
HTTPCallbackBaseURL string `env:"DSRHUB_HTTP_CALLBACK_BASE_URL" envDefault:"/dsrhub/callback"`
StatsdEnabled bool `env:"STATSD_ENABLED" envDefault:"false"`
StatsdHost string `env:"STATSD_HOST" envDefault:"127.0.0.1"`
StatsdPort string `env:"STATSD_PORT" envDefault:"8125"`
StatsdPrefix string `env:"STATSD_PREFIX" envDefault:"dsrhub."`
StatsdAPMEnabled bool `env:"STATSD_APM_ENABLED" envDefault:"false"`
StatsdAPMPort string `env:"STATSD_APM_PORT" envDefault:"8126"`
StatsdAPMServiceName string `env:"STATSD_APM_SERVICE_NAME" envDefault:"dsrhub"`

// utask init plugin's Service entrypoint
service *plugins.Service
}

func NewDSRHubInitPlugin() plugins.InitializerPlugin {
return &DSRHubInitPlugin{}
}

func (p *DSRHubInitPlugin) Description() string {
return "DSRHubInitPlugin"
}

func (p *DSRHubInitPlugin) Init(service *plugins.Service) error {
if err := env.Parse(p); err != nil {
return err
}

p.service = service

if err := p.setupHTTPCallback(); err != nil {
return err
}
if err := p.setupMetrics(); err != nil {
return err
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,11 @@ import (
"github.com/loopfz/gadgeto/zesty"
"github.com/ovh/utask"
"github.com/ovh/utask/models/resolution"
"github.com/ovh/utask/pkg/plugins"
"github.com/sirupsen/logrus"
"github.com/wI2L/fizz"
)

var Plugin = NewDSRHubCallbackPlugin() // nolint

type DSRHubCallbackPlugin struct{}

func NewDSRHubCallbackPlugin() plugins.InitializerPlugin { return &DSRHubCallbackPlugin{} }
func (p *DSRHubCallbackPlugin) Description() string { return "DSRHub Callback Plugin" }

func (p *DSRHubCallbackPlugin) Init(service *plugins.Service) error {
router, ok := service.Server.Handler(context.Background()).(*fizz.Fizz)
if !ok {
return fmt.Errorf("failed to load router in plugin: %s", p.Description())
}
router.POST("/dsrhub/callback/:resolution_id/:step_name",
[]fizz.OperationOption{fizz.Summary("Handle dsrhub webhook callback.")},
tonic.Handler(p.handleCallbackFunc(), 200))
return nil
}

type inCallback struct {
type httpCallback struct {
// dsrhub related fields
ResolutionID string `path:"resolution_id" validate:"required"`
StepName string `path:"step_name" validate:"required"`
Expand All @@ -48,8 +29,22 @@ type inCallback struct {
IdentityValue string `json:"identity_value"`
}

func (p *DSRHubCallbackPlugin) handleCallbackFunc() func(c *gin.Context, in *inCallback) error {
return func(c *gin.Context, in *inCallback) error {
func (p *DSRHubInitPlugin) setupHTTPCallback() error {
router, ok := p.service.Server.Handler(context.Background()).(*fizz.Fizz)
if !ok {
return fmt.Errorf("failed to load router in plugin: %s", p.Description())
}

router.POST(
fmt.Sprintf("%s/:resolution_id/:step_name", p.HTTPCallbackBaseURL),
[]fizz.OperationOption{fizz.Summary("Handle dsrhub webhook callback.")},
tonic.Handler(p.handleCallbackFunc(), 200),
)
return nil
}

func (p *DSRHubInitPlugin) handleCallbackFunc() func(c *gin.Context, in *httpCallback) error {
return func(c *gin.Context, in *httpCallback) error {
dbp, err := zesty.NewDBProvider(utask.DBName)
if err != nil {
return err
Expand All @@ -76,7 +71,7 @@ func (p *DSRHubCallbackPlugin) handleCallbackFunc() func(c *gin.Context, in *inC
logrus.WithFields(logrus.Fields{
"resolution_id": in.ResolutionID,
"controller_id": in.ControllerID,
}).Debug("update resolution resolver_input from DSRHubCallbackPlugin")
}).Debug("update resolution resolver_input from DSRHubInitPlugin")

if err := r.Update(dbp); err != nil {
dbp.Rollback()
Expand Down
33 changes: 33 additions & 0 deletions init/dsrhub_init_plugin/metric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"context"
"fmt"

"github.com/wI2L/fizz"
gintrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/gin-gonic/gin"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

func (p *DSRHubInitPlugin) setupMetrics() error {
if !p.StatsdEnabled || !p.StatsdAPMEnabled {
return nil
}

router, ok := p.service.Server.Handler(context.Background()).(*fizz.Fizz)
if !ok {
return fmt.Errorf("failed to load router in plugin: %s", p.Description())
}

tracer.Start(
tracer.WithAgentAddr(fmt.Sprintf("%s:%s", p.StatsdHost, p.StatsdAPMPort)),
tracer.WithServiceName(p.StatsdAPMServiceName),
)

ginEngine := router.Engine()
ginEngine.Use(gintrace.Middleware(
fmt.Sprintf("%s-http-server", p.StatsdAPMServiceName),
))

return nil
}
2 changes: 1 addition & 1 deletion mocks/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.14

require (
github.com/checkr/openmock v0.3.2
github.com/dsrhub/dsrhub v0.0.4
github.com/dsrhub/dsrhub v0.0.5
github.com/go-openapi/loads v0.19.5
github.com/golang/protobuf v1.4.2
github.com/jessevdk/go-flags v1.4.0
Expand Down
4 changes: 2 additions & 2 deletions mocks/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw=
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/dsrhub/dsrhub v0.0.4 h1:TJBkUbiUTqEPDusxcv2SMOASZIoni6SuIyMiHDmPmtw=
github.com/dsrhub/dsrhub v0.0.4/go.mod h1:5z/TMxnohVer6ePmQHlEmIBejEBUxnJUt966Pol9bT8=
github.com/dsrhub/dsrhub v0.0.5 h1:pZyowubaL74fHXWHWv4CuUJOfinqzwhvGluET+UawQU=
github.com/dsrhub/dsrhub v0.0.5/go.mod h1:5z/TMxnohVer6ePmQHlEmIBejEBUxnJUt966Pol9bT8=
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21 h1:YEetp8/yCZMuEPMUDHG0CW/brkkEp8mzqk2+ODEitlw=
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
Expand Down

0 comments on commit 1e23eb3

Please sign in to comment.