-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore(*): summer cleanup And... A lot of code from another project of mine. * chore(main): server config should be seperated from the kromgo config. It's also because I would have to implement a lot of if statements for default configuration. * chore: remove deprecated request handler * fix: label not empty * chore: add some basic ratelimit support * chore: fix config path, update config example, add off-format logging from chi * chore: fallback on old url format if /query * chore(deps): update docker/build-push-action action to v6.6.1 (#61) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * fix: container building in wrong location * chore(*): summer cleanup And... A lot of code from another project of mine. * chore(main): server config should be seperated from the kromgo config. It's also because I would have to implement a lot of if statements for default configuration. * chore: remove deprecated request handler * fix: label not empty * chore: add some basic ratelimit support * chore: fix config path, update config example, add off-format logging from chi * chore: fallback on old url format if /query * fix: container building in wrong location * fix: webhook -> kromgo * chore: don't send just status codes, send invalid endpoint * chore: shields ignores it if its not 200 * chore: don't skip on codes, just send it --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
- Loading branch information
1 parent
471a1ca
commit 301616e
Showing
17 changed files
with
811 additions
and
446 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
name: Release | ||
|
||
on: | ||
pull_request: | ||
push: | ||
branches: ["main"] | ||
release: | ||
types: ["published"] | ||
|
||
jobs: | ||
build-image: | ||
if: ${{ github.event.pull_request.head.repo.full_name == github.repository || github.event_name != 'pull_request' }} | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
packages: write | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Docker meta | ||
id: meta | ||
uses: docker/metadata-action@v5 | ||
with: | ||
images: | | ||
ghcr.io/${{ github.repository }} | ||
tags: | | ||
type=semver,pattern={{version}},prefix=v | ||
type=semver,pattern={{major}}.{{minor}},prefix=v | ||
type=semver,pattern={{major}},prefix=v | ||
type=ref,event=branch | ||
type=ref,event=pr | ||
flavor: | | ||
latest=auto | ||
- name: Set up QEMU | ||
uses: docker/setup-qemu-action@v3 | ||
|
||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v3 | ||
|
||
- name: Login to GitHub Container Registry | ||
uses: docker/login-action@v3 | ||
with: | ||
registry: ghcr.io | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Build and Push | ||
uses: docker/build-push-action@v6 | ||
with: | ||
context: . | ||
file: ./Dockerfile | ||
platforms: linux/amd64,linux/arm64 | ||
push: true | ||
tags: ${{ steps.meta.outputs.tags }} | ||
labels: ${{ steps.meta.outputs.labels }} | ||
build-args: | | ||
VERSION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }} | ||
REVISION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,16 @@ | ||
# Build Project | ||
FROM golang:1.22.5-alpine as build | ||
WORKDIR /go/src/github.com/kashalls/kromgo | ||
FROM golang:1.22-alpine AS build | ||
ARG PKG=github.com/kashalls/kromgo | ||
ARG VERSION=dev | ||
ARG REVISION=dev | ||
WORKDIR /build | ||
COPY . . | ||
RUN go build -ldflags "-s -w -X main.Version=${VERSION} -X main.Gitsha=${REVISION}" ./cmd/kromgo | ||
|
||
ARG TARGETOS | ||
ARG TARGETARCH | ||
ARG TARGETVARIANT="" | ||
|
||
ENV GO111MODULE=on \ | ||
CGO_ENABLED=0 \ | ||
GOOS=${TARGETOS} \ | ||
GOARCH=${TARGETARCH} \ | ||
GOARM=${TARGETVARIANT} | ||
|
||
COPY go.mod go.sum ./ | ||
RUN go mod download | ||
COPY *.go ./ | ||
RUN go build -ldflags="-s -w" -o /kromgo | ||
|
||
# Final Image | ||
FROM gcr.io/distroless/static:nonroot | ||
FROM gcr.io/distroless/static-debian12:nonroot | ||
USER nonroot:nonroot | ||
COPY --from=build --chown=nonroot:nonroot /kromgo /kromgo/ | ||
EXPOSE 8080 | ||
|
||
CMD ["/kromgo/kromgo"] | ||
COPY --from=build --chmod=555 /build/kromgo /kromgo/kromgo | ||
EXPOSE 8080/tcp 8888/tcp | ||
LABEL \ | ||
org.opencontainers.image.title="kromgo" \ | ||
org.opencontainers.image.source="https://github.com/kashalls/kromgo" | ||
org.opencontainers.image.source="https://github.com/kashalls/kromgo" | ||
ENTRYPOINT ["/kromgo/kromgo"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package configuration | ||
|
||
import ( | ||
"os" | ||
"time" | ||
|
||
"github.com/caarlos0/env/v11" | ||
"github.com/kashalls/kromgo/cmd/kromgo/init/log" | ||
"go.uber.org/zap" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type ServerConfig struct { | ||
ServerHost string `env:"SERVER_HOST" envDefault:"localhost"` | ||
ServerPort int `env:"SERVER_PORT" envDefault:"8080"` | ||
|
||
HealthHost string `env:"HEALTH_HOST" envDefault:"localhost"` | ||
HealthPort int `env:"HEALTH_PORT" envDefault:"8888"` | ||
|
||
ServerReadTimeout time.Duration `env:"SERVER_READ_TIMEOUT"` | ||
ServerWriteTimeout time.Duration `env:"SERVER_WRITE_TIMEOUT"` | ||
ServerLogging bool `env:"SERVER_LOGGING"` | ||
|
||
RatelimitEnable bool `env:"RATELIMIT_ENABLE"` | ||
RatelimitAll bool `env:"RATELIMIT_ALL"` | ||
RatelimitByRealIP bool `env:"RATELIMIT_BY_REAL_IP"` | ||
RatelimitRequestLimit int `env:"RATELIMIT_REQUEST_LIMIT" envDefault:"100"` | ||
RatelimitWindowLength time.Duration `env:"RATELIMIT_WINDOW_LENGTH" envDefault:"1m"` | ||
} | ||
|
||
// KromgoConfig struct for configuration environmental variables | ||
type KromgoConfig struct { | ||
Prometheus string `yaml:"prometheus,omitempty" json:"prometheus,omitempty"` | ||
Metrics []Metric `yaml:"metrics" json:"metrics"` | ||
} | ||
|
||
type Metric struct { | ||
Name string `yaml:"name" json:"name"` | ||
Query string `yaml:"query" json:"query"` | ||
Label string `yaml:"label,omitempty" json:"label,omitempty"` | ||
Prefix string `yaml:"prefix,omitempty" json:"prefix,omitempty"` | ||
Suffix string `yaml:"suffix,omitempty" json:"suffix,omitempty"` | ||
Colors []MetricColor `yaml:"colors,omitempty" json:"colors,omitempty"` | ||
} | ||
|
||
type MetricColor struct { | ||
Min float64 `yaml:"min" json:"min"` | ||
Max float64 `yaml:"max" json:"max"` | ||
Color string `yaml:"color,omitempty" json:"color,omitempty"` | ||
ValueOverride string `yaml:"valueOverride,omitempty" json:"valueOverride,omitempty"` | ||
} | ||
|
||
var ConfigPath = "/kromgo/config.yaml" // Default config file path | ||
var ProcessedMetrics map[string]Metric | ||
|
||
// Init sets up configuration by reading set environmental variables | ||
func Init(configPath string) KromgoConfig { | ||
|
||
if configPath == "" { | ||
configPath = ConfigPath | ||
} | ||
|
||
// Read file from path. | ||
data, err := os.ReadFile(configPath) | ||
if err != nil { | ||
log.Error("error reading config file", zap.Error(err)) | ||
os.Exit(1) | ||
} | ||
|
||
var config KromgoConfig | ||
if err := yaml.Unmarshal(data, &config); err != nil { | ||
log.Error("error unmarshalling config yaml", zap.Error(err)) | ||
os.Exit(1) | ||
} | ||
|
||
ProcessedMetrics = preprocess(config.Metrics) | ||
return config | ||
} | ||
|
||
func InitServer() ServerConfig { | ||
cfg := ServerConfig{} | ||
if err := env.Parse(&cfg); err != nil { | ||
log.Error("error reading configuration from environment", zap.Error(err)) | ||
} | ||
return cfg | ||
} | ||
|
||
func preprocess(metrics []Metric) map[string]Metric { | ||
reverseMap := make(map[string]Metric) | ||
for _, obj := range metrics { | ||
reverseMap[obj.Name] = obj | ||
} | ||
return reverseMap | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package log | ||
|
||
import ( | ||
"os" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
var logger *zap.Logger | ||
|
||
func Init() { | ||
config := zap.NewProductionConfig() | ||
|
||
// Set the log format | ||
format := os.Getenv("LOG_FORMAT") | ||
if format == "test" { | ||
config.Encoding = "console" | ||
} else { | ||
config.Encoding = "json" | ||
} | ||
|
||
// Set the log level | ||
level := os.Getenv("LOG_LEVEL") | ||
switch level { | ||
case "debug": | ||
config.Level = zap.NewAtomicLevelAt(zap.DebugLevel) | ||
case "info": | ||
config.Level = zap.NewAtomicLevelAt(zap.InfoLevel) | ||
case "warn": | ||
config.Level = zap.NewAtomicLevelAt(zap.WarnLevel) | ||
case "error": | ||
config.Level = zap.NewAtomicLevelAt(zap.ErrorLevel) | ||
default: | ||
config.Level = zap.NewAtomicLevelAt(zap.InfoLevel) | ||
} | ||
|
||
// Build the logger | ||
var err error | ||
logger, err = config.Build() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Ensure we flush any buffered log entries | ||
defer logger.Sync() | ||
} | ||
|
||
func Info(message string, fields ...zap.Field) { | ||
logger.Info(message, fields...) | ||
} | ||
|
||
func Debug(message string, fields ...zap.Field) { | ||
logger.Debug(message, fields...) | ||
} | ||
|
||
func Error(message string, fields ...zap.Field) { | ||
logger.Error(message, fields...) | ||
} | ||
|
||
func Fatal(message string, fields ...zap.Field) { | ||
logger.Fatal(message, fields...) | ||
} | ||
|
||
func With(fields ...zap.Field) *zap.Logger { | ||
return logger.With(fields...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package prometheus | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/kashalls/kromgo/cmd/kromgo/init/configuration" | ||
"github.com/prometheus/client_golang/api" | ||
v1 "github.com/prometheus/client_golang/api/prometheus/v1" | ||
) | ||
|
||
var Papi v1.API | ||
|
||
func Init(config configuration.KromgoConfig) (v1.API, error) { | ||
prometheusURL := os.Getenv("PROMETHEUS_URL") | ||
if prometheusURL != "" { | ||
config.Prometheus = prometheusURL | ||
} | ||
|
||
if len(config.Prometheus) == 0 { | ||
return nil, fmt.Errorf("no url pointing to a prometheus instance was provided") | ||
} | ||
|
||
client, err := api.NewClient(api.Config{ | ||
Address: config.Prometheus, | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating prometheus client: %s", err) | ||
} | ||
|
||
Papi = v1.NewAPI(client) | ||
return Papi, nil | ||
} |
Oops, something went wrong.