Skip to content

Commit

Permalink
1/3 Port PDH helpers from Core (#1175)
Browse files Browse the repository at this point in the history
* Port pdh code from core into new windowsperfcountersreciever component

* Update LICENSE

* Fix tab spacing in Makefile.Common
  • Loading branch information
james-bebbington committed Oct 12, 2020
1 parent 2b847fc commit 2d6c3a4
Show file tree
Hide file tree
Showing 16 changed files with 1,305 additions and 1 deletion.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ run:
# default value is empty list, but default dirs are skipped independently
# from this option's value (see skip-dirs-use-default).
skip-dirs:
- third_party

# default is true. Enables skipping of directories:
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
Expand Down
3 changes: 2 additions & 1 deletion Makefile.Common
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
SRC_ROOT := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))

ALL_SRC := $(shell find . -name '*.go' \
-not -path '*/third_party/*' \
-type f | sort)

# All source code and documents. Used in spell check.
Expand Down Expand Up @@ -127,4 +128,4 @@ impi:

.PHONY: dep
dep:
go mod download
go mod download
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/splunkhecreceiver v0.0.0-00010101000000-000000000000
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver v0.0.0-00010101000000-000000000000
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/wavefrontreceiver v0.0.0-00010101000000-000000000000
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowsperfcountersreceiver v0.0.0-00010101000000-000000000000
github.com/pavius/impi v0.0.3
github.com/stretchr/testify v1.6.1
github.com/tcnksm/ghr v0.13.0
Expand Down Expand Up @@ -132,6 +133,8 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/wavef

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/dockerstatsreceiver => ./receiver/dockerstatsreceiver

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowsperfcountersreceiver => ./receiver/windowsperfcountersreceiver

replace github.com/open-telemetry/opentelemetry-collector-contrib/processor/k8sprocessor => ./processor/k8sprocessor/

replace github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor => ./processor/resourcedetectionprocessor/
Expand Down
1 change: 1 addition & 0 deletions receiver/windowsperfcountersreceiver/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../Makefile.Common
8 changes: 8 additions & 0 deletions receiver/windowsperfcountersreceiver/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowsperfcountersreceiver

go 1.14

require (
github.com/stretchr/testify v1.6.1
golang.org/x/sys v0.0.0-20201005172224-997123666555
)
13 changes: 13 additions & 0 deletions receiver/windowsperfcountersreceiver/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20201005172224-997123666555 h1:fihtqzYxy4E31W1yUlyRGveTZT1JIP0bmKaDZ2ceKAw=
golang.org/x/sys v0.0.0-20201005172224-997123666555/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
105 changes: 105 additions & 0 deletions receiver/windowsperfcountersreceiver/internal/pdh/pdh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// 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:https://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.

// +build windows

package pdh

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

const totalInstanceName = "_Total"

type PerfCounter struct {
path string
query win_perf_counters.PerformanceQuery
handle win_perf_counters.PDH_HCOUNTER
}

// NewPerfCounter returns a new performance counter for the specified descriptor.
func NewPerfCounter(counterPath string, collectOnStartup bool) (*PerfCounter, error) {
query := &win_perf_counters.PerformanceQueryImpl{}
err := query.Open()
if err != nil {
return nil, err
}

var handle win_perf_counters.PDH_HCOUNTER
handle, err = query.AddEnglishCounterToQuery(counterPath)
if err != nil {
return nil, err
}

// Some perf counters (e.g. cpu) return the usage stats since the last measure.
// We collect data on startup to avoid an invalid initial reading
if collectOnStartup {
err = query.CollectData()
if err != nil {
return nil, err
}
}

counter := &PerfCounter{
path: counterPath,
query: query,
handle: handle,
}

return counter, nil
}

// Close
func (pc *PerfCounter) Close() error {
return pc.query.Close()
}

// Path
func (pc *PerfCounter) Path() string {
return pc.path
}

// ScrapeData
func (pc *PerfCounter) ScrapeData() ([]win_perf_counters.CounterValue, error) {
err := pc.query.CollectData()
if err != nil {
return nil, err
}

vals, err := pc.query.GetFormattedCounterArrayDouble(pc.handle)
if err != nil {
return nil, err
}

vals = removeTotalIfMultipleValues(vals)
return vals, nil
}

func removeTotalIfMultipleValues(vals []win_perf_counters.CounterValue) []win_perf_counters.CounterValue {
if len(vals) <= 1 {
return vals
}

for i, val := range vals {
if val.InstanceName == totalInstanceName {
return removeItemAt(vals, i)
}
}

return vals
}

