Skip to content

Commit

Permalink
Merge pull request #2 from axoflow/master
Browse files Browse the repository at this point in the history
metrics-exporter changes
  • Loading branch information
bshifter authored Aug 3, 2023
2 parents 780d04e + 4fb24de commit ee8639a
Show file tree
Hide file tree
Showing 14 changed files with 221 additions and 79 deletions.
54 changes: 27 additions & 27 deletions .github/workflows/artifacts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,31 +109,31 @@ jobs:
id: image-ref
run: echo "value=${{ steps.image-name.outputs.value }}@${{ steps.build.outputs.digest }}" >> "$GITHUB_OUTPUT"

- name: Fetch image
run: skopeo --insecure-policy copy docker://${{ steps.image-name.outputs.value }}:${{ steps.meta.outputs.version }} oci-archive:image.tar
if: inputs.publish

- name: Extract OCI tarball
run: |
mkdir -p image
tar -xf image.tar -C image
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@41f05d9ecffa2ed3f1580af306000f734b733e54 # 0.11.2
with:
input: image
format: sarif
output: trivy-results.sarif

- name: Upload Trivy scan results as artifact
uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # v3.1.2
with:
name: "[${{ github.job }}] Trivy scan results"
path: trivy-results.sarif
retention-days: 5

- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@1813ca74c3faaa3a2da2070b9b8a0b3e7373a0d8 # v2.21.0
with:
sarif_file: trivy-results.sarif
# - name: Fetch image
# run: skopeo --insecure-policy copy docker://${{ steps.image-name.outputs.value }}:${{ steps.meta.outputs.version }} oci-archive:image.tar
# if: inputs.publish

# - name: Extract OCI tarball
# run: |
# mkdir -p image
# tar -xf image.tar -C image

# - name: Run Trivy vulnerability scanner
# uses: aquasecurity/trivy-action@41f05d9ecffa2ed3f1580af306000f734b733e54 # 0.11.2
# with:
# input: image
# format: sarif
# output: trivy-results.sarif

# - name: Upload Trivy scan results as artifact
# uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # v3.1.2
# with:
# name: "[${{ github.job }}] Trivy scan results"
# path: trivy-results.sarif
# retention-days: 5

# - name: Upload Trivy scan results to GitHub Security tab
# uses: github/codeql-action/upload-sarif@1813ca74c3faaa3a2da2070b9b8a0b3e7373a0d8 # v2.21.0
# with:
# sarif_file: trivy-results.sarif

59 changes: 59 additions & 0 deletions .github/workflows/go-lint-and-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Go basic checks

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
check-license:
name: Check licenses
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: '1.20'
- name: Check out code
uses: actions/checkout@v3
- name: Cache licenses
uses: actions/cache@v3
with:
key: licensei-cache-${{ hashFiles('go.sum') }}
path: ".licensei.cache"
restore-keys: licensei-cache
- name: Ensure licensei cache
env:
GITHUB_TOKEN: ${{ github.token }}
run: make .licensei.cache
- name: Run license check
run: make check-license

run-test:
name: Go tests
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: '1.20'
- name: Check out code
uses: actions/checkout@v3
- name: Run tests
run: make test

run-lint:
name: Go lint
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: '1.20'
- name: Check out code
uses: actions/checkout@v3
- name: Run lint
run: make lint
env:
LINTER_FLAGS: '--timeout 5m'
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
# Dependency directories (remove the comment below to include it)
# vendor/

bin/
dist/

.idea
.vscode
.garden
.licensei.cache
18 changes: 18 additions & 0 deletions .licensei.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
approved = [
"apache-2.0",
"bsd-2-clause",
"bsd-3-clause",
"isc",
"mit",
"mit-0",
]

ignored = [
"google.golang.org/protobuf", # Unsupported VCS, bsd-3-clause
]

[header]
ignoreFiles = ["zz_generated.*.go", "generated.go", "cache.go"]
authors = ["Axoflow"]
template = """// Copyright © :YEAR: :AUTHOR:
// All rights reserved."""
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ FROM gcr.io/distroless/base-debian11:latest as prod

WORKDIR /
COPY --from=builder /LICENSE /
COPY --from=builder /app/dist/metrics-exporter .
ENTRYPOINT ["/metrics-exporter"]
COPY --from=builder /app/dist/axosyslog-metrics-exporter .
ENTRYPOINT ["/axosyslog-metrics-exporter"]
67 changes: 65 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
GOARCH := $(shell go env GOARCH)
GOOS := $(shell go env GOOS)
GOVERSION := $(shell go env GOVERSION)

DIST ?= ${PWD}/dist
GIT_TAG_COMMAND = git describe --always --dirty --tags 2>/dev/null
GIT_TAG ?= $(shell ${GIT_TAG_COMMAND} || cat .version-tag 2>/dev/null || echo dev)
GO_LDFLAGS := -X 'main.Version=${GIT_TAG}'

# build tools
BIN ?= ${PWD}/bin/${GOOS}/${GOARCH}

