gcp-monitoring-tally
uber-go/tally reporter for Google Cloud Platform (GCP) Monitoring
Getting Started
Installation
go get -u github.com/chupa-io/gcp-monitoring-tally
Initialization
There are two ways to initialize the stats reporter.
import (
reporter "github.com/chupa-io/gcp-monitoring-tally/reporter"
"github.com/uber-go/tally"
"go.uber.org/fx"
)
...
func NewGCPConfiguration() *reporter.GCPConfiguration {
return &GCPConfiguration{
ProjectID: "your-project-id",
MetricType: "your-desired-metric-type",
// The MetricType should correspond to the MetricDescriptor with MetricKind equal to GAUGE type
// Please see https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors#MetricDescriptor
}
}
var GCPConfigurationModule = fx.Provide(NewGCPConfiguration)
...
type ScopeDeps struct {
fx.In
GCPStatsReporter tally.StatsReporter `name:"gcp_monitoring"` // Make sure to have this tag
Lifecycle fx.Lifecycle // recommended, for adding closing hooks
}
func NewScope(deps ScopeDeps) (tally.Scope, error) {
scope, closer := tally.NewRootScope(tally.ScopeOptions{
Reporter: rep.GCPStatsReporter,
}, 5*time.Second)
deps.Lifecycle.Append(fx.Hook{
OnStop: func(c context.Context) error {
return closer.Close()
},
})
return scope
}
var ScopeModule = fx.Provide(NewScope)
and in your application code, consume tally.Scope
dependency.
type Deps struct {
fx.In
Metrics tally.Scope
}
type Out struct {
// ...
}
func NewModule(deps Deps) Out {
// ...
return Out{/*...*/}
}
Using Regular Constructors
You can simply call the constructors, and pass in the relevant dependency parameters (ignoring fx.In
, which is exclusively used for fx
dependency injection). Refer to the testing code main.go
.
Testing
Export the following environment variables.
export GCP_PROJECT_ID="<your-gcp-project-name>"
export GCP_METRIC_TYPE="<your-desired-metric-type>"
and run
go run main.go