| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package otelcol |
| 7 |
|
| 8 |
import ( |
| 9 |
"context" |
| 10 |
"flag" |
| 11 |
"fmt" |
| 12 |
"os" |
| 13 |
"time" |
| 14 |
|
| 15 |
"go.uber.org/zap" |
| 16 |
"go.uber.org/zap/zapcore" |
| 17 |
"golang.org/x/sys/windows/svc" |
| 18 |
"golang.org/x/sys/windows/svc/eventlog" |
| 19 |
|
| 20 |
"go.opentelemetry.io/collector/featuregate" |
| 21 |
"go.opentelemetry.io/collector/service" |
| 22 |
"go.opentelemetry.io/collector/service/telemetry/otelconftelemetry" |
| 23 |
) |
| 24 |
|
| 25 |
type windowsService struct { |
| 26 |
settings CollectorSettings |
| 27 |
col *Collector |
| 28 |
flags *flag.FlagSet |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
func NewSvcHandler(set CollectorSettings) svc.Handler { |
| 33 |
return &windowsService{settings: set, flags: flags(featuregate.GlobalRegistry())} |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
func (s *windowsService) Execute(args []string, requests <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) { |
| 38 |
|
| 39 |
|
| 40 |
if len(args) == 0 { |
| 41 |
return false, 1213 |
| 42 |
} |
| 43 |
|
| 44 |
elog, err := openEventLog(args[0]) |
| 45 |
if err != nil { |
| 46 |
return false, 1501 |
| 47 |
} |
| 48 |
|
| 49 |
colErrorChannel := make(chan error, 1) |
| 50 |
|
| 51 |
changes <- svc.Status{State: svc.StartPending} |
| 52 |
if err = s.start(elog, colErrorChannel); err != nil { |
| 53 |
_ = elog.Error(3, fmt.Sprintf("failed to start service: %v", err)) |
| 54 |
return false, 1064 |
| 55 |
} |
| 56 |
changes <- svc.Status{State: svc.Running, Accepts: svc.AcceptStop | svc.AcceptShutdown} |
| 57 |
|
| 58 |
for req := range requests { |
| 59 |
switch req.Cmd { |
| 60 |
case svc.Interrogate: |
| 61 |
changes <- req.CurrentStatus |
| 62 |
|
| 63 |
case svc.Stop, svc.Shutdown: |
| 64 |
changes <- svc.Status{State: svc.StopPending} |
| 65 |
if err = s.stop(colErrorChannel); err != nil { |
| 66 |
_ = elog.Error(3, fmt.Sprintf("errors occurred while shutting down the service: %v", err)) |
| 67 |
} |
| 68 |
changes <- svc.Status{State: svc.Stopped} |
| 69 |
return false, 0 |
| 70 |
|
| 71 |
default: |
| 72 |
_ = elog.Error(3, fmt.Sprintf("unexpected service control request #%d", req.Cmd)) |
| 73 |
return false, 1052 |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
return false, 0 |
| 78 |
} |
| 79 |
|
| 80 |
func (s *windowsService) start(elog *eventlog.Log, colErrorChannel chan error) error { |
| 81 |
|
| 82 |
if err := s.flags.Parse(os.Args[1:]); err != nil { |
| 83 |
return err |
| 84 |
} |
| 85 |
|
| 86 |
var err error |
| 87 |
err = updateSettingsUsingFlags(&s.settings, s.flags) |
| 88 |
if err != nil { |
| 89 |
return err |
| 90 |
} |
| 91 |
s.col, err = NewCollector(s.settings) |
| 92 |
if err != nil { |
| 93 |
return err |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
s.col.set.LoggingOptions = loggingOptionsWithEventLogCore(elog, &s.col.serviceConfig, s.col.set.LoggingOptions) |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
go func() { |
| 108 |
colErrorChannel <- s.col.Run(context.Background()) |
| 109 |
}() |
| 110 |
|
| 111 |
|
| 112 |
go func() { |
| 113 |
for { |
| 114 |
state := s.col.GetState() |
| 115 |
if state == StateRunning { |
| 116 |
colErrorChannel <- nil |
| 117 |
break |
| 118 |
} |
| 119 |
time.Sleep(time.Millisecond * 200) |
| 120 |
} |
| 121 |
}() |
| 122 |
|
| 123 |
|
| 124 |
return <-colErrorChannel |
| 125 |
} |
| 126 |
|
| 127 |
func (s *windowsService) stop(colErrorChannel chan error) error { |
| 128 |
s.col.Shutdown() |
| 129 |
|
| 130 |
return <-colErrorChannel |
| 131 |
} |
| 132 |
|
| 133 |
func openEventLog(serviceName string) (*eventlog.Log, error) { |
| 134 |
elog, err := eventlog.Open(serviceName) |
| 135 |
if err != nil { |
| 136 |
return nil, fmt.Errorf("service failed to open event log: %w", err) |
| 137 |
} |
| 138 |
|
| 139 |
return elog, nil |
| 140 |
} |
| 141 |
|
| 142 |
func loggingOptionsWithEventLogCore( |
| 143 |
elog *eventlog.Log, |
| 144 |
serviceConfig **service.Config, |
| 145 |
userOptions []zap.Option, |
| 146 |
) []zap.Option { |
| 147 |
return append( |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
[]zap.Option{zap.WrapCore(withWindowsCore(elog, serviceConfig))}, |
| 152 |
userOptions..., |
| 153 |
) |
| 154 |
} |
| 155 |
|
| 156 |
var _ zapcore.Core = (*windowsEventLogCore)(nil) |
| 157 |
|
| 158 |
type windowsEventLogCore struct { |
| 159 |
core zapcore.Core |
| 160 |
elog *eventlog.Log |
| 161 |
encoder zapcore.Encoder |
| 162 |
} |
| 163 |
|
| 164 |
func (w windowsEventLogCore) Enabled(level zapcore.Level) bool { |
| 165 |
return w.core.Enabled(level) |
| 166 |
} |
| 167 |
|
| 168 |
func (w windowsEventLogCore) With(fields []zapcore.Field) zapcore.Core { |
| 169 |
enc := w.encoder.Clone() |
| 170 |
for _, field := range fields { |
| 171 |
field.AddTo(enc) |
| 172 |
} |
| 173 |
return windowsEventLogCore{ |
| 174 |
core: w.core, |
| 175 |
elog: w.elog, |
| 176 |
encoder: enc, |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
func (w windowsEventLogCore) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry { |
| 181 |
if w.Enabled(ent.Level) { |
| 182 |
return ce.AddCore(ent, w) |
| 183 |
} |
| 184 |
return ce |
| 185 |
} |
| 186 |
|
| 187 |
func (w windowsEventLogCore) Write(ent zapcore.Entry, fields []zapcore.Field) error { |
| 188 |
buf, err := w.encoder.EncodeEntry(ent, fields) |
| 189 |
if err != nil { |
| 190 |
_ = w.elog.Warning(2, fmt.Sprintf("failed encoding log entry %v\r\n", err)) |
| 191 |
return err |
| 192 |
} |
| 193 |
msg := buf.String() |
| 194 |
buf.Free() |
| 195 |
|
| 196 |
switch ent.Level { |
| 197 |
case zapcore.FatalLevel, zapcore.PanicLevel, zapcore.DPanicLevel: |
| 198 |
|
| 199 |
return w.elog.Error(3, msg) |
| 200 |
case zapcore.ErrorLevel: |
| 201 |
return w.elog.Error(3, msg) |
| 202 |
case zapcore.WarnLevel: |
| 203 |
return w.elog.Warning(2, msg) |
| 204 |
case zapcore.InfoLevel: |
| 205 |
return w.elog.Info(1, msg) |
| 206 |
} |
| 207 |
|
| 208 |
return w.elog.Info(1, msg) |
| 209 |
} |
| 210 |
|
| 211 |
func (w windowsEventLogCore) Sync() error { |
| 212 |
return w.core.Sync() |
| 213 |
} |
| 214 |
|
| 215 |
func withWindowsCore(elog *eventlog.Log, serviceConfig **service.Config) func(zapcore.Core) zapcore.Core { |
| 216 |
return func(core zapcore.Core) zapcore.Core { |
| 217 |
if serviceConfig != nil && *serviceConfig != nil { |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
for _, output := range (*serviceConfig).Telemetry.(*otelconftelemetry.Config).Logs.OutputPaths { |
| 222 |
if output != "stdout" && output != "stderr" { |
| 223 |
|
| 224 |
return core |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
encoderConfig := zap.NewProductionEncoderConfig() |
| 231 |
encoderConfig.LineEnding = "\r\n" |
| 232 |
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder |
| 233 |
return windowsEventLogCore{core, elog, zapcore.NewConsoleEncoder(encoderConfig)} |
| 234 |
} |
| 235 |
} |