Skip to content

Commit

Permalink
telemetry: add stackdriver metrics sink
Browse files Browse the repository at this point in the history
  • Loading branch information
tam7t committed Aug 20, 2019
1 parent 296b9c6 commit d1f25c5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
18 changes: 18 additions & 0 deletions command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import (

"github.com/hashicorp/vault/helper/metricsutil"

monitoring "cloud.google.com/go/monitoring/apiv3"
metrics "github.com/armon/go-metrics"
"github.com/armon/go-metrics/circonus"
"github.com/armon/go-metrics/datadog"
"github.com/armon/go-metrics/prometheus"
stackdriver "github.com/google/go-metrics-stackdriver"
"github.com/hashicorp/errwrap"
log "github.com/hashicorp/go-hclog"
multierror "github.com/hashicorp/go-multierror"
Expand All @@ -42,6 +44,7 @@ import (
"github.com/hashicorp/vault/sdk/helper/logging"
"github.com/hashicorp/vault/sdk/helper/mlock"
"github.com/hashicorp/vault/sdk/helper/parseutil"
"github.com/hashicorp/vault/sdk/helper/useragent"
"github.com/hashicorp/vault/sdk/logical"
"github.com/hashicorp/vault/sdk/physical"
"github.com/hashicorp/vault/sdk/version"
Expand All @@ -51,6 +54,7 @@ import (
"github.com/mitchellh/cli"
testing "github.com/mitchellh/go-testing-interface"
"github.com/posener/complete"
"google.golang.org/api/option"
"google.golang.org/grpc/grpclog"
)

Expand Down Expand Up @@ -1969,6 +1973,20 @@ func (c *ServerCommand) setupTelemetry(config *server.Config) (*metricsutil.Metr
fanout = append(fanout, sink)
}

// Configure the stackdriver sink
if telConfig.StackdriverProjectID != "" {
client, err := monitoring.NewMetricClient(context.Background(), option.WithUserAgent(useragent.String()))
if err != nil {
return nil, fmt.Errorf("Failed to create stackdriver client: %v", err)
}
sink := stackdriver.NewSink(client, &stackdriver.Config{
ProjectID: telConfig.StackdriverProjectID,
Location: telConfig.StackdriverLocation,
Namespace: telConfig.StackdriverNamespace,
})
fanout = append(fanout, sink)
}

// Initialize the global sink
if len(fanout) > 1 {
// Hostname enabled will create poor quality metrics name for prometheus
Expand Down
8 changes: 8 additions & 0 deletions command/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,14 @@ type Telemetry struct {
// Default: 24h
PrometheusRetentionTime time.Duration `hcl:"-"`
PrometheusRetentionTimeRaw interface{} `hcl:"prometheus_retention_time"`

// Stackdriver:
// StackdriverProjectID is the project to publish stackdriver metrics to.
StackdriverProjectID string `hcl:"stackdriver_project_id"`
// StackdriverLocation is the GCP or AWS region of the monitored resource.
StackdriverLocation string `hcl:"stackdriver_location"`
// StackdriverNamespace is the namespace identifier, such as a cluster name.
StackdriverNamespace string `hcl:"stackdriver_namespace"`
}

func (s *Telemetry) GoString() string {
Expand Down
39 changes: 39 additions & 0 deletions website/source/docs/configuration/telemetry.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,42 @@ telemetry {
disable_hostname = true
}
```

### `stackdriver`

These `telemetry` parameters apply to [Stackdriver Monitoring](https://cloud.google.com/monitoring/).

The Stackdriver telemetry provider uses the official Google Cloud Golang SDK. This means
it supports the common ways of
[providing credentials to Google Cloud](https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application).

To use this telemetry provider, the service account must have the following
minimum scope(s):

```text
https://www.googleapis.com/auth/cloud-platform
https://www.googleapis.com/auth/monitoring
https://www.googleapis.com/auth/monitoring.write
```

And the following IAM role(s):

```text
roles/monitoring.metricWriter
```

* `stackdriver_project_id` the Google Cloud ProjectID to send telemetry data to.
* `stackdriver_location` the GCP or AWS region of the monitored resource.
* `stackdriver_namespace` a namespace identifier for the telemetry data.

It is recommended to also enable the option `disable_hostname` to avoid having prefixed
metrics with hostname.

```hcl
telemetry {
stackdriver_project_id = "my-test-project"
stackdriver_location = "us-east1-a"
stackdriver_namespace = "vault-cluster-a"
disable_hostname = true
}
```

0 comments on commit d1f25c5

Please sign in to comment.