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

[receiver/windowsperfcountersreceiver] Create common package for windows performance counter receivers #9108

Merged
merged 23 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from 8 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
5 changes: 4 additions & 1 deletion cmd/configschema/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheusremotewrite v0.48.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/signalfx v0.48.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/zipkin v0.48.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters v0.48.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/processor/attributesprocessor v0.48.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/processor/cumulativetodeltaprocessor v0.48.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatorateprocessor v0.48.0 // indirect
Expand Down Expand Up @@ -397,7 +398,7 @@ require (
github.com/shirou/gopsutil/v3 v3.22.3 // indirect
github.com/signalfx/com_signalfx_metrics_protobuf v0.0.3 // indirect
github.com/signalfx/gohistogram v0.0.0-20160107210732-1ccfd2ff5083 // indirect
github.com/signalfx/golib/v3 v3.3.13 // indirect
github.com/signalfx/golib/v3 v3.3.44 // indirect
github.com/signalfx/sapm-proto v0.9.0 // indirect
github.com/signalfx/signalfx-agent/pkg/apm v0.0.0-20201202163743-65b4fa925fc8 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
Expand Down Expand Up @@ -644,6 +645,8 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/extension/stor

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchperresourceattr => ../../pkg/batchperresourceattr

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters => ../../pkg/winperfcounters

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchpersignal => ../../pkg/batchpersignal

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata => ../../pkg/experimentalmetricmetadata
Expand Down
324 changes: 323 additions & 1 deletion cmd/configschema/go.sum

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheusremotewrite v0.48.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/signalfx v0.48.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/zipkin v0.48.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters v0.48.0 // indirect
github.com/open-telemetry/opentelemetry-log-collection v0.28.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
Expand Down Expand Up @@ -650,6 +651,8 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchperre

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchpersignal => ./pkg/batchpersignal

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters => ./pkg/winperfcounters

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata => ./pkg/experimentalmetricmetadata

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/resourcetotelemetry => ./pkg/resourcetotelemetry
Expand Down
1 change: 1 addition & 0 deletions pkg/winperfcounters/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../Makefile.Common
29 changes: 29 additions & 0 deletions pkg/winperfcounters/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package winperfcounters // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters"

// CounterConfig defines the individual counter in an object.
type CounterConfig struct {
Name string `mapstructure:"name"`
Metric string `mapstructure:"metric"`
Attributes map[string]string `mapstructure:"attributes"`
}

// ObjectConfig defines configuration for a perf counter object.
type ObjectConfig struct {
Object string `mapstructure:"object"`
Instances []string `mapstructure:"instances"`
Counters []CounterConfig `mapstructure:"counters"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
//go:build windows
// +build windows

package windowsperfcountersreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowsperfcountersreceiver"
package winperfcounters // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters"

func (pc *PerfCounterConfig) instances() []string {
func (pc *ObjectConfig) instances() []string {
if len(pc.Instances) == 0 {
return []string{""}
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/winperfcounters/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters

go 1.17

require (
github.com/stretchr/testify v1.7.1
go.uber.org/multierr v1.8.0
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

require github.com/rogpeppe/go-internal v1.6.2 // indirect

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest => ../../internal/scrapertest
37 changes: 37 additions & 0 deletions pkg/winperfcounters/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
//go:build windows
// +build windows

package pdh // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowsperfcountersreceiver/internal/pdh"
package pdh // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters/internal/pdh"

import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowsperfcountersreceiver/internal/third_party/telegraf/win_perf_counters"
import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters/internal/third_party/telegraf/win_perf_counters"

const totalInstanceName = "_Total"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
//go:build !windows
// +build !windows

package pdh // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowsperfcountersreceiver/internal/pdh"
package pdh // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters/internal/pdh"
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowsperfcountersreceiver/internal/third_party/telegraf/win_perf_counters"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters/internal/third_party/telegraf/win_perf_counters"
)

func TestNewPerfCounter_InvalidPath(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
//go:build windows
// +build windows

package win_perf_counters // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowsperfcountersreceiver/internal/third_party/telegraf/win_perf_counters"
package win_perf_counters // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters/internal/third_party/telegraf/win_perf_counters"

import (
"syscall"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
//go:build windows
// +build windows

package win_perf_counters // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowsperfcountersreceiver/internal/third_party/telegraf/win_perf_counters"
package win_perf_counters // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters/internal/third_party/telegraf/win_perf_counters"

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
//go:build windows
// +build windows

package win_perf_counters // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowsperfcountersreceiver/internal/third_party/telegraf/win_perf_counters"
package win_perf_counters // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters/internal/third_party/telegraf/win_perf_counters"

// Union specialization for double values
type PDH_FMT_COUNTERVALUE_DOUBLE struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
//go:build windows
// +build windows

package win_perf_counters // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowsperfcountersreceiver/internal/third_party/telegraf/win_perf_counters"
package win_perf_counters // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters/internal/third_party/telegraf/win_perf_counters"

// Union specialization for double values
type PDH_FMT_COUNTERVALUE_DOUBLE struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//go:build windows
// +build windows

package win_perf_counters // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowsperfcountersreceiver/internal/third_party/telegraf/win_perf_counters"
package win_perf_counters // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters/internal/third_party/telegraf/win_perf_counters"

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//go:build !windows
// +build !windows

package win_perf_counters // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters/internal/third_party/telegraf/win_perf_counters"
157 changes: 157 additions & 0 deletions pkg/winperfcounters/scraper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build windows
// +build windows

package winperfcounters // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters"

import (
"fmt"

"go.uber.org/multierr"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters/internal/pdh"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters/internal/third_party/telegraf/win_perf_counters"
)

var _ PerfCounterWatcher = (*Watcher)(nil)

// PerfCounterWatcher represents how to scrape data
type PerfCounterWatcher interface {
// Path returns the counter path
Path() string
// ScrapeData collects a measurement and returns the value(s).
ScrapeData() ([]win_perf_counters.CounterValue, error)
// Close all counters/handles related to the query and free all associated memory.
Close() error

GetMetricRep() MetricRep
}

const instanceLabelName = "instance"

type WatcherCfg struct {
ObjectCfg ObjectConfig
}

type MetricRep struct {
Name string
Attributes map[string]string
}

type Watcher struct {
Counter *pdh.PerfCounter
MetricRep
}

func (w Watcher) Path() string {
return w.Counter.Path()
}

func (w Watcher) ScrapeData() ([]win_perf_counters.CounterValue, error) {
return w.Counter.ScrapeData()
}

func (w Watcher) Close() error {
return w.Counter.Close()
}

func (w Watcher) GetMetricRep() MetricRep {
return w.MetricRep
}

// BuildPaths creates watchers and their paths from configs.
func BuildPaths(scraperCfgs []WatcherCfg) ([]PerfCounterWatcher, error) {
var errs error
var watchers []PerfCounterWatcher

for _, scraperCfg := range scraperCfgs {
for _, instance := range scraperCfg.ObjectCfg.instances() {
for _, counterCfg := range scraperCfg.ObjectCfg.Counters {
counterPath := counterPath(scraperCfg.ObjectCfg.Object, instance, counterCfg.Name)

c, err := pdh.NewPerfCounter(counterPath, true)
if err != nil {
errs = multierr.Append(errs, fmt.Errorf("counter %v: %w", counterPath, err))
} else {
newWatcher := Watcher{Counter: c}

if counterCfg.Metric != "" {
metricCfg := MetricRep{Name: counterCfg.Metric}
if counterCfg.Attributes != nil {
metricCfg.Attributes = counterCfg.Attributes
}
newWatcher.MetricRep = metricCfg
} else {
newWatcher.MetricRep.Name = c.Path()
}

watchers = append(watchers, newWatcher)
}
}
}
}

return watchers, errs
}

func counterPath(object, instance, counterName string) string {
if instance != "" {
instance = fmt.Sprintf("(%s)", instance)
}

return fmt.Sprintf("\\%s%s\\%s", object, instance, counterName)
}

type CounterValue struct {
MetricRep
Value int64
}

// WatchCounters pulls values given the passed in watchers
func WatchCounters(watchers []PerfCounterWatcher) (metrics []CounterValue, errs error) {
for _, scraper := range watchers {
counterValues, err := scraper.ScrapeData()
if err != nil {
errs = multierr.Append(errs, err)
continue
}
metric := scraper.GetMetricRep()

for _, counterValue := range counterValues {
if counterValue.InstanceName != "" {
if metric.Attributes == nil {
metric.Attributes = map[string]string{instanceLabelName: counterValue.InstanceName}
}
metric.Attributes[instanceLabelName] = counterValue.InstanceName
}

metrics = append(metrics, CounterValue{MetricRep: metric, Value: int64(counterValue.Value)})
}
}
return metrics, errs
}

// CloseCounters closes the passed in counters.
// This should be called in the shutdown function of receivers using this package
func CloseCounters(watchers []PerfCounterWatcher) error {
var errs error

for _, scraper := range watchers {
errs = multierr.Append(errs, scraper.Close())
}

return errs
}
Loading