Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Http std lib mux #76

Merged
merged 5 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/build-push-staging.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 'build & push docker staging'
name: "build & push docker staging"

on:
pull_request_target:
Expand All @@ -23,7 +23,7 @@ jobs:
- name: Setup golang environment
uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: "1.22"

- name: Install dependencies
run: go mod tidy
Expand Down Expand Up @@ -55,4 +55,4 @@ jobs:
VERSION=${{ env.REPO_VERSION }}
REPO_REV=${{ env.REPO_REV }}
DATE=${{ env.BUILD_DATE }}
tags: ${{ secrets.DOCKER_ENDPOINT }}${{ secrets.DOCKER_REPO }}/fishymetrics:${{ env.REPO_VERSION }}
tags: ${{ secrets.DOCKER_ENDPOINT }}${{ secrets.DOCKER_REPO }}/fishymetrics:${{ env.REPO_VERSION }}
19 changes: 9 additions & 10 deletions .github/workflows/pr-prechecks.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 'prechecks'
name: "prechecks"

on:
workflow_dispatch:
Expand All @@ -15,7 +15,7 @@ jobs:
- name: Setup golang environment
uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: "1.22"

- name: Install dependencies
run: go mod tidy
Expand All @@ -25,12 +25,12 @@ jobs:

- name: Run Whitesource/Mend scan
run: |
curl -LJO https://github.com/whitesource/unified-agent-distribution/releases/latest/download/wss-unified-agent.jar
export WS_CHECHKPOLICIES=true
export WS_FORCECHECKALLDEPENDENCIES=true
export WS_FORCEUPDATE=true
export WS_UPDATEINVENTORY=true
java -jar wss-unified-agent.jar -apiKey ${{ secrets.MEND_API_KEY }} -product ${{ secrets.MEND_PRODUCT_NAME }} -project WS-fishymetrics
curl -LJO https://github.com/whitesource/unified-agent-distribution/releases/latest/download/wss-unified-agent.jar
export WS_CHECHKPOLICIES=true
export WS_FORCECHECKALLDEPENDENCIES=true
export WS_FORCEUPDATE=true
export WS_UPDATEINVENTORY=true
java -jar wss-unified-agent.jar -apiKey ${{ secrets.MEND_API_KEY }} -product ${{ secrets.MEND_PRODUCT_NAME }} -project WS-fishymetrics

build:
runs-on: ubuntu-latest
Expand All @@ -41,11 +41,10 @@ jobs:
- name: Setup golang environment
uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: "1.22"

- name: Install dependencies
run: go mod tidy

- name: Make sure app can build
run: make build

