Skip to content

Commit

Permalink
add resource to vmreceiver. (#31)
Browse files Browse the repository at this point in the history
* add resource to vmreceiver.

* add panic if resource detection fails.

* add sync.Once around resource detection.
  • Loading branch information
rghetia authored and songy23 committed Jun 22, 2019
1 parent 4138e51 commit 9e8f857
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
contrib.go.opencensus.io/exporter/ocagent v0.5.0
contrib.go.opencensus.io/exporter/prometheus v0.1.0
contrib.go.opencensus.io/exporter/zipkin v0.1.1
contrib.go.opencensus.io/resource v0.1.1
github.com/VividCortex/gohistogram v1.0.0 // indirect
github.com/apache/thrift v0.0.0-20161221203622-b2a4d4ae21c7
github.com/aws/aws-sdk-go v1.19.18 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ contrib.go.opencensus.io/exporter/prometheus v0.1.0 h1:SByaIoWwNgMdPSgl5sMqM2KDE
contrib.go.opencensus.io/exporter/prometheus v0.1.0/go.mod h1:cGFniUXGZlKRjzOyuZJ6mgB+PgBcCIa79kEKR8YCW+A=
contrib.go.opencensus.io/exporter/zipkin v0.1.1 h1:PR+1zWqY8ceXs1qDQQIlgXe+sdiwCf0n32bH4+Epk8g=
contrib.go.opencensus.io/exporter/zipkin v0.1.1/go.mod h1:GMvdSl3eJ2gapOaLKzTKE3qDgUkJ86k9k3yY2eqwkzc=
contrib.go.opencensus.io/resource v0.1.1 h1:4r2CANuYhKGmYWP02+5E94rLRcS/YeD+KlxSrOsMxk0=
contrib.go.opencensus.io/resource v0.1.1/go.mod h1:F361eGI91LCmW1I/Saf+rX0+OFcigGlFvXwEGEnkRLA=
github.com/Azure/azure-sdk-for-go v0.0.0-20161028183111-bd73d950fa44 h1:L4fLiifszjLnCRGi6Xhp0MgUwjIMbVXKbayoRiVxkU8=
github.com/Azure/azure-sdk-for-go v0.0.0-20161028183111-bd73d950fa44/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
github.com/Azure/go-autorest v10.8.1+incompatible h1:u0jVQf+a6k6x8A+sT60l6EY9XZu+kHdnZVPAYqpVRo0=
Expand Down
34 changes: 34 additions & 0 deletions receiver/vmmetricsreceiver/vm_metrics_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"os"
"runtime"
"sync"
"time"

"github.com/prometheus/procfs"
Expand All @@ -28,7 +29,9 @@ import (
"github.com/open-telemetry/opentelemetry-service/data"
"github.com/open-telemetry/opentelemetry-service/internal"

"contrib.go.opencensus.io/resource/auto"
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1"
)

// VMMetricsCollector is a struct that collects and reports VM and process metrics (cpu, mem, etc).
Expand All @@ -51,6 +54,9 @@ const (
defaultScrapeInterval = 10 * time.Second
)

var rsc *resourcepb.Resource
var resourceDetectionSync sync.Once

// NewVMMetricsCollector creates a new set of VM and Process Metrics (mem, cpu).
func NewVMMetricsCollector(si time.Duration, mountPoint, processMountPoint, prefix string, consumer consumer.MetricsConsumer) (*VMMetricsCollector, error) {
if mountPoint == "" {
Expand Down Expand Up @@ -84,8 +90,28 @@ func NewVMMetricsCollector(si time.Duration, mountPoint, processMountPoint, pref
return vmc, nil
}

func detectResource() {
resourceDetectionSync.Do(func() {
res, err := auto.Detect(context.Background())
if err != nil {
panic(fmt.Sprintf("Resource detection failed, err:%v", err))
}
if res != nil {
rsc = &resourcepb.Resource{
Type: res.Type,
Labels: make(map[string]string, len(res.Labels)),
}
for k, v := range res.Labels {
rsc.Labels[k] = v
}
}
})
}

// StartCollection starts a ticker'd goroutine that will scrape and export vm metrics periodically.
func (vmc *VMMetricsCollector) StartCollection() {
detectResource()

go func() {
ticker := time.NewTicker(vmc.scrapeInterval)
for {
Expand Down Expand Up @@ -118,14 +144,17 @@ func (vmc *VMMetricsCollector) scrapeAndExport() {
metrics,
&metricspb.Metric{
MetricDescriptor: metricAllocMem,
Resource: rsc,
Timeseries: []*metricspb.TimeSeries{vmc.getInt64TimeSeries(ms.Alloc)},
},
&metricspb.Metric{
MetricDescriptor: metricTotalAllocMem,
Resource: rsc,
Timeseries: []*metricspb.TimeSeries{vmc.getInt64TimeSeries(ms.TotalAlloc)},
},
&metricspb.Metric{
MetricDescriptor: metricSysMem,
Resource: rsc,
Timeseries: []*metricspb.TimeSeries{vmc.getInt64TimeSeries(ms.Sys)},
},
)
Expand All @@ -140,6 +169,7 @@ func (vmc *VMMetricsCollector) scrapeAndExport() {
metrics,
&metricspb.Metric{
MetricDescriptor: metricProcessCPUSeconds,
Resource: rsc,
Timeseries: []*metricspb.TimeSeries{vmc.getDoubleTimeSeries(procStat.CPUTime(), nil)},
},
)
Expand All @@ -156,18 +186,22 @@ func (vmc *VMMetricsCollector) scrapeAndExport() {
metrics,
&metricspb.Metric{
MetricDescriptor: metricProcessesRunning,
Resource: rsc,
Timeseries: []*metricspb.TimeSeries{vmc.getInt64TimeSeries(stat.ProcessesRunning)},
},
&metricspb.Metric{
MetricDescriptor: metricProcessesBlocked,
Resource: rsc,
Timeseries: []*metricspb.TimeSeries{vmc.getInt64TimeSeries(stat.ProcessesBlocked)},
},
&metricspb.Metric{
MetricDescriptor: metricProcessesCreated,
Resource: rsc,
Timeseries: []*metricspb.TimeSeries{vmc.getInt64TimeSeries(stat.ProcessCreated)},
},
&metricspb.Metric{
MetricDescriptor: metricCPUSeconds,
Resource: rsc,
Timeseries: []*metricspb.TimeSeries{
vmc.getDoubleTimeSeries(cpuStat.User, labelValueCPUUser),
vmc.getDoubleTimeSeries(cpuStat.System, labelValueCPUSystem),
Expand Down

0 comments on commit 9e8f857

Please sign in to comment.