Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add resource to vmreceiver. #31

Merged
merged 3 commits into from
Jun 22, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
30 changes: 30 additions & 0 deletions receiver/vmmetricsreceiver/vm_metrics_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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 +53,8 @@ const (
defaultScrapeInterval = 10 * time.Second
)

var rsc *resourcepb.Resource
pjanotti marked this conversation as resolved.
Show resolved Hide resolved

// 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 +88,26 @@ func NewVMMetricsCollector(si time.Duration, mountPoint, processMountPoint, pref
return vmc, nil
}

func detectResource() {
res, err := auto.Detect(context.Background())
if err != nil {
panic(fmt.Sprintf("Resource detection failed, err:%v", err))
}
if res != nil {
rsc = &resourcepb.Resource{
pjanotti marked this conversation as resolved.
Show resolved Hide resolved
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 +140,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 +165,7 @@ func (vmc *VMMetricsCollector) scrapeAndExport() {
metrics,
&metricspb.Metric{
MetricDescriptor: metricProcessCPUSeconds,
Resource: rsc,
Timeseries: []*metricspb.TimeSeries{vmc.getDoubleTimeSeries(procStat.CPUTime(), nil)},
},
)
Expand All @@ -156,18 +182,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