feat: Tolerate collector failures (#1769)

Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
This commit is contained in:
Jan-Otto Kröpke
2024-11-25 21:27:31 +01:00
committed by GitHub
parent fd76be38e0
commit 1a4c6c5ce7
121 changed files with 1726 additions and 1221 deletions

View File

@@ -23,6 +23,7 @@ import (
"golang.org/x/sys/windows"
)
//nolint:gochecknoglobals
var (
modiphlpapi = windows.NewLazySystemDLL("iphlpapi.dll")
procGetExtendedTcpTable = modiphlpapi.NewProc("GetExtendedTcpTable")

View File

@@ -21,6 +21,7 @@ import (
"golang.org/x/sys/windows"
)
//nolint:gochecknoglobals
var (
kernel32 = windows.NewLazySystemDLL("kernel32.dll")

View File

@@ -45,6 +45,7 @@ type WorkstationInfo struct {
LoggedOnUsers uint32
}
//nolint:gochecknoglobals
var (
netapi32 = windows.NewLazySystemDLL("netapi32")
procNetWkstaGetInfo = netapi32.NewProc("NetWkstaGetInfo")
@@ -53,6 +54,8 @@ var (
// NetApiStatus is a map of Network Management Error Codes.
// https://docs.microsoft.com/en-gb/windows/win32/netmgmt/network-management-error-codes?redirectedfrom=MSDN
//
//nolint:gochecknoglobals
var NetApiStatus = map[uint32]string{
// Success
0: "NERR_Success",

View File

@@ -40,6 +40,7 @@ type PerformanceInformation struct {
ThreadCount uint32
}
//nolint:gochecknoglobals
var (
psapi = windows.NewLazySystemDLL("psapi.dll")
procGetPerformanceInfo = psapi.NewProc("GetPerformanceInfo")

View File

@@ -0,0 +1,88 @@
// Copyright 2024 The Prometheus 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 schedule_service
import (
"errors"
"fmt"
"runtime"
"github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
)
type ScheduleService struct {
taskSchedulerObj *ole.IUnknown
taskServiceObj *ole.IDispatch
taskService *ole.VARIANT
}
func New() *ScheduleService {
return &ScheduleService{}
}
func (s *ScheduleService) Connect() error {
// The only way to run WMI queries in parallel while being thread-safe is to
// ensure the CoInitialize[Ex]() call is bound to its current OS thread.
// Otherwise, attempting to initialize and run parallel queries across
// goroutines will result in protected memory errors.
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if err := ole.CoInitializeEx(0, ole.COINIT_MULTITHREADED|ole.COINIT_DISABLE_OLE1DDE); err != nil {
var oleCode *ole.OleError
if errors.As(err, &oleCode) && oleCode.Code() != ole.S_OK && oleCode.Code() != 0x00000001 {
return err
}
}
scheduleClassID, err := ole.ClassIDFrom("Schedule.Service")
if err != nil {
return err
}
s.taskSchedulerObj, err = ole.CreateInstance(scheduleClassID, nil)
if err != nil || s.taskSchedulerObj == nil {
return err
}
s.taskServiceObj = s.taskSchedulerObj.MustQueryInterface(ole.IID_IDispatch)
s.taskService, err = oleutil.CallMethod(s.taskServiceObj, "Connect")
if err != nil {
return fmt.Errorf("failed to connect to task service: %w", err)
}
return nil
}
func (s *ScheduleService) GetOLETaskServiceObj() *ole.IDispatch {
return s.taskServiceObj
}
func (s *ScheduleService) Close() {
if s.taskService != nil {
_ = s.taskService.Clear()
}
if s.taskServiceObj != nil {
s.taskServiceObj.Release()
}
if s.taskSchedulerObj != nil {
s.taskSchedulerObj.Release()
}
ole.CoUninitialize()
}

View File

@@ -25,7 +25,8 @@ import (
)
// based on https://github.com/carlpett/winlsa/blob/master/winlsa.go
//
//nolint:gochecknoglobals
var (
secur32 = windows.NewLazySystemDLL("Secur32.dll")
advapi32 = windows.NewLazySystemDLL("advapi32.dll")

View File

@@ -22,6 +22,7 @@ import (
"golang.org/x/sys/windows"
)
//nolint:gochecknoglobals
var (
slc = windows.NewLazySystemDLL("slc.dll")
procSLIsWindowsGenuineLocal = slc.NewProc("SLIsWindowsGenuineLocal")

View File

@@ -113,6 +113,7 @@ const (
ComputerNameMax
)
//nolint:gochecknoglobals
var (
kernel32 = windows.NewLazySystemDLL("kernel32.dll")
procGetSystemInfo = kernel32.NewProc("GetSystemInfo")

View File

@@ -97,6 +97,7 @@ type WTSSession struct {
FarmName string
}
//nolint:gochecknoglobals
var (
wtsapi32 = windows.NewLazySystemDLL("wtsapi32.dll")