Skip to content

Commit

Permalink
Merge pull request #42 from smlx/refactor-metrics
Browse files Browse the repository at this point in the history
refactor metrics
  • Loading branch information
smlx authored Feb 19, 2024
2 parents a90e83d + ca0623d commit fea8e5f
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 28 deletions.
12 changes: 12 additions & 0 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@ updates:
directory: /
schedule:
interval: weekly
groups:
github-actions:
patterns:
- "*"
- package-ecosystem: docker
directory: /
schedule:
interval: daily
groups:
docker:
patterns:
- "*"
- package-ecosystem: gomod
directory: /
schedule:
interval: daily
groups:
gomod:
patterns:
- "*"
10 changes: 1 addition & 9 deletions .github/workflows/dependabot-automerge.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,8 @@ jobs:
uses: dependabot/fetch-metadata@c9c4182bf1b97f5224aee3906fd373f6b61b4526 # v1.6.0
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"
- name: Auto-merge Dependabot PRs
# don't auto-merge action updates to appease OpenSSF scorecard
if: ${{ ! contains(steps.metadata.outputs.package-ecosystem, 'github-actions') }}
- name: Enable auto-merge for Dependabot PRs # these still need approval before merge
run: gh pr merge --auto --merge "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
- name: Auto-approve well-maintained dependencies
if: contains(steps.metadata.outputs.dependency-names, 'k8s.io/client-go')
run: gh pr review --approve "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
go-version: stable
- uses: golangci/golangci-lint-action@3cfe3a4abbb849e10058ce4af15d205b6da42804 # v4.0.0
with:
args: --timeout=180s
args: --timeout=180s --enable gocritic
lint-commits:
permissions:
contents: read
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ build:

.PHONY: lint
lint:
golangci-lint run
golangci-lint run --enable gocritic

.PHONY: fuzz
fuzz: mod-tidy generate
Expand Down
34 changes: 20 additions & 14 deletions cmd/sems_mitm_exporter/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
)

const (
metricsPort = ":14028"
metricsReadTimeout = 2 * time.Second
metricsPort = ":14028"
metricsReadTimeout = 2 * time.Second
metricsShutdownTimeout = 2 * time.Second
)

// ServeCmd represents the `serve` command.
Expand All @@ -25,14 +26,7 @@ type ServeCmd struct {
SEMSPassthrough bool `kong:"env='SEMS_PASSTHROUGH',default='true',help='Enable passthrough to SEMS Portal'"`
}

// Run the serve command.
func (cmd *ServeCmd) Run(log *slog.Logger) error {
// handle signals
ctx, stop := signal.NotifyContext(
context.Background(),
syscall.SIGTERM,
syscall.SIGINT)
defer stop()
func serveMetrics(ctx context.Context, eg *errgroup.Group) {
// configure metrics server
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
Expand All @@ -42,9 +36,6 @@ func (cmd *ServeCmd) Run(log *slog.Logger) error {
WriteTimeout: metricsReadTimeout,
Handler: mux,
}
// set up multithreading
var eg *errgroup.Group
eg, ctx = errgroup.WithContext(ctx)
// start metrics server
eg.Go(func() error {
if err := metricsSrv.ListenAndServe(); err != http.ErrServerClosed {
Expand All @@ -55,10 +46,25 @@ func (cmd *ServeCmd) Run(log *slog.Logger) error {
// start metrics server shutdown handler for graceful shutdown
eg.Go(func() error {
<-ctx.Done()
timeoutCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
timeoutCtx, cancel :=
context.WithTimeout(context.Background(), metricsShutdownTimeout)
defer cancel()
return metricsSrv.Shutdown(timeoutCtx)
})
}

// Run the serve command.
func (cmd *ServeCmd) Run(log *slog.Logger) error {
// handle signals
ctx, stop := signal.NotifyContext(
context.Background(),
syscall.SIGTERM,
syscall.SIGINT)
defer stop()
// set up multithreading
eg, ctx := errgroup.WithContext(ctx)
// start metrics server
serveMetrics(ctx, eg)
// start mitm server
if cmd.SEMSPassthrough {
eg.Go(func() error {
Expand Down
3 changes: 2 additions & 1 deletion mitm/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ func (h *OutboundPacketHandler) HandlePacket(
return nil, fmt.Errorf("couldn't signal batman: %v", err)
}
var fullPacket []byte
fullPacket = append(headerData, newBodyData...)
fullPacket = append(fullPacket, headerData...)
fullPacket = append(fullPacket, newBodyData...)
fullPacket =
outboundCRCByteOrder.AppendUint16(fullPacket, goodwe.CRC(fullPacket))
return fullPacket, nil
Expand Down
4 changes: 2 additions & 2 deletions mitm/outboundmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestUnmarshalMeterMetrics(t *testing.T) {
}{
"metrics": {
input: []byte{
//0x50, 0x4f, 0x53, 0x54, 0x47, 0x57, 0x00, 0x00, 0x00, 0x99, 0x03, 0x04,
// 0x50, 0x4f, 0x53, 0x54, 0x47, 0x57, 0x00, 0x00, 0x00, 0x99, 0x03, 0x04,
0x00, 0x00, 0x39, 0x31,
0x30, 0x30, 0x30, 0x48, 0x4b, 0x55, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x17, 0x09,
0x12, 0x09, 0x09, 0x1b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x09,
Expand All @@ -25,7 +25,7 @@ func TestUnmarshalMeterMetrics(t *testing.T) {
0x0e, 0x52, 0xb3, 0xcc, 0x9f, 0x0a, 0xaf, 0xef, 0x6d, 0x28, 0xce, 0xad, 0x52, 0xe7, 0x9f, 0x7f,
0x9b, 0xe3, 0x3c, 0xa0, 0x1b, 0x22, 0xc9, 0x59, 0x33, 0x04, 0xf2, 0x39, 0x8d, 0xd1, 0x20, 0xfc,
0x88, 0xaa, 0x1d, 0x99,
// 0x4b, 0xcd,
// 0x4b, 0xcd,
},
metrics: OutboundMeterMetricsPacket{
OutboundEnvelopeTS: OutboundEnvelopeTS{
Expand Down

0 comments on commit fea8e5f

Please sign in to comment.