|
| 1 | +// Copyright 2021 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// Package metrics provides a service for reporting metrics to |
| 6 | +// Stackdriver, or locally during development. |
| 7 | +package metrics |
| 8 | + |
| 9 | +import ( |
| 10 | + "errors" |
| 11 | + "fmt" |
| 12 | + "net/http" |
| 13 | + "path" |
| 14 | + "time" |
| 15 | + |
| 16 | + "cloud.google.com/go/compute/metadata" |
| 17 | + "contrib.go.opencensus.io/exporter/prometheus" |
| 18 | + "contrib.go.opencensus.io/exporter/stackdriver" |
| 19 | + "go.opencensus.io/stats/view" |
| 20 | + mrpb "google.golang.org/genproto/googleapis/api/monitoredres" |
| 21 | +) |
| 22 | + |
| 23 | +// NewService initializes a *Service. |
| 24 | +// |
| 25 | +// The Service returned is configured to send metric data to |
| 26 | +// StackDriver. When not running on GCE, it will host metrics through |
| 27 | +// a prometheus HTTP handler. |
| 28 | +// |
| 29 | +// views will be passed to view.Register for export to the metric |
| 30 | +// service. |
| 31 | +func NewService(resource *MonitoredResource, views []*view.View) (*Service, error) { |
| 32 | + err := view.Register(views...) |
| 33 | + if err != nil { |
| 34 | + return nil, err |
| 35 | + } |
| 36 | + |
| 37 | + if !metadata.OnGCE() { |
| 38 | + view.SetReportingPeriod(5 * time.Second) |
| 39 | + pe, err := prometheus.NewExporter(prometheus.Options{}) |
| 40 | + if err != nil { |
| 41 | + return nil, fmt.Errorf("prometheus.NewExporter: %w", err) |
| 42 | + } |
| 43 | + view.RegisterExporter(pe) |
| 44 | + return &Service{pExporter: pe}, nil |
| 45 | + } |
| 46 | + |
| 47 | + projID, err := metadata.ProjectID() |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + if resource == nil { |
| 52 | + return nil, errors.New("resource is required, got nil") |
| 53 | + } |
| 54 | + sde, err := stackdriver.NewExporter(stackdriver.Options{ |
| 55 | + ProjectID: projID, |
| 56 | + MonitoredResource: resource, |
| 57 | + ReportingInterval: time.Minute, // Minimum interval for Stackdriver is 1 minute. |
| 58 | + }) |
| 59 | + if err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + |
| 63 | + // Minimum interval for Stackdriver is 1 minute. |
| 64 | + view.SetReportingPeriod(time.Minute) |
| 65 | + // Start the metrics exporter. |
| 66 | + if err := sde.StartMetricsExporter(); err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + |
| 70 | + return &Service{sdExporter: sde}, nil |
| 71 | +} |
| 72 | + |
| 73 | +// Service controls metric exporters. |
| 74 | +type Service struct { |
| 75 | + sdExporter *stackdriver.Exporter |
| 76 | + pExporter *prometheus.Exporter |
| 77 | +} |
| 78 | + |
| 79 | +func (m *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 80 | + if m.pExporter != nil { |
| 81 | + m.pExporter.ServeHTTP(w, r) |
| 82 | + return |
| 83 | + } |
| 84 | + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) |
| 85 | +} |
| 86 | + |
| 87 | +// Stop flushes metrics and stops exporting. Stop should be called |
| 88 | +// before exiting. |
| 89 | +func (m *Service) Stop() { |
| 90 | + if sde := m.sdExporter; sde != nil { |
| 91 | + // Flush any unsent data before exiting. |
| 92 | + sde.Flush() |
| 93 | + |
| 94 | + sde.StopMetricsExporter() |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// MonitoredResource wraps a *mrpb.MonitoredResource to implement the |
| 99 | +// monitoredresource.MonitoredResource interface. |
| 100 | +type MonitoredResource mrpb.MonitoredResource |
| 101 | + |
| 102 | +func (r *MonitoredResource) MonitoredResource() (resType string, labels map[string]string) { |
| 103 | + return r.Type, r.Labels |
| 104 | +} |
| 105 | + |
| 106 | +// GCEResource populates a MonitoredResource with GCE Metadata. |
| 107 | +// |
| 108 | +// The returned MonitoredResource will have the type set to "generic_task". |
| 109 | +func GCEResource(jobName string) (*MonitoredResource, error) { |
| 110 | + projID, err := metadata.ProjectID() |
| 111 | + if err != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + zone, err := metadata.Zone() |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + inst, err := metadata.InstanceName() |
| 119 | + if err != nil { |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + group, err := instanceGroupName() |
| 123 | + if err != nil { |
| 124 | + return nil, err |
| 125 | + } else if group == "" { |
| 126 | + group = projID |
| 127 | + } |
| 128 | + |
| 129 | + return (*MonitoredResource)(&mrpb.MonitoredResource{ |
| 130 | + Type: "generic_task", // See: https://cloud.google.com/monitoring/api/resources#tag_generic_task |
| 131 | + Labels: map[string]string{ |
| 132 | + "project_id": projID, |
| 133 | + "location": zone, |
| 134 | + "namespace": group, |
| 135 | + "job": jobName, |
| 136 | + "task_id": inst, |
| 137 | + }, |
| 138 | + }), nil |
| 139 | +} |
| 140 | + |
| 141 | +// instanceGroupName fetches the instanceGroupName from the instance |
| 142 | +// metadata. |
| 143 | +// |
| 144 | +// The instance group manager applies a custom "created-by" attribute |
| 145 | +// to the instance, which is not part of the metadata package API, and |
| 146 | +// must be queried separately. |
| 147 | +// |
| 148 | +// An empty string will be returned if a metadata.NotDefinedError is |
| 149 | +// returned when fetching metadata. An error will be returned if other |
| 150 | +// errors occur when fetching metadata. |
| 151 | +func instanceGroupName() (string, error) { |
| 152 | + ig, err := metadata.InstanceAttributeValue("created-by") |
| 153 | + if errors.As(err, new(metadata.NotDefinedError)) { |
| 154 | + return "", nil |
| 155 | + } else if err != nil { |
| 156 | + return "", err |
| 157 | + } |
| 158 | + // "created-by" format: "projects/{{InstanceID}}/zones/{{Zone}}/instanceGroupManagers/{{Instance Group Name}} |
| 159 | + return path.Base(ig), nil |
| 160 | +} |
0 commit comments