forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added virtual memory scraper to hostmetrics receiver (open-telemetry#989
) * Initial commit of host metrics virtual memory scraper for windows * Added non-windows virtual memory scraper implementation
- Loading branch information
1 parent
7b3a920
commit 3d83031
Showing
14 changed files
with
958 additions
and
40 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
22 changes: 22 additions & 0 deletions
22
receiver/hostmetricsreceiver/internal/scraper/virtualmemoryscraper/config.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,22 @@ | ||
// Copyright 2020, 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 virtualmemoryscraper | ||
|
||
import "go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal" | ||
|
||
// Config relating to VirtualMemory Metric Scraper. | ||
type Config struct { | ||
internal.ConfigSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct | ||
} |
49 changes: 49 additions & 0 deletions
49
receiver/hostmetricsreceiver/internal/scraper/virtualmemoryscraper/factory.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,49 @@ | ||
// Copyright 2020, 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 virtualmemoryscraper | ||
|
||
import ( | ||
"context" | ||
|
||
"go.uber.org/zap" | ||
|
||
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal" | ||
) | ||
|
||
// This file implements Factory for VirtualMemory scraper. | ||
|
||
const ( | ||
// The value of "type" key in configuration. | ||
TypeStr = "virtualmemory" | ||
) | ||
|
||
// Factory is the Factory for scraper. | ||
type Factory struct { | ||
} | ||
|
||
// CreateDefaultConfig creates the default configuration for the Scraper. | ||
func (f *Factory) CreateDefaultConfig() internal.Config { | ||
return &Config{} | ||
} | ||
|
||
// CreateMetricsScraper creates a scraper based on provided config. | ||
func (f *Factory) CreateMetricsScraper( | ||
ctx context.Context, | ||
logger *zap.Logger, | ||
config internal.Config, | ||
) (internal.Scraper, error) { | ||
cfg := config.(*Config) | ||
return newVirtualMemoryScraper(ctx, cfg), nil | ||
} |
38 changes: 38 additions & 0 deletions
38
receiver/hostmetricsreceiver/internal/scraper/virtualmemoryscraper/factory_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,38 @@ | ||
// Copyright 2020, 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 virtualmemoryscraper | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func TestCreateDefaultConfig(t *testing.T) { | ||
factory := &Factory{} | ||
cfg := factory.CreateDefaultConfig() | ||
assert.IsType(t, &Config{}, cfg) | ||
} | ||
|
||
func TestCreateMetricsScraper(t *testing.T) { | ||
factory := &Factory{} | ||
cfg := &Config{} | ||
|
||
scraper, err := factory.CreateMetricsScraper(context.Background(), zap.NewNop(), cfg) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, scraper) | ||
} |
73 changes: 73 additions & 0 deletions
73
receiver/hostmetricsreceiver/internal/scraper/virtualmemoryscraper/pagefile_windows.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 2020, 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 virtualmemoryscraper | ||
|
||
import ( | ||
"syscall" | ||
"unsafe" | ||
|
||
"golang.org/x/sys/windows" | ||
) | ||
|
||
var ( | ||
modPsapi = windows.NewLazySystemDLL("psapi.dll") | ||
procEnumPageFilesW = modPsapi.NewProc("EnumPageFilesW") | ||
) | ||
|
||
type pageFileData struct { | ||
name string | ||
used uint64 | ||
total uint64 | ||
} | ||
|
||
// system type as defined in https://docs.microsoft.com/en-us/windows/win32/api/psapi/ns-psapi-enum_page_file_information | ||
type enumPageFileInformation struct { | ||
cb uint32 | ||
reserved uint32 | ||
totalSize uint64 | ||
totalInUse uint64 | ||
peakUsage uint64 | ||
} | ||
|
||
func getPageFileStatsInternal() ([]*pageFileData, error) { | ||
// the following system call invokes the supplied callback function once for each page file before returning | ||
// see https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-enumpagefilesw | ||
var pageFiles []*pageFileData | ||
result, _, _ := procEnumPageFilesW.Call(windows.NewCallback(pEnumPageFileCallbackW), uintptr(unsafe.Pointer(&pageFiles))) | ||
if result == 0 { | ||
return nil, windows.GetLastError() | ||
} | ||
|
||
return pageFiles, nil | ||
} | ||
|
||
// system callback as defined in https://docs.microsoft.com/en-us/windows/win32/api/psapi/nc-psapi-penum_page_file_callbackw | ||
func pEnumPageFileCallbackW(pageFiles *[]*pageFileData, enumPageFileInfo *enumPageFileInformation, lpFilenamePtr *[syscall.MAX_LONG_PATH]uint16) *bool { | ||
pageFileName := syscall.UTF16ToString((*lpFilenamePtr)[:]) | ||
|
||
pfData := &pageFileData{ | ||
name: pageFileName, | ||
used: enumPageFileInfo.totalInUse, | ||
total: enumPageFileInfo.totalSize, | ||
} | ||
|
||
*pageFiles = append(*pageFiles, pfData) | ||
|
||
// return true to continue enumerating page files | ||
ret := true | ||
return &ret | ||
} |
Oops, something went wrong.