Skip to content

Commit e766aae

Browse files
committed
Go 1.17 enhancements
1 parent bafb4d9 commit e766aae

File tree

11 files changed

+481
-182
lines changed

11 files changed

+481
-182
lines changed

.github/workflows/pr.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
- main
77

88
env:
9-
GOVERSION: "1.16"
9+
GOVERSION: "1.17"
1010

1111
jobs:
1212
lint:

.github/workflows/push.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
- main
77

88
env:
9-
GOVERSION: "1.16"
9+
GOVERSION: "1.17"
1010

1111
jobs:
1212
lint:

.github/workflows/release.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ name: Release
33
on:
44
push:
55
tags:
6-
- "*"
6+
- "v*"
77

88
env:
9-
GOVERSION: "1.16"
9+
GOVERSION: "1.17"
1010

1111
jobs:
1212
release:

.golangci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ linters:
1919
- goconst
2020
- gofmt
2121
- goimports
22-
- golint
2322
- gosec
2423
- gosimple
2524
- govet
2625
- ineffassign
2726
- misspell
2827
- nolintlint
2928
- prealloc
29+
- revive
3030
- staticcheck
3131
- structcheck
3232
- stylecheck

Dockerfile

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ FROM alpine
33
LABEL maintainer="Axiom, Inc. <info@axiom.co>"
44

55
# Upgrade packages and install ca-certificates.
6-
RUN apk update --no-cache
7-
RUN apk upgrade --no-cache
8-
RUN apk add --no-cache ca-certificates
6+
RUN apk update --no-cache \
7+
&& apk upgrade --no-cache \
8+
&& apk add --no-cache ca-certificates
99

1010
# Copy binary into image.
1111
COPY axiom-honeycomb-proxy /usr/bin/axiom-honeycomb-proxy

Makefile

+22-12
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,28 @@ GO_BIN_IN_PATH := CGO_ENABLED=0 go
44
GOFMT := $(GO)fmt
55

66
# ENVIRONMENT
7-
VERBOSE =
8-
GOPATH := $(GOPATH)
7+
VERBOSE =
8+
GOPATH := $(GOPATH)
99

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

1616
# GO TOOLS
1717
GOLANGCI_LINT := bin/golangci-lint
1818
GORELEASER := bin/goreleaser
1919
GOTESTSUM := bin/gotestsum
2020

21+
GOTOOLS := $(shell cat tools.go | grep "_ \"" | awk '{ print $$2 }' | tr -d '"')
22+
2123
# MISC
2224
COVERPROFILE := coverage.out
23-
DIST_DIR := dist
25+
DIST_DIR := dist
2426

2527
# TAGS
26-
GOTAGS := osusergo netgo static_build
28+
GOTAGS := osusergo netgo static_build
2729

2830
# FLAGS
2931
GOFLAGS := -buildmode=pie -tags='$(GOTAGS)' -installsuffix=cgo -trimpath
@@ -62,12 +64,12 @@ build: $(GORELEASER) dep.stamp $(call go-pkg-sourcefiles, ./...) ## Build the bi
6264
.PHONY: clean
6365
clean: ## Remove build and test artifacts
6466
@echo ">> cleaning up artifacts"
65-
@rm -rf bin $(DIST_DIR) $(COVERPROFILE)
67+
@rm -rf bin $(DIST_DIR) $(COVERPROFILE) dep.stamp
6668

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

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

85+
.PHONY: dep-upgrade-tools
86+
dep-upgrade-tools: $(GOTOOLS) ## Upgrade all tool dependencies to their latest version and install them
87+
8388
.PHONY: dep
8489
dep: dep-clean dep.stamp ## Install and verify dependencies and remove obsolete ones
8590

@@ -138,3 +143,8 @@ $(GORELEASER): dep.stamp $(call go-pkg-sourcefiles, github.com/goreleaser/gorele
138143
$(GOTESTSUM): dep.stamp $(call go-pkg-sourcefiles, gotest.tools/gotestsum)
139144
@echo ">> installing gotestsum"
140145
@$(GO) install gotest.tools/gotestsum
146+
147+
$(GOTOOLS): dep.stamp $(call go-pkg-sourcefiles, $@)
148+
@echo ">> installing $@"
149+
@$(GO) get -d $@
150+
@$(GO) install $@

README.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,19 @@ Docker images are available on [DockerHub][docker].
6262

6363
## Usage
6464

65-
1. Set the following environment variables:
65+
1. Set the following environment variables to connect to Axiom Cloud:
6666

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

7172
When using Axiom Selfhost:
7273

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

7579
2. Run it: `./axiom-honeycomb-proxy` or using Docker:
7680

@@ -144,4 +148,4 @@ See [LICENSE](LICENSE) for more information.
144148
[license]: https://opensource.org/licenses/MIT
145149
[license_badge]: https://img.shields.io/github/license/axiomhq/axiom-honeycomb-proxy.svg?color=blue&style=flat-square&ghcache=unused
146150
[docker]: https://hub.docker.com/r/axiomhq/axiom-honeycomb-proxy
147-
[docker_badge]: https://img.shields.io/docker/pulls/axiomhq/axiom-honeycomb-proxy.svg?style=flat-square&ghcache=unused
151+
[docker_badge]: https://img.shields.io/docker/pulls/axiomhq/axiom-honeycomb-proxy.svg?style=flat-square&ghcache=unused

cmd/axiom-honeycomb-proxy/main.go

+63-18
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,92 @@
11
package main
22

33
import (
4+
"context"
45
"flag"
56
"log"
6-
"net/http"
77
"os"
8+
"os/signal"
9+
"syscall"
10+
"time"
811

912
"github.com/axiomhq/axiom-go/axiom"
13+
"github.com/axiomhq/pkg/http"
1014
"github.com/axiomhq/pkg/version"
1115

1216
"github.com/axiomhq/axiom-honeycomb-proxy/proxy"
1317
)
1418

15-
var (
16-
defaultHoneyCombURL = "https://api.honeycomb.io"
17-
deploymentURL = os.Getenv("AXIOM_URL")
18-
accessToken = os.Getenv("AXIOM_TOKEN")
19-
addr = flag.String("addr", ":8080", "Listen address <ip>:<port>")
19+
const (
20+
exitOK int = iota
21+
exitConfig
22+
exitInternal
2023
)
2124

25+
const defaultHoneyCombURL = "https://api.honeycomb.io"
26+
27+
var addr = flag.String("addr", ":8080", "Listen address <ip>:<port>")
28+
2229
func main() {
30+
os.Exit(Main())
31+
}
32+
33+
func Main() int {
34+
// Export `AXIOM_TOKEN` and `AXIOM_ORG_ID` for Axiom Cloud
35+
// Export `AXIOM_URL` and `AXIOM_TOKEN` for Axiom Selfhost
36+
2337
log.Print("starting axiom-honeycomb-proxy version ", version.Release())
2438

2539
flag.Parse()
2640

27-
if deploymentURL == "" {
28-
deploymentURL = axiom.CloudURL
29-
}
30-
if accessToken == "" {
31-
log.Fatal("missing AXIOM_TOKEN")
32-
}
41+
ctx, cancel := signal.NotifyContext(context.Background(),
42+
os.Interrupt,
43+
os.Kill,
44+
syscall.SIGHUP,
45+
syscall.SIGINT,
46+
syscall.SIGQUIT,
47+
)
48+
defer cancel()
3349

34-
client, err := axiom.NewClient(deploymentURL, accessToken)
50+
client, err := axiom.NewClient()
3551
if err != nil {
36-
log.Fatal(err)
52+
log.Print(err)
53+
return exitConfig
54+
} else if err = client.ValidateCredentials(ctx); err != nil {
55+
log.Print(err)
56+
return exitConfig
3757
}
3858

39-
log.Print("listening on", *addr)
4059
mp, err := proxy.NewMultiplexer(client, defaultHoneyCombURL)
4160
if err != nil {
42-
panic(err)
61+
log.Print(err)
62+
return exitInternal
63+
}
64+
65+
srv, err := http.NewServer(*addr, mp)
66+
if err != nil {
67+
log.Print(err)
68+
return exitInternal
69+
}
70+
defer func() {
71+
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), time.Second*5)
72+
defer shutdownCancel()
73+
74+
if shutdownErr := srv.Shutdown(shutdownCtx); shutdownErr != nil {
75+
log.Print(shutdownErr)
76+
}
77+
}()
78+
79+
srv.Run(ctx)
80+
81+
log.Print("listening on ", srv.ListenAddr().String())
82+
83+
select {
84+
case <-ctx.Done():
85+
log.Print("received interrupt, exiting gracefully")
86+
case err := <-srv.ListenError():
87+
log.Print("error starting http server, exiting gracefully: ", err)
88+
return exitInternal
4389
}
4490

45-
server := http.Server{Handler: mp, Addr: *addr}
46-
log.Fatal(server.ListenAndServe())
91+
return exitOK
4792
}

0 commit comments

Comments
 (0)