func removeItemAt(vals []win_perf_counters.CounterValue, idx int) []win_perf_counters.CounterValue {
vals[idx] = vals[len(vals)-1]
vals[len(vals)-1] = win_perf_counters.CounterValue{}
return vals[:len(vals)-1]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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:https://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.

// +build !windows

package pdh
91 changes: 91 additions & 0 deletions receiver/windowsperfcountersreceiver/internal/pdh/pdh_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// 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:https://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.

// +build windows

package pdh

import (
"testing"

"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"
)

func TestNewPerfCounter_InvalidPath(t *testing.T) {
_, err := NewPerfCounter("Invalid Counter Path", false)
if assert.Error(t, err) {
assert.Regexp(t, "^Unable to parse the counter path", err.Error())
}
}

func TestNewPerfCounter(t *testing.T) {
pc, err := NewPerfCounter(`\Memory\Committed Bytes`, false)
require.NoError(t, err, "Failed to create performance counter: %v", err)

assert.NotNil(t, pc.query)
assert.NotNil(t, pc.handle)

// the first collection will return a zero value
var vals []win_perf_counters.CounterValue
vals, err = pc.query.GetFormattedCounterArrayDouble(pc.handle)
require.NoError(t, err)
assert.Equal(t, []win_perf_counters.CounterValue{{InstanceName: "", Value: 0}}, vals)

err = pc.query.Close()
require.NoError(t, err, "Failed to close initialized performance counter query: %v", err)
}

func TestNewPerfCounter_CollectOnStartup(t *testing.T) {
pc, err := NewPerfCounter(`\Memory\Committed Bytes`, true)
require.NoError(t, err, "Failed to create performance counter: %v", err)

assert.NotNil(t, pc.query)
assert.NotNil(t, pc.handle)

// since we collected on startup, the next collection will return a measured value
var vals []win_perf_counters.CounterValue
vals, err = pc.query.GetFormattedCounterArrayDouble(pc.handle)
require.NoError(t, err)
assert.Greater(t, vals[0].Value, float64(0))

err = pc.query.Close()
require.NoError(t, err, "Failed to close initialized performance counter query: %v", err)
}

func TestPerfCounter_Close(t *testing.T) {
pc, err := NewPerfCounter(`\Memory\Committed Bytes`, false)
require.NoError(t, err)

err = pc.Close()
require.NoError(t, err, "Failed to close initialized performance counter query: %v", err)

err = pc.Close()
if assert.Error(t, err) {
assert.Equal(t, "uninitialised query", err.Error())
}
}

func TestPerfCounter_ScrapeData(t *testing.T) {
pc, err := NewPerfCounter(`\Memory\Committed Bytes`, false)
require.NoError(t, err)

performanceCounters, err := pc.ScrapeData()
require.NoError(t, err, "Failed to scrape data: %v", err)

assert.Equal(t, 1, len(performanceCounters))
assert.NotNil(t, 1, performanceCounters[0])
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015-2020 InfluxData Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) 2010 The win Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. The names of the authors may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// This is the official list of 'win' authors for copyright purposes.
//
// Alexander Neumann <[email protected]>
// Joseph Watson <[email protected]>
// Kevin Pors <[email protected]>

// +build windows

package win_perf_counters

import (
"syscall"
)

type SYSTEMTIME struct {
wYear uint16
wMonth uint16
wDayOfWeek uint16
wDay uint16
wHour uint16
wMinute uint16
wSecond uint16
wMilliseconds uint16
}

type FILETIME struct {
dwLowDateTime uint32
dwHighDateTime uint32
}

var (
// Library
libkrnDll *syscall.DLL

// Functions
krn_FileTimeToSystemTime *syscall.Proc
krn_FileTimeToLocalFileTime *syscall.Proc
krn_LocalFileTimeToFileTime *syscall.Proc
krn_WideCharToMultiByte *syscall.Proc
)

func init() {
libkrnDll = syscall.MustLoadDLL("Kernel32.dll")

krn_FileTimeToSystemTime = libkrnDll.MustFindProc("FileTimeToSystemTime")
krn_FileTimeToLocalFileTime = libkrnDll.MustFindProc("FileTimeToLocalFileTime")
krn_LocalFileTimeToFileTime = libkrnDll.MustFindProc("LocalFileTimeToFileTime")
krn_WideCharToMultiByte = libkrnDll.MustFindProc("WideCharToMultiByte")
}
Loading

0 comments on commit 2d6c3a4

Please sign in to comment.