Skip to content

Commit

Permalink
Use axiomhq/pkg/cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasmalkmus committed Oct 6, 2021
1 parent 550f0b0 commit 05aefab
Show file tree
Hide file tree
Showing 3 changed files with 262 additions and 160 deletions.
78 changes: 26 additions & 52 deletions cmd/axiom-loki-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,86 +3,60 @@ package main
import (
"context"
"flag"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
stdHttp "net/http"

"github.com/axiomhq/axiom-go/axiom"
xhttp "github.com/axiomhq/pkg/http"
"github.com/axiomhq/pkg/version"
"github.com/axiomhq/pkg/cmd"
"github.com/axiomhq/pkg/http"
"go.uber.org/zap"

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

const (
exitOK int = iota
exitConfig
exitInternal
)

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

func main() {
os.Exit(Main())
cmd.Run("axiom-loki-proxy", run,
cmd.WithValidateAxiomCredentials(),
)
}

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())
func run(ctx context.Context, log *zap.Logger, client *axiom.Client) error {
// Export `AXIOM_TOKEN` and `AXIOM_ORG_ID` for Axiom Cloud.
// Export `AXIOM_URL` and `AXIOM_TOKEN` for Axiom Selfhost.

flag.Parse()

ctx, cancel := signal.NotifyContext(context.Background(),
os.Interrupt,
os.Kill,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGQUIT,
)
defer cancel()

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

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

srv, err := xhttp.NewServer(*addr, mux)
srv, err := http.NewServer(*addr, mux,
http.WithBaseContext(ctx),
http.WithLogger(log),
)
if err != nil {
log.Print(err)
return exitInternal
return cmd.Error("create http server", err)
}
defer func() {
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), time.Second*5)
defer shutdownCancel()

if shutdownErr := srv.Shutdown(shutdownCtx); shutdownErr != nil {
log.Print(shutdownErr)
if shutdownErr := srv.Shutdown(); shutdownErr != nil {
log.Error("stopping server", zap.Error(shutdownErr))
return
}
}()

srv.Run(ctx)

log.Print("listening on ", srv.ListenAddr().String())
log.Info("server listening",
zap.String("address", srv.ListenAddr().String()),
zap.String("network", srv.ListenAddr().Network()),
)

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

return exitOK
return nil
}
83 changes: 50 additions & 33 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,42 @@ module github.com/axiomhq/axiom-loki-proxy
go 1.17

require (
github.com/axiomhq/axiom-go v0.6.0
github.com/axiomhq/pkg v0.1.0
github.com/axiomhq/axiom-go v0.6.2
github.com/axiomhq/pkg v0.2.0
github.com/golang/snappy v0.0.4
github.com/golangci/golangci-lint v1.42.1
github.com/goreleaser/goreleaser v0.177.0
github.com/goreleaser/goreleaser v0.180.3
github.com/grafana/loki v1.6.1
github.com/stretchr/testify v1.7.0
go.uber.org/zap v1.19.1
gotest.tools/gotestsum v1.7.0
)