1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ log is based on the [Keep a CHANGELOG](http://keepachangelog.com/) project.
- consolidate hardware component structs to a single package [#45](https://github.com/Comcast/fishymetrics/issues/45)
- get chassis serial number from JSON response instead of url path [#50](https://github.com/Comcast/fishymetrics/issues/50)
- HP DL380 module to include CPU metrics and all HP models to include bayNumber in PSU metrics [#57](https://github.com/Comcast/fishymetrics/issues/57)
- use standard library for http routing instead of gorilla mux package [#47](https://github.com/Comcast/fishymetrics/issues/47)

## [0.7.1]

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# syntax=docker/dockerfile:1

FROM golang:1.21 as build
FROM golang:1.22 as build
COPY . /go/src/github.com/comcast/fishymetrics
WORKDIR /go/src/github.com/comcast/fishymetrics

Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,12 @@ dependency licenses for each package:

| package | license |
| --------------------------------------------- | ----------------------------- |
| `github.com/google/uuid` | `BSD-3-Clause license` |
| `github.com/gorilla/mux` | `BSD-3-Clause license` |
| `github.com/hashicorp/go-hclog` | `MIT license` |
| `github.com/hashicorp/go-retryablehttp` | `Mozilla Public License v2.0` |
| `github.com/hashicorp/vault/api` | `Mozilla Public License v2.0` |
| `github.com/hashicorp/vault/api/auth/approle` | `Mozilla Public License v2.0` |
| `github.com/hashicorp/vault/sdk` | `Mozilla Public License v2.0` |
| `github.com/nrednav/cuid2` | `MIT license` |
| `github.com/prometheus/client_golang` | `Apache-2.0 license` |
| `github.com/stretchr/testify` | `MIT license` |
| `go.uber.org/zap` | `MIT license` |
Expand Down
120 changes: 46 additions & 74 deletions cmd/fishymetrics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,19 @@ import (
"strings"
"sync"
"syscall"
"time"

"github.com/comcast/fishymetrics/buildinfo"
"github.com/comcast/fishymetrics/common"
"github.com/comcast/fishymetrics/config"
"github.com/comcast/fishymetrics/exporter"
"github.com/comcast/fishymetrics/exporter/moonshot"
"github.com/comcast/fishymetrics/logger"
"github.com/comcast/fishymetrics/middleware/logging"
"github.com/comcast/fishymetrics/middleware/muxprom"
"github.com/comcast/fishymetrics/plugins/nuova"
fishy_vault "github.com/comcast/fishymetrics/vault"
"go.uber.org/zap"

"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gopkg.in/alecthomas/kingpin.v2"
Expand Down Expand Up @@ -245,10 +243,26 @@ func main() {
VectorEndpoint: *vectorEndpoint,
}

logger.Initialize(app, hostname, logConfig)
err = logger.Initialize(app, hostname, logConfig)
if err != nil {
panic(fmt.Errorf("error initializing logger - log_method=%s vector_endpoint=%s log_file_path=%s log_file_max_size=%d log_file_max_backups=%d log_file_max_age=%d - err=%s",
*logMethod, *vectorEndpoint, *logFilePath, logfileMaxSize, logfileMaxBackups, logfileMaxAge, err.Error()))
}

log = zap.L()
defer logger.Flush()

if *logMethod == "vector" {
log.Info("successfully initialized logger", zap.String("log_method", *logMethod),
zap.String("vector_endpoint", *vectorEndpoint))
} else if *logMethod == "file" {
log.Info("successfully initialized logger", zap.String("log_method", *logMethod),
zap.String("log_file_path", *logFilePath),
zap.Int("log_file_max_size", logfileMaxSize),
zap.Int("log_file_max_backups", logfileMaxBackups),
zap.Int("log_file_max_age", logfileMaxAge))
}

// configure vault client if vaultRoleId & vaultSecretId are set
if *vaultRoleId != "" && *vaultSecretId != "" {
var err error
Expand All @@ -261,15 +275,17 @@ func main() {
},
)
if err != nil {
log.Error("failed initializing vault client", zap.Error(err))
}

// we add this here so we can update credentials once we detect they are rotated
common.ChassisCreds.Vault = vault
log.Error("failed initializing vault client", zap.Error(err),
zap.String("vault_address", *vaultAddr),
zap.String("vault_role_id", *vaultRoleId))
} else {
// we add this here so we can update credentials once we detect they are rotated
common.ChassisCreds.Vault = vault

// start go routine to continuously renew vault token
wg.Add(1)
go vault.RenewToken(ctx, doneRenew, tokenLifecycle, &wg)
// start go routine to continuously renew vault token
wg.Add(1)
go vault.RenewToken(ctx, doneRenew, tokenLifecycle, &wg)
}
}

config.NewConfig(&config.Config{
Expand All @@ -278,46 +294,46 @@ func main() {
Pass: *password,
})

mux := mux.NewRouter()
mux := http.NewServeMux()

instrumentation := muxprom.NewDefaultInstrumentation()
mux.Use(instrumentation.Middleware)

mux.HandleFunc("/info", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("GET /info", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(buildinfo.Info)
}).Methods("GET")
})

mux.Handle("/metrics", promhttp.Handler()).Methods("GET")
mux.Handle("GET /metrics", promhttp.Handler())

mux.HandleFunc("/scrape", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("GET /scrape", func(w http.ResponseWriter, r *http.Request) {
handler(ctx, w, r)
}).Methods("GET")
})

tmplIndex := template.Must(template.New("index").Parse(indexTmpl))
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
err := tmplIndex.Execute(w, buildinfo.Info)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}).Methods("GET")
})

tmplIgnored := template.Must(template.New("ignored").Parse(ignoredTmpl))
mux.HandleFunc("/ignored", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("GET /ignored", func(w http.ResponseWriter, r *http.Request) {
err := tmplIgnored.Execute(w, common.IgnoredDevices)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}).Methods("GET")
})

mux.HandleFunc("/ignored/test-conn", common.TestConn).Methods("POST")
mux.HandleFunc("/ignored/remove", common.RemoveHost).Methods("POST")
mux.HandleFunc("POST /ignored/test-conn", common.TestConn)
mux.HandleFunc("POST /ignored/remove", common.RemoveHost)

mux.HandleFunc("/verbosity", logger.Verbosity).Methods("GET")
mux.HandleFunc("/verbosity", logger.SetVerbosity).Methods("PUT")
mux.HandleFunc("GET /verbosity", logger.Verbosity)
mux.HandleFunc("PUT /verbosity", logger.SetVerbosity)

