Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Windows platform support, add the Golangci check code, compile… #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ vendor
.vscode
.DS_Store
beat-exporter
beat-exporter.exe
beat-exporter.exe
/bin
22 changes: 22 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
linters-settings:
golint:
min-confidence: 0
misspell:
locale: US
linters:
disable-all: true
enable:
- typecheck
- varcheck
- goimports
- misspell
- govet
- golint
- ineffassign
- gosimple
- deadcode
- structcheck
- unused
- errcheck
service:
golangci-lint-version: 1.41.1 # use the fixed version to not introduce new linters unexpectedly
23 changes: 18 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
FROM quay.io/prometheus/busybox:latest
LABEL MAINTAINER="Audrius Karabanovas <[email protected]>"
FROM golang:1.16.10-alpine3.13 AS builder

COPY .build/linux-amd64/beat-exporter /bin/beat-exporter
WORKDIR /root/go/src/beat-exporter

EXPOSE 9479
ENTRYPOINT [ "/bin/beat-exporter" ]
ENV GOPROXY https://goproxy.cn
ENV GOPATH /root/go
COPY go.mod .
COPY go.sum .
RUN go mod download

COPY . .
RUN CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -a -o beat-exporter

FROM alpine:3.13 AS final

WORKDIR /bin
COPY --from=builder /root/go/src/beat-exporter/beat-exporter /bin/

EXPOSE 9479
ENTRYPOINT ["/bin/beat-exporter"]
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
BUILD_BIN_PATH := $(shell pwd)/bin
BUILD_BIN_NAME := beat-exporter

VERSION_PKG := beat-exporter/internal/service

LD_FLAGS += -X "$(VERSION_PKG).releaseVersion=$(shell git describe --tags --dirty --always)"
LD_FLAGS += -X "$(VERSION_PKG).buildDate=$(shell date -u '+%Y-%m-%d %I:%M:%S')"
LD_FLAGS += -X "$(VERSION_PKG).gitHash=$(shell git rev-parse HEAD)"
LD_FLAGS += -X "$(VERSION_PKG).gitBranch=$(shell git rev-parse --abbrev-ref HEAD)"

GOVER_MAJOR := $(shell go version | sed -E -e "s/.*go([0-9]+)[.]([0-9]+).*/\1/")
GOVER_MINOR := $(shell go version | sed -E -e "s/.*go([0-9]+)[.]([0-9]+).*/\2/")
GO111 := $(shell [ $(GOVER_MAJOR) -gt 1 ] || [ $(GOVER_MAJOR) -eq 1 ] && [ $(GOVER_MINOR) -ge 11 ]; echo $$?)
ifeq ($(GO111), 1)
$(error "go below 1.11 does not support modules")
endif

default: check build

build-deps:
@mkdir -p bin
build: export GO111MODULE=on
build: build-deps
go build -ldflags '$(LD_FLAGS)' -o $(BUILD_BIN_PATH)/$(BUILD_BIN_NAME) main.go

check: check-static
check-static: tools/bin/golangci-lint
tools/bin/golangci-lint run -v --deadline=3m --config ./.golangci.yaml $$($(PACKAGE_DIRECTORIES))
tools/bin/golangci-lint:
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ./tools/bin v1.41.1;
34 changes: 0 additions & 34 deletions build.sh

This file was deleted.

34 changes: 0 additions & 34 deletions create-artifacts.sh

This file was deleted.

3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/trustpilot/beat-exporter
module beat-exporter

go 1.12

Expand All @@ -7,5 +7,4 @@ require (
github.com/prometheus/client_golang v1.3.0
github.com/prometheus/common v0.8.0
github.com/sirupsen/logrus v1.4.2
golang.org/x/sys v0.0.0-20200113162924-86b910548bc1
)
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5Fsn
github.com/prometheus/client_golang v1.3.0 h1:miYCvYqFXtl/J9FIy8eNpBfYthAEFg+Ys0XyUVEcDsc=
github.com/prometheus/client_golang v1.3.0/go.mod h1:hJaj2vgQTGQmVCsAACORcieXFeDPbaTKGT+JTgUa3og=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 h1:S/YWwWx/RA8rT8tKFRuGUZhuA90OyIBpPCXkcbwU8DE=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.1.0 h1:ElTg5tNp4DqfV7UQjDqv2+RJlNzsDtvNAWccbItceIE=
github.com/prometheus/client_model v0.1.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand Down
135 changes: 135 additions & 0 deletions internal/service/exporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package service