require (
4d63.com/gochecknoglobals v0.0.0-20201008074935-acfc0b28355a // indirect
cloud.google.com/go v0.81.0 // indirect
cloud.google.com/go/storage v1.15.0 // indirect
cloud.google.com/go v0.94.0 // indirect
cloud.google.com/go/kms v0.1.0 // indirect
cloud.google.com/go/storage v1.16.1 // indirect
code.gitea.io/sdk/gitea v0.15.0 // indirect
github.com/AlekSi/pointer v1.1.0 // indirect
github.com/Antonboom/errname v0.1.4 // indirect
github.com/Azure/azure-pipeline-go v0.2.3 // indirect
github.com/Azure/azure-sdk-for-go v54.0.0+incompatible // indirect
github.com/Azure/azure-storage-blob-go v0.13.0 // indirect
github.com/Azure/azure-sdk-for-go v57.0.0+incompatible // indirect
github.com/Azure/azure-storage-blob-go v0.14.0 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest v0.11.18 // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.13 // indirect
github.com/Azure/go-autorest/autorest/azure/auth v0.5.7 // indirect
github.com/Azure/go-autorest/autorest/azure/cli v0.4.2 // indirect
github.com/Azure/go-autorest/autorest v0.11.20 // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.15 // indirect
github.com/Azure/go-autorest/autorest/azure/auth v0.5.8 // indirect
github.com/Azure/go-autorest/autorest/azure/cli v0.4.3 // indirect
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/Azure/go-autorest/autorest/to v0.4.0 // indirect
github.com/Azure/go-autorest/autorest/validation v0.3.1 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/BurntSushi/toml v0.4.1 // indirect
github.com/DisgoOrg/disgohook v1.4.3 // indirect
github.com/DisgoOrg/log v1.1.0 // indirect
github.com/DisgoOrg/restclient v1.2.7 // indirect
github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
Expand All @@ -46,7 +51,18 @@ require (
github.com/apex/log v1.9.0 // indirect
github.com/ashanbrown/forbidigo v1.2.0 // indirect
github.com/ashanbrown/makezero v0.0.0-20210520155254-b6261585ddde // indirect
github.com/aws/aws-sdk-go v1.38.35 // indirect
github.com/atc0005/go-teams-notify/v2 v2.6.0 // indirect
github.com/aws/aws-sdk-go v1.40.34 // indirect
github.com/aws/aws-sdk-go-v2 v1.9.0 // indirect
github.com/aws/aws-sdk-go-v2/config v1.7.0 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.4.0 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.5.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.2.2 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.3.0 // indirect
github.com/aws/aws-sdk-go-v2/service/kms v1.5.0 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.4.0 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.7.0 // indirect
github.com/aws/smithy-go v1.8.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bkielbasa/cyclop v1.2.0 // indirect
github.com/blakesmith/ar v0.0.0-20190502131153-809d4375e1fb // indirect
Expand Down Expand Up @@ -78,8 +94,7 @@ require (
github.com/ettle/strcase v0.1.1 // indirect
github.com/fatih/color v1.12.0 // indirect
github.com/fatih/structtag v1.2.0 // indirect
github.com/form3tech-oss/jwt-go v3.2.2+incompatible // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/fzipp/gocyclo v0.3.1 // indirect
github.com/go-critic/go-critic v0.5.6 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
Expand All @@ -100,6 +115,7 @@ require (
github.com/gogo/googleapis v1.1.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/gogo/status v1.0.3 // indirect
github.com/golang-jwt/jwt/v4 v4.0.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2 // indirect
Expand All @@ -116,9 +132,9 @@ require (
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/rpmpack v0.0.0-20210410105602-e20c988a6f5a // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/wire v0.5.0 // indirect
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
github.com/googleapis/gax-go/v2 v2.1.0 // indirect
github.com/gordonklaus/ineffassign v0.0.0-20210225214923-2e10b2664254 // indirect
github.com/goreleaser/chglog v0.1.2 // indirect
github.com/goreleaser/fileglob v1.2.0 // indirect
Expand Down Expand Up @@ -146,7 +162,6 @@ require (
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/json-iterator/go v1.1.11 // indirect
github.com/jstemmer/go-junit-report v0.9.1 // indirect
github.com/julz/importas v0.0.0-20210419104244-841f0c0fe66d // indirect
github.com/kevinburke/ssh_config v1.1.0 // indirect
github.com/kisielk/errcheck v1.6.0 // indirect
Expand Down Expand Up @@ -238,27 +253,29 @@ require (
github.com/xanzy/ssh-agent v0.3.0 // indirect
github.com/yeya24/promlinter v0.1.0 // indirect
go.opencensus.io v0.23.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/goleak v1.1.10 // indirect
gocloud.dev v0.23.0 // indirect
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/net v0.0.0-20210505214959-0714010a04ed // indirect
golang.org/x/oauth2 v0.0.0-20210628180205-a41e5a781914 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/goleak v1.1.11-0.20210813005559-691160354723 // indirect
go.uber.org/multierr v1.7.0 // indirect
gocloud.dev v0.24.0 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/mod v0.5.0 // indirect
golang.org/x/net v0.0.0-20210825183410-e898025ed96a // indirect
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e // indirect
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
golang.org/x/tools v0.1.5 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
golang.org/x/tools v0.1.7 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/api v0.46.0 // indirect
google.golang.org/api v0.56.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
google.golang.org/grpc v1.38.0 // indirect
google.golang.org/protobuf v1.26.0 // indirect
google.golang.org/genproto v0.0.0-20210831024726-fe130286e0e2 // indirect
google.golang.org/grpc v1.40.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/mail.v2 v2.3.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
Expand Down
Loading

0 comments on commit 05aefab

Please sign in to comment.