instrumentation := muxprom.NewDefaultInstrumentation()
wrappedmux := logging.LoggingHandler(instrumentation.Middleware(mux))

srv := &http.Server{
Addr: ":" + *exporterPort,
Handler: loggingHandler(mux),
Handler: wrappedmux,
}

signals := make(chan os.Signal, 1)
Expand Down Expand Up @@ -359,47 +375,3 @@ func main() {

wg.Wait()
}

// statusResponseWriter wraps an http.ResponseWriter, recording
// the status code for logging.
type statusResponseWriter struct {
http.ResponseWriter
status int // the http.ResponseWriter updates this value
}

// WriteHeader writes the header and saves the status for inspection.
func (r *statusResponseWriter) WriteHeader(status int) {
r.ResponseWriter.WriteHeader(status)
r.status = status
}

// loggingHandler accepts an http.Handler and wraps it with a
// handler that logs the request and response information.
func loggingHandler(h http.Handler) http.Handler {
if h == nil {
h = http.DefaultServeMux
}

return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
newCtx := context.WithValue(req.Context(), "traceID", uuid.New().String())
req = req.WithContext(newCtx)
srw := statusResponseWriter{ResponseWriter: w, status: http.StatusOK}
query := req.URL.Query()

defer func(start time.Time) {
log.Info("finished handling",
zap.String("module", query.Get("module")),
zap.String("target", query.Get("target")),
zap.String("sourceAddr", req.RemoteAddr),
zap.String("method", req.Method),
zap.String("url", req.URL.String()),
zap.String("proto", req.Proto),
zap.Int("status", srw.status),
zap.Float64("elapsed_time_sec", time.Since(start).Seconds()),
zap.Any("trace_id", req.Context().Value("traceID")),
)
}(time.Now())

h.ServeHTTP(&srw, req)
})
}
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
module github.com/comcast/fishymetrics

go 1.21
go 1.22

require (
github.com/google/uuid v1.3.1
github.com/gorilla/mux v1.8.0
github.com/hashicorp/go-hclog v1.5.0
github.com/hashicorp/go-retryablehttp v0.7.2
github.com/hashicorp/vault/api v1.12.0
github.com/hashicorp/vault/api/auth/approle v0.4.0
github.com/hashicorp/vault/sdk v0.11.1
github.com/nrednav/cuid2 v1.0.0
github.com/prometheus/client_golang v1.14.0
github.com/stretchr/testify v1.8.4
go.uber.org/zap v1.26.0
go.uber.org/zap v1.27.0
gopkg.in/alecthomas/kingpin.v2 v2.2.6
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gopkg.in/yaml.v3 v3.0.1
Expand Down
14 changes: 6 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down Expand Up @@ -148,6 +144,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
github.com/nrednav/cuid2 v1.0.0 h1:27dn1oGiG+23Wa8XJ2DHeMoMa18Zs9u1+UHI9IlcGKM=
github.com/nrednav/cuid2 v1.0.0/go.mod h1:pdRH5Zrjwnv8DZ74XvHR3jX+bzJNfQjwLQ3JgSI2EmI=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.0-rc2.0.20221005185240-3a7f492d3f1b h1:YWuSjZCQAPM8UUBLkYUk1e+rZcvWHJmFb6i6rM44Xs8=
Expand Down Expand Up @@ -191,12 +189,12 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand Down
10 changes: 6 additions & 4 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (lumberjackSink) Sync() error {
return nil
}

func Initialize(svc, hostname string, config LoggerConfig) {
func Initialize(svc, hostname string, config LoggerConfig) error {

atomicLevel = zap.NewAtomicLevel()

Expand Down Expand Up @@ -98,17 +98,17 @@ func Initialize(svc, hostname string, config LoggerConfig) {
if config.LogMethod == "vector" {
url, err := url.Parse(config.VectorEndpoint)
if err != nil {
panic(err)
return err
}

err = zap.RegisterSink(url.Scheme, initVectorSink)
if err != nil {
panic(err)
return err
}

vecWriteSyncer, _, err := zap.Open(url.String())
if err != nil {
panic(err)
return err
}

ws := zapcore.Lock(vecWriteSyncer)
Expand Down Expand Up @@ -137,6 +137,8 @@ func Initialize(svc, hostname string, config LoggerConfig) {
atomicLevel.SetLevel(parseLevel(config.LogLevel))

zap.ReplaceGlobals(logger)

return nil
}

func Flush() {
Expand Down
Loading
Loading