-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from dsrhub/zz/add-datadog-apm
Add statsd and apm metrics
- Loading branch information
Showing
7 changed files
with
223 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters