Skip to content

Commit

Permalink
Go 1.17 enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasmalkmus committed Sep 8, 2021
1 parent ec1129a commit 550f0b0
Show file tree
Hide file tree
Showing 10 changed files with 457 additions and 163 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- main

env:
GOVERSION: "1.16"
GOVERSION: "1.17"

jobs:
lint:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- main

env:
GOVERSION: "1.16"
GOVERSION: "1.17"

jobs:
lint:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- "*"

env:
GOVERSION: "1.16"
GOVERSION: "1.17"

jobs:
release:
Expand Down
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ FROM alpine
LABEL maintainer="Axiom, Inc. <[email protected]>"

# Upgrade packages and install ca-certificates.
RUN apk update --no-cache
RUN apk upgrade --no-cache
RUN apk add --no-cache ca-certificates
RUN apk update --no-cache \
&& apk upgrade --no-cache \
&& apk add --no-cache ca-certificates

# Copy binary into image.
COPY axiom-loki-proxy /usr/bin/axiom-loki-proxy
Expand Down
52 changes: 31 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
# TOOLCHAIN
GO := CGO_ENABLED=0 GOBIN=$(CURDIR)/bin go
GO_BIN_IN_PATH := CGO_ENABLED=0 go
GOFMT := $(GO)fmt
GO := CGO_ENABLED=0 GOBIN=$(CURDIR)/bin go
GO_BIN_IN_PATH := CGO_ENABLED=0 go
GOFMT := $(GO)fmt

# ENVIRONMENT
VERBOSE =
GOPATH := $(GOPATH)
VERBOSE =
GOPATH := $(GOPATH)

# APPLICATION INFORMATION
BUILD_DATE := $(shell date -u '+%Y-%m-%dT%H:%M:%SZ')
REVISION := $(shell git rev-parse --short HEAD)
RELEASE := $(shell git describe --tags 2>/dev/null || git rev-parse --short HEAD)-dev
USER := $(shell whoami)
BUILD_DATE := $(shell date -u '+%Y-%m-%dT%H:%M:%SZ')
REVISION := $(shell git rev-parse --short HEAD)
RELEASE := $(shell git describe --tags 2>/dev/null || git rev-parse --short HEAD)-dev
USER := $(shell whoami)

# GO TOOLS
GOLANGCI_LINT := bin/golangci-lint
GORELEASER := bin/goreleaser
GOTESTSUM := bin/gotestsum
GOLANGCI_LINT := bin/golangci-lint
GORELEASER := bin/goreleaser
GOTESTSUM := bin/gotestsum

GOTOOLS := $(shell cat tools.go | grep "_ \"" | awk '{ print $$2 }' | tr -d '"')

# MISC
COVERPROFILE := coverage.out
DIST_DIR := dist
COVERPROFILE := coverage.out
DIST_DIR := dist

# TAGS
GOTAGS := osusergo netgo static_build
Expand All @@ -33,14 +35,14 @@ GOFLAGS += -X github.com/axiomhq/pkg/version.revision=$(REVISION)
GOFLAGS += -X github.com/axiomhq/pkg/version.buildDate=$(BUILD_DATE)
GOFLAGS += -X github.com/axiomhq/pkg/version.buildUser=$(USER)'

GO_TEST_FLAGS := -race -coverprofile=$(COVERPROFILE)
GORELEASER_FLAGS := --snapshot --rm-dist
GO_TEST_FLAGS := -race -coverprofile=$(COVERPROFILE)
GORELEASER_FLAGS := --snapshot --rm-dist

# DEPENDENCIES
GOMODDEPS = go.mod go.sum

# Enable verbose test output if explicitly set.
GOTESTSUM_FLAGS =
GOTESTSUM_FLAGS =
ifdef VERBOSE
GOTESTSUM_FLAGS += --format=standard-verbose
endif
Expand All @@ -62,12 +64,12 @@ build: $(GORELEASER) dep.stamp $(call go-pkg-sourcefiles, ./...) ## Build the bi
.PHONY: clean
clean: ## Remove build and test artifacts
@echo ">> cleaning up artifacts"
@rm -rf bin $(DIST_DIR) $(COVERPROFILE)
@rm -rf bin $(DIST_DIR) $(COVERPROFILE) dep.stamp

.PHONY: cover
cover: $(COVERPROFILE) ## Calculate the code coverage score
.PHONY: coverage
coverage: $(COVERPROFILE) ## Calculate the code coverage score
@echo ">> calculating code coverage"
@$(GO) tool cover -func=$(COVERPROFILE) | tail -n1
@$(GO) tool cover -func=$(COVERPROFILE) | grep total | awk '{print $$3}'

