@@ -19,8 +19,10 @@ import (
1919 "fmt"
2020 "os"
2121 "runtime"
22+ "sync"
2223 "time"
2324
25+ "contrib.go.opencensus.io/resource/auto"
2426 "github.com/prometheus/procfs"
2527 "go.opencensus.io/trace"
2628
@@ -29,6 +31,7 @@ import (
2931 "github.com/census-instrumentation/opencensus-service/internal"
3032
3133 metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
34+ resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1"
3235)
3336
3437// VMMetricsCollector is a struct that collects and reports VM and process metrics (cpu, mem, etc).
@@ -51,6 +54,9 @@ const (
5154 defaultScrapeInterval = 10 * time .Second
5255)
5356
57+ var rsc * resourcepb.Resource
58+ var resourceDetectorSync sync.Once
59+
5460// NewVMMetricsCollector creates a new set of VM and Process Metrics (mem, cpu).
5561func NewVMMetricsCollector (si time.Duration , mountPoint , processMountPoint , prefix string , consumer consumer.MetricsConsumer ) (* VMMetricsCollector , error ) {
5662 if mountPoint == "" {
@@ -84,8 +90,28 @@ func NewVMMetricsCollector(si time.Duration, mountPoint, processMountPoint, pref
8490 return vmc , nil
8591}
8692
93+ func detectResource () {
94+ resourceDetectorSync .Do (func () {
95+ res , err := auto .Detect (context .Background ())
96+ if err != nil {
97+ panic (fmt .Sprintf ("Resource detection failed, err:%v" , err ))
98+ }
99+ if res != nil {
100+ rsc = & resourcepb.Resource {
101+ Type : res .Type ,
102+ Labels : make (map [string ]string , len (res .Labels )),
103+ }
104+ for k , v := range res .Labels {
105+ rsc .Labels [k ] = v
106+ }
107+ }
108+ })
109+ }
110+
87111// StartCollection starts a ticker'd goroutine that will scrape and export vm metrics periodically.
88112func (vmc * VMMetricsCollector ) StartCollection () {
113+ detectResource ()
114+
89115 go func () {
90116 ticker := time .NewTicker (vmc .scrapeInterval )
91117 for {
@@ -118,14 +144,17 @@ func (vmc *VMMetricsCollector) scrapeAndExport() {
118144 metrics ,
119145 & metricspb.Metric {
120146 MetricDescriptor : metricAllocMem ,
147+ Resource : rsc ,
121148 Timeseries : []* metricspb.TimeSeries {vmc .getInt64TimeSeries (ms .Alloc )},
122149 },
123150 & metricspb.Metric {
124151 MetricDescriptor : metricTotalAllocMem ,
152+ Resource : rsc ,
125153 Timeseries : []* metricspb.TimeSeries {vmc .getInt64TimeSeries (ms .TotalAlloc )},
126154 },
127155 & metricspb.Metric {
128156 MetricDescriptor : metricSysMem ,
157+ Resource : rsc ,
129158 Timeseries : []* metricspb.TimeSeries {vmc .getInt64TimeSeries (ms .Sys )},
130159 },
131160 )
@@ -140,6 +169,7 @@ func (vmc *VMMetricsCollector) scrapeAndExport() {
140169 metrics ,
141170 & metricspb.Metric {
142171 MetricDescriptor : metricProcessCPUSeconds ,
172+ Resource : rsc ,
143173 Timeseries : []* metricspb.TimeSeries {vmc .getDoubleTimeSeries (procStat .CPUTime (), nil )},
144174 },
145175 )
@@ -156,18 +186,22 @@ func (vmc *VMMetricsCollector) scrapeAndExport() {
156186 metrics ,
157187 & metricspb.Metric {
158188 MetricDescriptor : metricProcessesRunning ,
189+ Resource : rsc ,
159190 Timeseries : []* metricspb.TimeSeries {vmc .getInt64TimeSeries (stat .ProcessesRunning )},
160191 },
161192 & metricspb.Metric {
162193 MetricDescriptor : metricProcessesBlocked ,
194+ Resource : rsc ,
163195 Timeseries : []* metricspb.TimeSeries {vmc .getInt64TimeSeries (stat .ProcessesBlocked )},
164196 },
165197 & metricspb.Metric {
166198 MetricDescriptor : metricProcessesCreated ,
199+ Resource : rsc ,
167200 Timeseries : []* metricspb.TimeSeries {vmc .getInt64TimeSeries (stat .ProcessCreated )},
168201 },
169202 & metricspb.Metric {
170203 MetricDescriptor : metricCPUSeconds ,
204+ Resource : rsc ,
171205 Timeseries : []* metricspb.TimeSeries {
172206 vmc .getDoubleTimeSeries (cpuStat .User , labelValueCPUUser ),
173207 vmc .getDoubleTimeSeries (cpuStat .System , labelValueCPUSystem ),
0 commit comments