-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into otel-bump-v0.102.1
- Loading branch information
Showing
13 changed files
with
332 additions
and
38 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,102 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
//go:build windows | ||
// +build windows | ||
|
||
package windows_event_log | ||
|
||
import ( | ||
"errors" | ||
"log" | ||
"time" | ||
|
||
"golang.org/x/sys/windows/svc" | ||
"golang.org/x/sys/windows/svc/mgr" | ||
) | ||
|
||
const ( | ||
serviceCheckInterval = 10 * time.Second | ||
|
||
serviceName = "eventlog" | ||
) | ||
|
||
var ( | ||
errServiceNotRunning = errors.New("service is not running") | ||
) | ||
|
||
type statusChecker interface { | ||
Query() (svc.Status, error) | ||
} | ||
|
||
type serviceMonitor struct { | ||
listeners []chan struct{} | ||
done chan struct{} | ||
} | ||
|
||
func newServiceMonitor() *serviceMonitor { | ||
return &serviceMonitor{ | ||
listeners: []chan struct{}{}, | ||
} | ||
} | ||
|
||
func (m *serviceMonitor) start() { | ||
manager, err := mgr.Connect() | ||
if err != nil { | ||
log.Printf("E! [windows_event_log] Unable to connect to Windows service manager: %v", err) | ||
return | ||
} | ||
|
||
service, err := manager.OpenService(serviceName) | ||
if err != nil { | ||
log.Printf("E! [windows_event_log] Unable to observe Windows event log service: %v", err) | ||
return | ||
} | ||
|
||
ticker := time.NewTicker(serviceCheckInterval) | ||
defer ticker.Stop() | ||
|
||
// get initial service PID | ||
oldPID, _ := getPID(service) | ||
for { | ||
select { | ||
case <-ticker.C: | ||
newPID, err := getPID(service) | ||
if err == nil && oldPID != newPID { | ||
log.Printf("D! [windows_event_log] Detected Windows event log service restart") | ||
oldPID = newPID | ||
m.notify() | ||
} | ||
case <-m.done: | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (m *serviceMonitor) stop() { | ||
close(m.done) | ||
} | ||
|
||
func (m *serviceMonitor) addListener(listener chan struct{}) { | ||
m.listeners = append(m.listeners, listener) | ||
} | ||
|
||
func (m *serviceMonitor) notify() { | ||
for _, l := range m.listeners { | ||
select { | ||
case l <- struct{}{}: | ||
default: | ||
} | ||
} | ||
} | ||
|
||
func getPID(service statusChecker) (uint32, error) { | ||
status, err := service.Query() | ||
if err != nil { | ||
return 0, err | ||
} | ||
if status.State == svc.Running { | ||
return status.ProcessId, nil | ||
} | ||
return 0, errServiceNotRunning | ||
} |
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,62 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
//go:build windows | ||
// +build windows | ||
|
||
package windows_event_log | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"golang.org/x/sys/windows/svc" | ||
) | ||
|
||
type mockStatusCheck struct { | ||
status svc.Status | ||
err error | ||
} | ||
|
||
func (m *mockStatusCheck) Query() (svc.Status, error) { | ||
return m.status, m.err | ||
} | ||
|
||
func TestGetPID(t *testing.T) { | ||
testErr := errors.New("test error") | ||
testCases := map[string]struct { | ||
status svc.Status | ||
err error | ||
wantPID uint32 | ||
wantErr error | ||
}{ | ||
"WithQueryError": { | ||
err: testErr, | ||
wantPID: 0, | ||
wantErr: testErr, | ||
}, | ||
"WithStoppedService": { | ||
status: svc.Status{ | ||
State: svc.Stopped, | ||
ProcessId: 0, | ||
}, | ||
wantPID: 0, | ||
wantErr: errServiceNotRunning, | ||
}, | ||
"WithRunningService": { | ||
status: svc.Status{ | ||
State: svc.Running, | ||
ProcessId: 123, | ||
}, | ||
wantPID: 123, | ||
}, | ||
} | ||
for name, testCase := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
gotPID, gotErr := getPID(&mockStatusCheck{status: testCase.status, err: testCase.err}) | ||
assert.Equal(t, testCase.wantPID, gotPID) | ||
assert.Equal(t, testCase.wantErr, gotErr) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.