@@ -55,6 +55,8 @@ import (
5555 "github.com/prometheus/prometheus/promql"
5656 "go.opencensus.io/plugin/ocgrpc"
5757 "go.opencensus.io/plugin/ochttp"
58+ "go.opencensus.io/resource"
59+ "go.opencensus.io/stats"
5860 "go.opencensus.io/stats/view"
5961 "go.opencensus.io/tag"
6062 metric_pb "google.golang.org/genproto/googleapis/api/metric"
@@ -66,6 +68,14 @@ import (
6668var (
6769 sizeDistribution = view .Distribution (0 , 1024 , 2048 , 4096 , 16384 , 65536 , 262144 , 1048576 , 4194304 , 33554432 )
6870 latencyDistribution = view .Distribution (0 , 1 , 2 , 5 , 10 , 15 , 25 , 50 , 100 , 200 , 400 , 800 , 1500 , 3000 , 6000 )
71+
72+ // VersionTag identifies the version of this binary.
73+ VersionTag = tag .MustNewKey ("version" )
74+ // UptimeMeasure is a cumulative metric.
75+ UptimeMeasure = stats .Int64 (
76+ "agent.googleapis.com/agent/uptime" ,
77+ "uptime of the Stackdriver Prometheus collector" ,
78+ stats .UnitSeconds )
6979)
7080
7181func init () {
@@ -108,6 +118,15 @@ func init() {
108118 ); err != nil {
109119 panic (err )
110120 }
121+ if err := view .Register (
122+ & view.View {
123+ Measure : UptimeMeasure ,
124+ TagKeys : []tag.Key {VersionTag },
125+ Aggregation : view .Sum (),
126+ },
127+ ); err != nil {
128+ panic (err )
129+ }
111130}
112131
113132type kubernetesConfig struct {
@@ -276,6 +295,21 @@ func main() {
276295 level .Info (logger ).Log ("host_details" , Uname ())
277296 level .Info (logger ).Log ("fd_limits" , FdLimits ())
278297
298+ // We instantiate a context here since the tailer is used by two other components.
299+ // The context will be used in the lifecycle of prometheusReader further down.
300+ ctx , cancel := context .WithCancel (context .Background ())
301+
302+ go func () {
303+ uptimeUpdateTime := time .Now ()
304+ c := time .Tick (60 * time .Second )
305+ for now := range c {
306+ stats .RecordWithTags (ctx ,
307+ []tag.Mutator {tag .Upsert (VersionTag , fmt .Sprintf ("stackdriver-prometheus-sidecar/%s" , version .Version ))},
308+ UptimeMeasure .M (int64 (now .Sub (uptimeUpdateTime ).Seconds ())))
309+ uptimeUpdateTime = now
310+ }
311+ }()
312+
279313 httpClient := & http.Client {Transport : & ochttp.Transport {}}
280314
281315 if * projectId == "" {
@@ -294,14 +328,30 @@ func main() {
294328 }
295329 view .RegisterExporter (promExporter )
296330 case "stackdriver" :
297- sd , err := oc_stackdriver .NewExporter (oc_stackdriver.Options {ProjectID : * projectId })
331+ const reportingInterval = 60 * time .Second
332+ sd , err := oc_stackdriver .NewExporter (oc_stackdriver.Options {
333+ ProjectID : * projectId ,
334+ // If the OpenCensus resource environment variables aren't set, the monitored resource will likely fall back to `generic_task`.
335+ ResourceDetector : resource .FromEnv ,
336+ ReportingInterval : reportingInterval ,
337+ // Disable default `opencensus_task` label.
338+ DefaultMonitoringLabels : & oc_stackdriver.Labels {},
339+ GetMetricType : func (v * view.View ) string {
340+ // Curated metrics produced by this process.
341+ if strings .Contains (v .Name , "agent.googleapis.com" ) {
342+ return v .Name
343+ }
344+ // Default OpenCensus behavior.
345+ return path .Join ("custom.googleapis.com" , "opencensus" , v .Name )
346+ },
347+ })
298348 if err != nil {
299349 level .Error (logger ).Log ("msg" , "Creating Stackdriver exporter failed" , "err" , err )
300350 os .Exit (1 )
301351 }
302352 defer sd .Flush ()
303353 view .RegisterExporter (sd )
304- view .SetReportingPeriod (60 * time . Second )
354+ view .SetReportingPeriod (reportingInterval )
305355 default :
306356 level .Error (logger ).Log ("msg" , "Unknown monitoring backend" , "backend" , backend )
307357 os .Exit (1 )
@@ -356,10 +406,6 @@ func main() {
356406 }
357407 metadataCache := metadata .NewCache (httpClient , metadataURL , cfg .StaticMetadata )
358408
359- // We instantiate a context here since the tailer is used by two other components.
360- // The context will be used in the lifecycle of prometheusReader further down.
361- ctx , cancel := context .WithCancel (context .Background ())
362-
363409 tailer , err := tail .Tail (ctx , cfg .WALDirectory )
364410 if err != nil {
365411 level .Error (logger ).Log ("msg" , "Tailing WAL failed" , "err" , err )
0 commit comments