1111// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212// See the License for the specific language governing permissions and
1313// limitations under the License.
14+
15+ // Package stackdriver provides a cloud monitoring sink for applications
16+ // instrumented with the go-metrics library.
1417package stackdriver
1518
1619import (
@@ -30,7 +33,6 @@ import (
3033 metrics "github.com/armon/go-metrics"
3134 googlepb "github.com/golang/protobuf/ptypes/timestamp"
3235 distributionpb "google.golang.org/genproto/googleapis/api/distribution"
33- "google.golang.org/genproto/googleapis/api/metric"
3436 metricpb "google.golang.org/genproto/googleapis/api/metric"
3537 monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
3638 monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
@@ -80,8 +82,8 @@ type Config struct {
8082 // variable parameters within a metric name.
8183 // Optional. Defaults to DefaultLabelExtractor.
8284 LabelExtractor ExtractLabelsFn
83- // Prefix of the metrics recorded. Defaults to "go-metrics/" so your metric "foo" will be recorded as
84- // "custom.googleapis.com/go-metrics/foo".
85+ // Prefix of the metrics recorded. Defaults to "go-metrics/" so a metric
86+ // "foo" will be recorded as " custom.googleapis.com/go-metrics/foo".
8587 Prefix * string
8688 // The bucketer is used to determine histogram bucket boundaries
8789 // for the sampled metrics. This will execute before the LabelExtractor.
@@ -110,19 +112,21 @@ type Config struct {
110112 // Optional. Defaults to a combination of hostname+pid.
111113 TaskID string
112114
113- // Debug logging. Errors will be logged to stderr, but setting this to true
114- // will log additional information that is helpful when debugging errors.
115+ // Debug logging. Errors are always logged to stderr, but setting this to
116+ // true will log additional information that is helpful when debugging
117+ // errors.
115118 // Optional. Defaults to false.
116119 DebugLogs bool
117120
118- // MonitoredResource identifies the machine/service/resource that is monitored.
119- // Different possible settings are defined here:
121+ // MonitoredResource identifies the machine/service/resource that is
122+ // monitored. Different possible settings are defined here:
120123 // https://cloud.google.com/monitoring/api/resources
121124 //
122- // Setting a nil MonitoredResource will run a defaultMonitoredResource function.
125+ // Setting a nil MonitoredResource will run a defaultMonitoredResource
126+ // function.
123127 MonitoredResource * monitoredrespb.MonitoredResource
124128
125- // Logger implements our Logger interface, providing Printf and Println functions
129+ // Logger that can be injected for custom log formatting.
126130 Logger Logger
127131}
128132
@@ -176,7 +180,7 @@ func DefaultLabelExtractor(key []string, kind string) ([]string, []metrics.Label
176180 case "histogram" :
177181 return key , nil , nil
178182 }
179- return nil , nil , fmt .Errorf ("Unknown metric kind: %s" , kind )
183+ return nil , nil , fmt .Errorf ("unknown metric kind: %s" , kind )
180184}
181185
182186// NewSink creates a Sink to flush metrics to stackdriver every interval. The
@@ -354,10 +358,10 @@ func (s *Sink) report(ctx context.Context) {
354358 Type : fmt .Sprintf ("custom.googleapis.com/%s%s" , s .prefix , name ),
355359 Labels : labels ,
356360 },
357- MetricKind : metric .MetricDescriptor_GAUGE ,
361+ MetricKind : metricpb .MetricDescriptor_GAUGE ,
358362 Resource : resource ,
359363 Points : []* monitoringpb.Point {
360- & monitoringpb. Point {
364+ {
361365 Interval : & monitoringpb.TimeInterval {
362366 EndTime : & googlepb.Timestamp {
363367 Seconds : end .Unix (),
@@ -387,10 +391,10 @@ func (s *Sink) report(ctx context.Context) {
387391 Type : fmt .Sprintf ("custom.googleapis.com/%s%s" , s .prefix , name ),
388392 Labels : labels ,
389393 },
390- MetricKind : metric .MetricDescriptor_GAUGE ,
394+ MetricKind : metricpb .MetricDescriptor_GAUGE ,
391395 Resource : resource ,
392396 Points : []* monitoringpb.Point {
393- & monitoringpb. Point {
397+ {
394398 Interval : & monitoringpb.TimeInterval {
395399 EndTime : & googlepb.Timestamp {
396400 Seconds : end .Unix (),
@@ -427,10 +431,10 @@ func (s *Sink) report(ctx context.Context) {
427431 Type : fmt .Sprintf ("custom.googleapis.com/%s%s" , s .prefix , name ),
428432 Labels : labels ,
429433 },
430- MetricKind : metric .MetricDescriptor_CUMULATIVE ,
434+ MetricKind : metricpb .MetricDescriptor_CUMULATIVE ,
431435 Resource : resource ,
432436 Points : []* monitoringpb.Point {
433- & monitoringpb. Point {
437+ {
434438 Interval : & monitoringpb.TimeInterval {
435439 StartTime : & googlepb.Timestamp {
436440 Seconds : s .firstTime .Unix (),
@@ -487,12 +491,12 @@ func (s *Sink) report(ctx context.Context) {
487491 }
488492}
489493
490- // A Gauge should retain the last value it is set to.
494+ // SetGauge retains the last value it is set to.
491495func (s * Sink ) SetGauge (key []string , val float32 ) {
492496 s .SetGaugeWithLabels (key , val , nil )
493497}
494498
495- // A Gauge should retain the last value it is set to.
499+ // SetGaugeWithLabels retains the last value it is set to.
496500func (s * Sink ) SetGaugeWithLabels (key []string , val float32 , labels []metrics.Label ) {
497501 n := newSeries (key , labels )
498502
@@ -507,17 +511,17 @@ func (s *Sink) SetGaugeWithLabels(key []string, val float32, labels []metrics.La
507511 s .gauges [n .hash ] = g
508512}
509513
510- // Should emit a Key/Value pair for each call .
514+ // EmitKey is not implemented .
511515func (s * Sink ) EmitKey (key []string , val float32 ) {
512516 // EmitKey is not implemented for stackdriver
513517}
514518
515- // Counters should accumulate values .
519+ // IncrCounter increments a counter by a value .
516520func (s * Sink ) IncrCounter (key []string , val float32 ) {
517521 s .IncrCounterWithLabels (key , val , nil )
518522}
519523
520- // Counters should accumulate values .
524+ // IncrCounterWithLabels increments a counter by a value .
521525func (s * Sink ) IncrCounterWithLabels (key []string , val float32 , labels []metrics.Label ) {
522526 n := newSeries (key , labels )
523527
@@ -535,12 +539,12 @@ func (s *Sink) IncrCounterWithLabels(key []string, val float32, labels []metrics
535539 }
536540}
537541
538- // Samples are for timing information, where quantiles are used .
542+ // AddSample adds a sample to a histogram metric .
539543func (s * Sink ) AddSample (key []string , val float32 ) {
540544 s .AddSampleWithLabels (key , val , nil )
541545}
542546
543- // Samples are for timing information, where quantiles are used .
547+ // AddSampleWithLabels adds a sample to a histogram metric .
544548func (s * Sink ) AddSampleWithLabels (key []string , val float32 , labels []metrics.Label ) {
545549 n := newSeries (key , labels )
546550
@@ -566,7 +570,7 @@ type series struct {
566570 hash string
567571}
568572
569- var forbiddenChars = regexp .MustCompile (" [ .=\\ -/]" )
573+ var forbiddenChars = regexp .MustCompile (` [ .=\-/]` )
570574
571575func newSeries (key []string , labels []metrics.Label ) * series {
572576 hash := strings .Join (key , "_" )
0 commit comments