Skip to content
This repository was archived by the owner on Nov 7, 2022. It is now read-only.

Commit d8a2872

Browse files
rghetiasongy23
authored andcommitted
add resource to vmreceiver (#587)
1 parent dee9655 commit d8a2872

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
contrib.go.opencensus.io/exporter/prometheus v0.1.0
88
contrib.go.opencensus.io/exporter/stackdriver v0.12.2
99
contrib.go.opencensus.io/exporter/zipkin v0.1.1
10+
contrib.go.opencensus.io/resource v0.1.1
1011
github.com/DataDog/datadog-go v2.2.0+incompatible // indirect
1112
github.com/DataDog/opencensus-go-exporter-datadog v0.0.0-20181026070331-e7c4bd17b329
1213
github.com/VividCortex/gohistogram v1.0.0 // indirect

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ contrib.go.opencensus.io/exporter/stackdriver v0.12.2 h1:jU1p9F07ASK11wYgSTPKtFl
1616
contrib.go.opencensus.io/exporter/stackdriver v0.12.2/go.mod h1:iwB6wGarfphGGe/e5CWqyUk/cLzKnWsOKPVW3no6OTw=
1717
contrib.go.opencensus.io/exporter/zipkin v0.1.1 h1:PR+1zWqY8ceXs1qDQQIlgXe+sdiwCf0n32bH4+Epk8g=
1818
contrib.go.opencensus.io/exporter/zipkin v0.1.1/go.mod h1:GMvdSl3eJ2gapOaLKzTKE3qDgUkJ86k9k3yY2eqwkzc=
19+
contrib.go.opencensus.io/resource v0.1.1 h1:4r2CANuYhKGmYWP02+5E94rLRcS/YeD+KlxSrOsMxk0=
1920
contrib.go.opencensus.io/resource v0.1.1/go.mod h1:F361eGI91LCmW1I/Saf+rX0+OFcigGlFvXwEGEnkRLA=
2021
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999 h1:sihTnRgTOUSCQz0iS0pjZuFQy/z7GXCJgSBg3+rZKHw=
2122
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=

receiver/vmmetricsreceiver/vm_metrics_collector.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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).
5561
func 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.
88112
func (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

Comments
 (0)