import (
"context"
"encoding/json"
"io/ioutil"
"net"
"net/http"
"net/url"
"time"

"beat-exporter/collector"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/version"
log "github.com/sirupsen/logrus"
)

type Params struct {
ServiceName string
ListenAddress string
TLSCertFile string
TLSKeyFile string
MetricsPath string
BeatURI string
BeatTimeout time.Duration
SystemBeat bool
}

func NewParams(
listenAddress,
tlsCertFile,
tlsKeyFile,
metricsPath,
beatURI string,
BeatTimeout time.Duration,
systemBeat bool) *Params {
return &Params{
ServiceName: "beat_exporter",
ListenAddress: listenAddress,
TLSCertFile: tlsCertFile,
TLSKeyFile: tlsKeyFile,
MetricsPath: metricsPath,
BeatURI: beatURI,
BeatTimeout: BeatTimeout,
SystemBeat: systemBeat,
}
}

type Exporter struct {
ServiceParams *Params
BeatURL *url.URL
HTTPClient *http.Client
BeatInfo *collector.BeatInfo
PrometheusRegistry *prometheus.Registry
}

func NewExporter(params *Params) *Exporter {
exporter := &Exporter{ServiceParams: params}
beatURL, err := url.Parse(exporter.ServiceParams.BeatURI)
if err != nil {
log.Fatalf("failed to parse beat.uri, error: %v", err)
panic(err)
}
exporter.BeatURL = beatURL
exporter.HTTPClient = &http.Client{
Timeout: exporter.ServiceParams.BeatTimeout,
}

exporter.handleURL()
exporter.loadBeatInfo()
exporter.loadPrometheusRegistry()
return exporter
}

func (e *Exporter) handleURL() {
if e.BeatURL.Scheme == "unix" {
unixPath := e.BeatURL.Path
e.BeatURL.Scheme = "http"
e.BeatURL.Host = "localhost"
e.BeatURL.Path = ""
e.HTTPClient.Transport = &http.Transport{
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, "unix", unixPath)
},
}
}
}

func (e *Exporter) loadBeatInfo() {
response, err := e.HTTPClient.Get(e.BeatURL.String())
if err != nil {
log.Errorf("Could not load beat type, with error: %v", err)
panic(err)
}
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
log.Errorf("Beat URL: %q status code: %d", e.BeatURL.String(), response.StatusCode)
panic(err)
}

bodyBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Error("Can't read body of response")
panic(err)
}

beatInfo := &collector.BeatInfo{}
if err := json.Unmarshal(bodyBytes, &beatInfo); err != nil {
log.Error("Could not parse JSON response for target")
panic(err)
}

log.WithFields(
log.Fields{
"beat": beatInfo.Beat,
"version": beatInfo.Version,
"name": beatInfo.Name,
"hostname": beatInfo.Hostname,
"uuid": beatInfo.UUID,
}).Info("Target beat configuration loaded successfully!")

e.BeatInfo = beatInfo
}

func (e *Exporter) loadPrometheusRegistry() {
params := e.ServiceParams
// version metric
e.PrometheusRegistry = prometheus.NewRegistry()
versionMetric := version.NewCollector(params.ServiceName)
mainCollector := collector.NewMainCollector(e.HTTPClient, e.BeatURL, params.ServiceName, e.BeatInfo, params.SystemBeat)
e.PrometheusRegistry.MustRegister(versionMetric)
e.PrometheusRegistry.MustRegister(mainCollector)
}
24 changes: 0 additions & 24 deletions internal/service/handler.go

This file was deleted.

55 changes: 0 additions & 55 deletions internal/service/handler_windows.go

This file was deleted.

Loading