-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable to Collector to be run as a Windows service
- Loading branch information
1 parent
8b9fe07
commit 9028089
Showing
7 changed files
with
253 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// 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 main | ||
|
||
import "log" | ||
|
||
func main() { | ||
params, err := applicationParameters() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
runInteractive(params) | ||
} |
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,51 @@ | ||
// 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 main | ||
|
||
import ( | ||
"log" | ||
|
||
"golang.org/x/sys/windows/svc" | ||
|
||
"go.opentelemetry.io/collector/service" | ||
) | ||
|
||
func main() { | ||
params, err := applicationParameters() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
isInteractive, err := svc.IsAnInteractiveSession() | ||
if err != nil { | ||
log.Fatalf("failed to determine if we are running in an interactive session: %v", err) | ||
} | ||
|
||
if isInteractive { | ||
runInteractive(params) | ||
} else { | ||
runService(params) | ||
} | ||
} | ||
|
||
func runService(params service.Parameters) { | ||
// do not need to supply service name when startup is invoked through Service Control Manager directly | ||
err := svc.Run("", service.NewWindowsService(params)) | ||
if err != nil { | ||
log.Fatalf("failed to start service: %v", err) | ||
} | ||
} |
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,133 @@ | ||
// 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 service | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"syscall" | ||
|
||
"go.uber.org/zap/zapcore" | ||
"golang.org/x/sys/windows/svc" | ||
"golang.org/x/sys/windows/svc/eventlog" | ||
) | ||
|
||
type WindowsService struct { | ||
app *Application | ||
params Parameters | ||
} | ||
|
||
func NewWindowsService(params Parameters) *WindowsService { | ||
return &WindowsService{params: params} | ||
} | ||
|
||
func (s *WindowsService) Execute(args []string, requests <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) { | ||
if len(args) == 0 { | ||
log.Fatal("expected first argument supplied to service.Execute to be the service name") | ||
} | ||
|
||
changes <- svc.Status{State: svc.StartPending} | ||
s.start(args[0], s.params) | ||
changes <- svc.Status{State: svc.Running, Accepts: svc.AcceptStop | svc.AcceptShutdown} | ||
|
||
for req := range requests { | ||
switch req.Cmd { | ||
case svc.Interrogate: | ||
changes <- req.CurrentStatus | ||
case svc.Stop, svc.Shutdown: | ||
s.stop() | ||
return | ||
default: | ||
log.Fatalf(fmt.Sprintf("unexpected control request #%d", req)) | ||
} | ||
} | ||
|
||
return | ||
} | ||
|
||
func (s *WindowsService) start(logSourceName string, params Parameters) { | ||
var err error | ||
s.app, err = newWithEventViewerLoggingHook(logSourceName, params) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// app.Start blocks until receiving a SIGTERM signal, so we need to start it asynchronously | ||
go func() { | ||
err = s.app.Start() | ||
if err != nil { | ||
log.Fatalf("application run finished with error: %v", err) | ||
} | ||
}() | ||
|
||
// wait until the app is in the Running state | ||
for state := range s.app.GetStateChannel() { | ||
if state == Running { | ||
break | ||
} | ||
} | ||
} | ||
|
||
func (s *WindowsService) stop() { | ||
// simulate a SIGTERM signal to terminate the application | ||
s.app.signalsChannel <- syscall.SIGTERM | ||
|
||
// wait until the app is in the Closed state | ||
for state := range s.app.GetStateChannel() { | ||
if state == Closed { | ||
break | ||
} | ||
} | ||
|
||
s.app = nil | ||
} | ||
|
||
func newWithEventViewerLoggingHook(serviceName string, params Parameters) (*Application, error) { | ||
elog, err := eventlog.Open(serviceName) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to open event log: %v", err) | ||
} | ||
|
||
params.LoggingHooks = append( | ||
params.LoggingHooks, | ||
func(entry zapcore.Entry) error { | ||
msg := fmt.Sprintf("%v\r\n\r\nStack Trace:\r\n%v", entry.Message, entry.Stack) | ||
|
||
switch entry.Level { | ||
case zapcore.FatalLevel, zapcore.PanicLevel, zapcore.DPanicLevel: | ||
// golang.org/x/sys/windows/svc/eventlog does not support Critical level event logs | ||
return elog.Error(3, msg) | ||
case zapcore.ErrorLevel: | ||
return elog.Error(3, msg) | ||
case zapcore.WarnLevel: | ||
return elog.Warning(2, msg) | ||
case zapcore.InfoLevel: | ||
return elog.Info(1, msg) | ||
} | ||
|
||
// ignore Debug level logs | ||
return nil | ||
}, | ||
) | ||
|
||
app, err := New(params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return app, nil | ||
} |