.PHONY: dep-clean
dep-clean: ## Remove obsolete dependencies
Expand All @@ -80,6 +82,9 @@ dep-upgrade: ## Upgrade all direct dependencies to their latest version
@$(GO) get -d $(shell $(GO) list -f '{{if not (or .Main .Indirect)}}{{.Path}}{{end}}' -m all)
@make dep

.PHONY: dep-upgrade-tools
dep-upgrade-tools: $(GOTOOLS) ## Upgrade all tool dependencies to their latest version and install them

.PHONY: dep
dep: dep-clean dep.stamp ## Install and verify dependencies and remove obsolete ones

Expand Down Expand Up @@ -138,3 +143,8 @@ $(GORELEASER): dep.stamp $(call go-pkg-sourcefiles, github.com/goreleaser/gorele
$(GOTESTSUM): dep.stamp $(call go-pkg-sourcefiles, gotest.tools/gotestsum)
@echo ">> installing gotestsum"
@$(GO) install gotest.tools/gotestsum

$(GOTOOLS): dep.stamp $(call go-pkg-sourcefiles, $@)
@echo ">> installing $@"
@$(GO) get -d $@
@$(GO) install $@
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,19 @@ Docker images are available on [DockerHub][docker].

## Usage

1. Set the following environment variables:
1. Set the following environment variables to connect to Axiom Cloud:

* `AXIOM_TOKEN`: **Personal Access** or **Ingest** token. Can be
created under `Profile` or `Settings > Ingest Tokens`. For security reasons it
is advised to use an Ingest Token with minimal privileges only.
* `AXIOM_TOKEN`: **Personal Access** or **Ingest** token. Can be created under
`Profile` or `Settings > Ingest Tokens`. For security reasons it is advised to
use an Ingest Token with minimal privileges only.
* `AXIOM_ORG_ID`: The organization identifier of the organization to use

When using Axiom Selfhost:

* `AXIOM_URL`: URL of the Axiom deployment to use.
* `AXIOM_TOKEN`: **Personal Access** or **Ingest** token. Can be created under
`Profile` or `Settings > Ingest Tokens`. For security reasons it is advised to
use an Ingest Token with minimal privileges only.
* `AXIOM_URL`: URL of the Axiom deployment to use

2. Run it: `./axiom-loki-proxy` or using Docker:

Expand Down
73 changes: 58 additions & 15 deletions cmd/axiom-loki-proxy/main.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,88 @@
package main

import (
"context"
"flag"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"github.com/axiomhq/axiom-go/axiom"
xhttp "github.com/axiomhq/pkg/http"
"github.com/axiomhq/pkg/version"

httpProxy "github.com/axiomhq/axiom-loki-proxy/http"
)

var (
deploymentURL = os.Getenv("AXIOM_URL")
accessToken = os.Getenv("AXIOM_TOKEN")
addr = flag.String("addr", ":8080", "Listen address <ip>:<port>")
const (
exitOK int = iota
exitConfig
exitInternal
)

var addr = flag.String("addr", ":8080", "Listen address <ip>:<port>")

func main() {
os.Exit(Main())
}

func Main() int {
// Export `AXIOM_TOKEN` and `AXIOM_ORG_ID` for Axiom Cloud
// Export `AXIOM_URL` and `AXIOM_TOKEN` for Axiom Selfhost

log.Print("starting axiom-loki-proxy version ", version.Release())

flag.Parse()

if deploymentURL == "" {
deploymentURL = axiom.CloudURL
}
if accessToken == "" {
log.Fatal("missing AXIOM_TOKEN")
}
ctx, cancel := signal.NotifyContext(context.Background(),
os.Interrupt,
os.Kill,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGQUIT,
)
defer cancel()

client, err := axiom.NewClient(deploymentURL, accessToken)
client, err := axiom.NewClient()
if err != nil {
log.Fatal(err)
log.Print(err)
return exitConfig
} else if err = client.ValidateCredentials(ctx); err != nil {
log.Print(err)
return exitConfig
}

mux := http.NewServeMux()
mux.Handle("/loki/api/v1/push", httpProxy.NewPushHandler(client))

log.Print("listening on", *addr)
srv, err := xhttp.NewServer(*addr, mux)
if err != nil {
log.Print(err)
return exitInternal
}
defer func() {
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), time.Second*5)
defer shutdownCancel()

if shutdownErr := srv.Shutdown(shutdownCtx); shutdownErr != nil {
log.Print(shutdownErr)
}
}()

srv.Run(ctx)

log.Print("listening on ", srv.ListenAddr().String())

select {
case <-ctx.Done():
log.Print("received interrupt, exiting gracefully")
case err := <-srv.ListenError():
log.Print("error starting http server, exiting gracefully: ", err)
return exitInternal
}

server := http.Server{Handler: mux, Addr: *addr}
log.Fatal(server.ListenAndServe())
return exitOK
}
Loading

0 comments on commit 550f0b0

Please sign in to comment.