-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port pdh code from core into new windowsperfcountersreciever component
- Loading branch information
1 parent
450f782
commit dbb9d58
Showing
16 changed files
with
1,304 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include ../../Makefile.Common |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
105
receiver/windowsperfcountersreceiver/internal/pdh/pdh.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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://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] | ||
} |
17 changes: 17 additions & 0 deletions
17
receiver/windowsperfcountersreceiver/internal/pdh/pdh_notwindows.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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://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
91
receiver/windowsperfcountersreceiver/internal/pdh/pdh_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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://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]) | ||
} |
21 changes: 21 additions & 0 deletions
21
receiver/windowsperfcountersreceiver/internal/third_party/telegraf/win_perf_counters/LICENSE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015-2019 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. |
73 changes: 73 additions & 0 deletions
73
...r/windowsperfcountersreceiver/internal/third_party/telegraf/win_perf_counters/kernel32.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <an2048@googlemail.com> | ||
// Joseph Watson <jtwatson@linux-consulting.us> | ||
// Kevin Pors <krpors@gmail.com> | ||
|
||
// +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") | ||
} |
Oops, something went wrong.