Skip to content

Commit

Permalink
feat: support windows service registration
Browse files Browse the repository at this point in the history
  • Loading branch information
fgouteroux committed Jul 22, 2024
1 parent 1e15a5b commit f0e9201
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 15 deletions.
4 changes: 3 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ issues:

linters-settings:
errcheck:
exclude: scripts/errcheck_excludes.txt
exclude-functions:
- (net/http.ResponseWriter).Write
- (github.com/go-kit/log.Logger).Log
1 change: 1 addition & 0 deletions .promu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ repository:
build:
binaries:
- name: promk
path: ./cmd/promk
ldflags: |
-X github.com/prometheus/common/version.Version={{.Version}}
-X github.com/prometheus/common/version.Revision={{.Revision}}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/prometheus/client_model v0.6.1
github.com/prometheus/common v0.55.0
github.com/prometheus/prometheus v0.52.1
golang.org/x/sys v0.21.0
)

require (
Expand Down Expand Up @@ -56,7 +57,6 @@ require (
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be // indirect
Expand Down
2 changes: 1 addition & 1 deletion client.go → pkg/pusher/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package pusher

import (
"crypto/tls"
Expand Down
2 changes: 1 addition & 1 deletion collect_push.go → pkg/pusher/collect_push.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package pusher

import (
"context"
Expand Down
35 changes: 28 additions & 7 deletions main.go → pkg/pusher/pusher.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package pusher

import (
"os"
Expand All @@ -7,14 +7,25 @@ import (

"github.com/alecthomas/kingpin/v2"

"github.com/go-kit/log"
"github.com/go-kit/log/level"

"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/version"

"github.com/prometheus/prometheus/storage/remote"
)

func main() {
type Pusher struct {
logger log.Logger
client *remote.Client
interval time.Duration
jobLabel string
labels map[string]string
}

func Setup() (p *Pusher) {
app := kingpin.New(filepath.Base(os.Args[0]), "Prometheus Keepalive Agent.").UsageWriter(os.Stdout)
baseURL := app.Flag("remote-write-url", "Prometheus remote-write url").Envar("PROMK_URL").Required().URL()
username := app.Flag("basic-auth.username", "Prometheus remote-write username").Envar("PROMK_USERNAME").String()
Expand Down Expand Up @@ -52,17 +63,27 @@ func main() {
os.Exit(1)
}

return &Pusher{
logger: logger,
client: remoteWriteClient,
interval: *pushInterval,
jobLabel: *jobLabel,
labels: *labels,
}
}

func (p *Pusher) Run() {
// create a new Ticker
tk := time.NewTicker(*pushInterval)
tk := time.NewTicker(p.interval)

// start the ticker
for range tk.C {
metric := CollectAndEncode(logger, *jobLabel, *labels)
err := Push(remoteWriteClient, metric)
metric := CollectAndEncode(p.logger, p.jobLabel, p.labels)
err := Push(p.client, metric)
if err != nil {
level.Error(logger).Log("msg", "Could not push to the remote write", "err", err) // #nosec G104
level.Error(p.logger).Log("msg", "Could not push to the remote write", "err", err) // #nosec G104
} else {
level.Info(logger).Log("msg", "Successful push to the remote write") // #nosec G104
level.Info(p.logger).Log("msg", "Successful push to the remote write") // #nosec G104
}
}
}
45 changes: 45 additions & 0 deletions pkg/windows/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//go:build windows
// +build windows

package windows

import (
"fmt"
"log"

"golang.org/x/sys/windows/svc"
)

// WindowsExporterService channel for service stop
type WindowsExporterService struct {
stopCh chan<- bool
}

// NewWindowsExporterService return new WindowsExporterService
func NewWindowsExporterService(ch chan<- bool) *WindowsExporterService {
return &WindowsExporterService{stopCh: ch}
}

// Execute run programm directly or for service
func (s *WindowsExporterService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
changes <- svc.Status{State: svc.StartPending}
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
loop:
for {
select {
case c := <-r:
switch c.Cmd {
case svc.Interrogate:
changes <- c.CurrentStatus
case svc.Stop, svc.Shutdown:
s.stopCh <- true
break loop
default:
log.Fatalf(fmt.Sprintf("unexpected control request #%d", c))
}
}
}
changes <- svc.Status{State: svc.StopPending}
return
}
4 changes: 0 additions & 4 deletions scripts/errcheck_excludes.txt

This file was deleted.

0 comments on commit f0e9201

Please sign in to comment.