Skip to content

Commit

Permalink
arg parse, grace shutdown, concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
ezrizhu committed Jul 1, 2024
1 parent 0a12735 commit 4148344
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 12 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: build

on:
push:
branches: [ "main" ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.REGISTRY_TOKEN }}

- name: Build and Push Docker Image
uses: docker/build-push-action@v4
with:
push: true
tags: |
ghcr.io/PEERINGTestbed/peeringmon_exporter:${{ github.sha }}
ghcr.io/PEERINGTestbed/peeringmon_exporter:latest
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM golang:1.22

WORKDIR /app

COPY . .

RUN go mod tidy
RUN go build -o /peeringmon_exporter

EXPOSE 2112

CMD [ "/peeringmon_exporter" ]
59 changes: 47 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package main

import (
"context"
"encoding/json"
"errors"
"flag"
"io/ioutil"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"time"

"github.com/rs/zerolog"
Expand All @@ -17,11 +23,6 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
)

const (
appID = "PEERINGMON-DEV"
port = ":2112"
)

var prefixes = []string{
// PEERING v6
"2804:269c:fe41::/48",
Expand Down Expand Up @@ -76,6 +77,9 @@ var prefixes = []string{

var prefixStates = []*PrefixState{}

var port int
var appId string

var (
prefixStateGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "prefix_visibility",
Expand All @@ -91,7 +95,7 @@ type PrefixState struct {

func (p *PrefixState) checkState() {
log.Trace().Str("Prefix", p.Prefix).Msg("checking prefix state")
url := "https://stat.ripe.net/data/visibility/data.json?data_overload_limit=ignore&include=peers_seeing&resource=" + p.Prefix + "&sourceapp=" + appID
url := "https://stat.ripe.net/data/visibility/data.json?data_overload_limit=ignore&include=peers_seeing&resource=" + p.Prefix + "&sourceapp=" + appId
resp, err := http.Get(url)
if err != nil {
log.Error().Err(err).Msg("Fetching ripestat")
Expand Down Expand Up @@ -129,15 +133,26 @@ func (p *PrefixState) checkState() {
func updateStates() {
log.Debug().Msg("Updating Prefixes")
for _, ps := range prefixStates {
ps.checkState()
go ps.checkState()
}
}

func main() {
func init() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout})
zerolog.SetGlobalLevel(zerolog.DebugLevel)
log.Info().Msg("Starting PEERINGMON Exporter")

flag.StringVar(&appId, "appid", "exporter", "provide a unique identifier to every data call")
flag.IntVar(&port, "port", 2112, "port")
}

func main() {
flag.Parse()

log.Info().
Str("appID", appId).
Str("Data Source", "RIPE RIS via RIPEstat API").
Msg("Starting PEERINGMON Exporter")

for _, prefix := range prefixes {
prefixStates = append(prefixStates, &PrefixState{
Expand All @@ -157,9 +172,29 @@ func main() {
}
}()

log.Info().Msg("Starting exporter on port " + port)
http.Handle("/metrics", promhttp.Handler())
if err := http.ListenAndServe(port, nil); err != nil {
log.Error().Err(err).Msg("Failed on http listening")

done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

srv := &http.Server{
Addr: ":" + strconv.Itoa(port),
}

go func() {
if err := srv.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
log.Fatal().Err(err).Msg("Failed to start HTTP server")
}
}()
log.Info().Int("port", port).Msg("Started exporter")

<-done
log.Info().Msg("Stopping")
shutdownCtx, shutdownRelease := context.WithTimeout(context.Background(), 10*time.Second)
defer shutdownRelease()

if err := srv.Shutdown(shutdownCtx); err != nil {
log.Fatal().Err(err).Msg("Failed to gracefully stop server")
}
log.Info().Msg("Graceful Shutdown Successful, bye")
}
Binary file added peeringmon_exporter
Binary file not shown.

0 comments on commit 4148344

Please sign in to comment.