LICENSEI := ${BIN}/licensei
LICENSEI_VERSION = v0.8.0
GOLANGCI_LINT := ${BIN}/golangci-lint
GOLANGCI_LINT_VERSION := v1.51.2

.PHONY: fmt
fmt: ## format Go sources
go fmt ./...
Expand All @@ -13,15 +25,66 @@ tidy: ## ensures go.mod dependecies

.PHONY: build
build: ## build
go build -o $(DIST)/metrics-exporter -ldflags="$(GO_LDFLAGS)" $(BUILDFLAGS) ./
go build -o $(DIST)/axosyslog-metrics-exporter -ldflags="$(GO_LDFLAGS)" $(BUILDFLAGS) ./

.PHONY: run
run: ## runs project locally with go run
go run $(BUILDFLAGS) ./ $(ARGS)

.PHONY: docker-build
docker-build: ## builds docker container locally
docker build . -t "axoflow.local.dev/metrics-exporter:latest" -f Dockerfile
docker build . -t "axoflow.local.dev/axosyslog-metrics-exporter:latest" -f Dockerfile

.PHONY: test
test: ## runs unit tests
go test ./...

.PHONY: lint
lint: ${GOLANGCI_LINT} ## check coding style
${GOLANGCI_LINT} run ${LINTER_FLAGS}

## =========================
## == Tool dependencies ==
## =========================

${BIN}:
mkdir -p $@

${LICENSEI}: ${LICENSEI}_${LICENSEI_VERSION}_${GOVERSION} | ${BIN}

.PHONY: check-license
check-license: ${LICENSEI} .licensei.cache ## check license + copyright headers
${LICENSEI} check
${LICENSEI} header

${LICENSEI}: ${LICENSEI}_${LICENSEI_VERSION}_${GOVERSION}
ln -sf $(notdir $<) $@

${LICENSEI}_${LICENSEI_VERSION}_${GOVERSION}: IMPORT_PATH := github.com/goph/licensei/cmd/licensei
${LICENSEI}_${LICENSEI_VERSION}_${GOVERSION}: VERSION := ${LICENSEI_VERSION}
${LICENSEI}_${LICENSEI_VERSION}_${GOVERSION}: | ${BIN}
${go_install_binary}

.licensei.cache: ${LICENSEI}
ifndef GITHUB_TOKEN
@>&2 echo "WARNING: building licensei cache without Github token, rate limiting might occur."
@>&2 echo "(Hint: If too many licenses are missing, try specifying a Github token via the environment variable GITHUB_TOKEN.)"
endif
${LICENSEI} cache

${GOLANGCI_LINT}: ${GOLANGCI_LINT}_${GOLANGCI_LINT_VERSION}_${GOVERSION} | ${BIN}
ln -sf $(notdir $<) $@

${GOLANGCI_LINT}_${GOLANGCI_LINT_VERSION}_${GOVERSION}: IMPORT_PATH := github.com/golangci/golangci-lint/cmd/golangci-lint
${GOLANGCI_LINT}_${GOLANGCI_LINT_VERSION}_${GOVERSION}: VERSION := ${GOLANGCI_LINT_VERSION}
${GOLANGCI_LINT}_${GOLANGCI_LINT_VERSION}_${GOVERSION}: | ${BIN}
${go_install_binary}

define go_install_binary
find ${BIN} -name '$(notdir ${IMPORT_PATH})_*' -exec rm {} +
GOBIN=${BIN} go install ${IMPORT_PATH}@${VERSION}
mv ${BIN}/$(notdir ${IMPORT_PATH}) $@
endef

# Self-documenting Makefile
.DEFAULT_GOAL = help
Expand Down
3 changes: 1 addition & 2 deletions garden.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
kind: Module
description: syslog-ng metrics exporter image
name: metrics-exporter-image
name: axosyslog-metrics-exporter-image
type: container
dockerfile: Dockerfile
image: ${var.imageRegistry}/${var.imageRepository}/metrics-exporter
build:
targetImage: prod
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module github.com/axoflow/metrics-exporter
module github.com/axoflow/axosyslog-metrics-exporter

go 1.20

replace github.com/axoflow/metrics-exporter/pkg/syslog-ng-ctl => ./pkg/syslog-ng-ctl
replace github.com/axoflow/axosyslog-metrics-exporter/pkg/syslog-ng-ctl => ./pkg/syslog-ng-ctl

require (
github.com/axoflow/metrics-exporter/pkg/syslog-ng-ctl v0.0.0-00010101000000-000000000000
github.com/axoflow/axosyslog-metrics-exporter/pkg/syslog-ng-ctl v0.0.0-00010101000000-000000000000
github.com/prometheus/common v0.44.0
)

Expand Down
54 changes: 40 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,66 @@ package main
import (
"bytes"
"context"
"flag"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"time"

syslogngctl "github.com/axoflow/metrics-exporter/pkg/syslog-ng-ctl"
syslogngctl "github.com/axoflow/axosyslog-metrics-exporter/pkg/syslog-ng-ctl"
"github.com/prometheus/common/expfmt"
)

