Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit a6442a4

Browse files
author
Ramon Nogueira
authored
Allow customizing the set of default labels for stats exporting (#20)
* Introduce DefaultMonitoringLabels option * Provide more documentation on how the exporter works with Stackdriver Monitoring concepts (labels, time series, resources). Fixes: #16
1 parent d86a268 commit a6442a4

5 files changed

Lines changed: 459 additions & 125 deletions

File tree

example_test.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@ package stackdriver_test
1717
import (
1818
"log"
1919
"net/http"
20+
"os"
2021

22+
"cloud.google.com/go/compute/metadata"
2123
"contrib.go.opencensus.io/exporter/stackdriver"
2224
"contrib.go.opencensus.io/exporter/stackdriver/propagation"
2325
"go.opencensus.io/plugin/ochttp"
2426
"go.opencensus.io/stats/view"
2527
"go.opencensus.io/trace"
28+
"google.golang.org/genproto/googleapis/api/monitoredres"
2629
)
2730

28-
func Example() {
31+
func Example_defaults() {
2932
exporter, err := stackdriver.NewExporter(stackdriver.Options{ProjectID: "google-project-id"})
3033
if err != nil {
3134
log.Fatal(err)
@@ -56,3 +59,51 @@ func Example() {
5659
// All outgoing requests from client will include a Stackdriver Trace header.
5760
// See the ochttp package for how to handle incoming requests.
5861
}
62+
63+
func Example_gKE() {
64+
65+
// This example shows how to set up a Stackdriver exporter suitable for
66+
// monitoring a GKE container.
67+
68+
instanceID, err := metadata.InstanceID()
69+
if err != nil {
70+
log.Println("Error getting instance ID:", err)
71+
instanceID = "unknown"
72+
}
73+
zone, err := metadata.Zone()
74+
if err != nil {
75+
log.Println("Error getting zone:", err)
76+
zone = "unknown"
77+
}
78+
79+
exporter, err := stackdriver.NewExporter(stackdriver.Options{
80+
ProjectID: "google-project-id",
81+
// Set a MonitoredResource that represents a GKE container.
82+
Resource: &monitoredres.MonitoredResource{
83+
Type: "gke_container",
84+
Labels: map[string]string{
85+
"project_id": "google-project-id",
86+
"cluster_name": "my-cluster-name",
87+
"instance_id": instanceID,
88+
"zone": zone,
89+
90+
// See: https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/
91+
"namespace_id": os.Getenv("MY_POD_NAMESPACE"),
92+
"pod_id": os.Getenv("MY_POD_NAME"),
93+
"container_name": os.Getenv("MY_CONTAINER_NAME"),
94+
},
95+
},
96+
// Set DefaultMonitoringLabels to avoid getting the default "opencensus_task"
97+
// label. For this to be valid, this exporter should be the only writer
98+
// to the metrics against this gke_container MonitoredResource. In this case,
99+
// it means you should only have one process writing to Stackdriver from this
100+
// container.
101+
DefaultMonitoringLabels: &stackdriver.Labels{},
102+
})
103+
if err != nil {
104+
log.Fatal(err)
105+
}
106+
107+
// Register so that views are exported.
108+
view.RegisterExporter(exporter)
109+
}

label.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2018, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package stackdriver
16+
17+
// Labels represents a set of Stackdriver Monitoring labels.
18+
type Labels struct {
19+
m map[string]labelValue
20+
}
21+
22+
type labelValue struct {
23+
val, desc string
24+
}
25+
26+
// Set stores a label with the given key, value and description,
27+
// overwriting any previous values with the given key.
28+
func (labels *Labels) Set(key, value, description string) {
29+
if labels.m == nil {
30+
labels.m = make(map[string]labelValue)
31+
}
32+
labels.m[key] = labelValue{value, description}
33+
}

stackdriver.go

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,37 @@
1515
// Package stackdriver contains the OpenCensus exporters for
1616
// Stackdriver Monitoring and Stackdriver Tracing.
1717
//
18-
// Please note that the Stackdriver exporter is currently experimental.
18+
// This exporter can be used to send metrics to Stackdriver Monitoring and traces
19+
// to Stackdriver trace.
1920
//
20-
// The package uses Application Default Credentials to authenticate. See
21-
// https://developers.google.com/identity/protocols/application-default-credentials
21+
// The package uses Application Default Credentials to authenticate by default.
22+
// See: https://developers.google.com/identity/protocols/application-default-credentials
23+
//
24+
// Alternatively, pass the authentication options in both the MonitoringClientOptions
25+
// and the TraceClientOptions fields of Options.
26+
//
27+
// Stackdriver Monitoring
28+
//
29+
// This exporter support exporting OpenCensus views to Stackdriver Monitoring.
30+
// Each registered view becomes a metric in Stackdriver Monitoring, with the
31+
// tags becoming labels.
32+
//
33+
// The aggregation function determines the metric kind: LastValue aggregations
34+
// generate Gauge metrics and all other aggregations generate Cumulative metrics.
35+
//
36+
// In order to be able to push your stats to Stackdriver Monitoring, you must:
37+
//
38+
// 1. Create a Cloud project: https://support.google.com/cloud/answer/6251787?hl=en
39+
// 2. Enable billing: https://support.google.com/cloud/answer/6288653#new-billing
40+
// 3. Enable the Stackdriver Monitoring API: https://console.cloud.google.com/apis/dashboard
41+
// 4. Make sure you have a Premium Stackdriver account: https://cloud.google.com/monitoring/accounts/tiers
42+
//
43+
// These steps enable the API but don't require that your app is hosted on Google Cloud Platform.
44+
//
45+
// Stackdriver Trace
46+
//
47+
// This exporter supports exporting Trace Spans to Stackdriver Trace. It also
48+
// supports the Google "Cloud Trace" propagation format header.
2249
package stackdriver // import "contrib.go.opencensus.io/exporter/stackdriver"
2350

2451
import (
@@ -71,19 +98,52 @@ type Options struct {
7198
// Optional.
7299
BundleCountThreshold int
73100

74-
// Resource is an optional field that represents the Stackdriver
75-
// MonitoredResource, a resource that can be used for monitoring.
76-
// If no custom ResourceDescriptor is set, a default MonitoredResource
77-
// with type global and no resource labels will be used.
78-
// Optional.
101+
// Resource sets the MonitoredResource against which all views will be
102+
// recorded by this exporter.
103+
//
104+
// All Stackdriver metrics created by this exporter are custom metrics,
105+
// so only a limited number of MonitoredResource types are supported, see:
106+
// https://cloud.google.com/monitoring/custom-metrics/creating-metrics#which-resource
107+
//
108+
// An important consideration when setting the Resource here is that
109+
// Stackdriver Monitoring only allows a single writer per
110+
// TimeSeries, see: https://cloud.google.com/monitoring/api/v3/metrics-details#intro-time-series
111+
// A TimeSeries is uniquely defined by the metric type name
112+
// (constructed from the view name and the MetricPrefix), the Resource field,
113+
// and the set of label key/value pairs (in OpenCensus terminology: tag).
114+
//
115+
// If no custom Resource is set, a default MonitoredResource
116+
// with type global and no resource labels will be used. If you explicitly
117+
// set this field, you may also want to set custom DefaultMonitoringLabels.
118+
//
119+
// Optional, but encouraged.
79120
Resource *monitoredrespb.MonitoredResource
80121

81-
// MetricPrefix overrides the OpenCensus prefix of a stackdriver metric.
82-
// Optional.
122+
// MetricPrefix overrides the prefix of a Stackdriver metric type names.
123+
// Optional. If unset defaults to "OpenCensus".
83124
MetricPrefix string
84125

85-
// DefaultTraceAttributes will be appended to every span that is exported.
126+
// DefaultTraceAttributes will be appended to every span that is exported to
127+
// Stackdriver Trace.
86128
DefaultTraceAttributes map[string]interface{}
129+
130+
// DefaultMonitoringLabels are labels added to every metric created by this
131+
// exporter in Stackdriver Monitoring.
132+
//
133+
// If unset, this defaults to a single label with key "opencensus_task" and
134+
// value "go-<pid>@<hostname>". This default ensures that the set of labels
135+
// together with the default Resource (global) are unique to this
136+
// process, as required by Stackdriver Monitoring.
137+
//
138+
// If you set DefaultMonitoringLabels, make sure that the Resource field
139+
// together with these labels is unique to the
140+
// current process. This is to ensure that there is only a single writer to
141+
// each TimeSeries in Stackdriver.
142+
//
143+
// Set this to &Labels{} (a pointer to an empty Labels) to avoid getting the
144+
// default "opencensus_task" label. You should only do this if you know that
145+
// the Resource you set uniquely identifies this Go process.
146+
DefaultMonitoringLabels *Labels
87147
}
88148

89149
// Exporter is a stats.Exporter and trace.Exporter
@@ -106,7 +166,7 @@ func NewExporter(o Options) (*Exporter, error) {
106166
}
107167
o.ProjectID = creds.ProjectID
108168
}
109-
se, err := newStatsExporter(o)
169+
se, err := newStatsExporter(o, true)
110170
if err != nil {
111171
return nil, err
112172
}

0 commit comments

Comments
 (0)