const (
TIMEOUT_SYSLOG time.Duration = time.Second * 3
HTTP_PORT = 9999
DEFAULT_TIMEOUT_SYSLOG time.Duration = time.Second * 5
DEFAULT_SERVICE_PORT = "9577"
DEFAULT_SOCKET_ADDR = "/var/run/syslog-ng/syslog-ng.ctl"
)

var (
Version = "dev" // should be set build-time, see Makefile
)

type RunArgs struct {
SocketAddr string
ServicePort string
RequestTimeout string
}

func envOrDef(envName string, def string) (res string) {
res = os.Getenv(envName)
if res == "" {
res = def
}
return
}

func main() {
fmt.Fprintf(os.Stdout, "metrics exporter version %q\n", Version)
runArgs := RunArgs{}

socketAddr := os.Getenv("CONTROL_SOCKET")
if socketAddr == "" {
socketAddr = "/var/run/syslog-ng/syslog-ng.tcl"
_, _ = fmt.Fprintf(os.Stdout, "CONTROL_SOCKET environment variable not set, defaulting to %q", socketAddr)
fmt.Fprintf(os.Stdout, "%v version %q\n", filepath.Base(os.Args[0]), Version)

flag.StringVar(&runArgs.SocketAddr, "socket.path", envOrDef("CONTROL_SOCKET", DEFAULT_SOCKET_ADDR), "syslog-ng control socket path")
flag.StringVar(&runArgs.ServicePort, "service.port", envOrDef("SERVICE_PORT", DEFAULT_SERVICE_PORT), "service port")
flag.StringVar(&runArgs.RequestTimeout, "service.timeout", envOrDef("SERVICE_TIMEOUT", DEFAULT_TIMEOUT_SYSLOG.String()), "request timeout")

flag.Parse()

requestTimeout, err := time.ParseDuration(runArgs.RequestTimeout)
if err != nil {
requestTimeout = DEFAULT_TIMEOUT_SYSLOG
}

ctl := syslogngctl.Controller{
ControlChannel: syslogngctl.NewUnixDomainSocketControlChannel(socketAddr),
ControlChannel: syslogngctl.NewUnixDomainSocketControlChannel(runArgs.SocketAddr),
}

mux := http.NewServeMux()
mux.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
subCtx, cancel := context.WithTimeout(r.Context(), TIMEOUT_SYSLOG)

subCtx, cancel := context.WithTimeout(r.Context(), requestTimeout)
defer cancel()
mfs, err := ctl.StatsPrometheus(subCtx)
if err != nil {
Expand All @@ -65,7 +90,7 @@ func main() {
})

mux.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
subCtx, cancel := context.WithTimeout(r.Context(), TIMEOUT_SYSLOG)
subCtx, cancel := context.WithTimeout(r.Context(), requestTimeout)
defer cancel()
err := ctl.Ping(subCtx)
if err != nil {
Expand All @@ -78,8 +103,9 @@ func main() {
}
})

fmt.Fprintf(os.Stdout, "listening on port %v, \n", HTTP_PORT)
fmt.Fprintf(os.Stdout, "syslog-ng control socket path %v, \n", socketAddr)
fmt.Fprintf(os.Stdout, "listening on port: %v\n", runArgs.ServicePort)
fmt.Fprintf(os.Stdout, "syslog-ng control socket path: %v\n", runArgs.SocketAddr)
fmt.Fprintf(os.Stdout, "service timeout: %v\n", requestTimeout.String())

fmt.Println(http.ListenAndServe(fmt.Sprintf(":%v", HTTP_PORT), mux))
fmt.Println(http.ListenAndServe(fmt.Sprintf(":%v", runArgs.ServicePort), mux))
}
2 changes: 1 addition & 1 deletion pkg/syslog-ng-ctl/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"os"
"strings"

syslogngctl "github.com/axoflow/metrics-exporter/pkg/syslog-ng-ctl"
syslogngctl "github.com/axoflow/axosyslog-metrics-exporter/pkg/syslog-ng-ctl"
"github.com/prometheus/common/expfmt"
"golang.org/x/exp/slices"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/syslog-ng-ctl/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/axoflow/metrics-exporter/pkg/syslog-ng-ctl
module github.com/axoflow/axosyslog-metrics-exporter/pkg/syslog-ng-ctl

go 1.20

Expand Down
2 changes: 1 addition & 1 deletion pkg/syslog-ng-ctl/io/read_until.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"bytes"
"io"

bytesx "github.com/axoflow/metrics-exporter/pkg/syslog-ng-ctl/bytes"
bytesx "github.com/axoflow/axosyslog-metrics-exporter/pkg/syslog-ng-ctl/bytes"
)

// ReadUntil reads from the specified reader until it reaches the specified separator (or an error occurs, which includes EOF).
Expand Down
Loading

0 comments on commit ee8639a

Please sign in to comment.