Skip to content
Merged
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
19 changes: 19 additions & 0 deletions LICENSE.BOILERPLATE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

DISCLAIMER

Copyright 2020 ArangoDB GmbH, Cologne, Germany

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Copyright holder is ArangoDB GmbH, Cologne, Germany

77 changes: 72 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ DOCKERIMAGE := $(DOCKERNAMESPACE)/arangodb-exporter:$(DOCKERTAG)
RELEASE := $(SRCDIR)/bin/release$(shell go env GOEXE)
GHRELEASE := $(SRCDIR)/bin/github-release$(shell go env GOEXE)
GOX := $(SRCDIR)/bin/gox$(shell go env GOEXE)
GOIMPORTS := $(SRCDIR)/bin/fmt$(shell go env GOEXE)
ADDLICENSE := $(SRCDIR)/bin/addlicense$(shell go env GOEXE)

# Magical rubbish to teach make what commas and spaces are.
EMPTY :=
Expand All @@ -53,6 +55,12 @@ COMMA := $(EMPTY),$(EMPTY)
ARCHS:=amd64 arm arm64
PLATFORMS:=$(subst $(SPACE),$(COMMA),$(foreach arch,$(ARCHS),linux/$(arch)))

GO_IGNORED:=vendor .gobuild

GO_SOURCES_QUERY := find $(SRCDIR) -name '*.go' -type f $(foreach IGNORED,$(GO_IGNORED),-not -path '$(SRCDIR)/$(IGNORED)/*' )
GO_SOURCES := $(shell $(GO_SOURCES_QUERY) | sort | uniq)
GO_SOURCES_PACKAGES := $(shell $(GO_SOURCES_QUERY) -exec dirname {} \; | sort | uniq)

SOURCES := $(shell find $(SRCDIR) -name '*.go' -not -path './test/*')

.PHONY: all clean build docker
Expand Down Expand Up @@ -91,31 +99,45 @@ docker-ubi-base: check-vars
docker build --no-cache -t $(DOCKERIMAGE)-base-image-ubi -f Dockerfile.ubi .

docker-ubi: docker-ubi-base build
ifndef LOCAL
for arch in amd64; do \
docker build --build-arg "GOARCH=$$arch" --build-arg "VERSION=$(VERSION_MAJOR_MINOR_PATCH)" -t $(DOCKERIMAGE)-ubi --build-arg "BASE_IMAGE=$(DOCKERIMAGE)-base-image-ubi" -f Dockerfile.scratch . ; \
docker push $(DOCKERIMAGE)-ubi ; \
done
else
for arch in amd64; do \
docker build --build-arg "GOARCH=$$arch" --build-arg "VERSION=$(VERSION_MAJOR_MINOR_PATCH)" -t $(DOCKERIMAGE)-ubi --build-arg "BASE_IMAGE=$(DOCKERIMAGE)-base-image-ubi" -f Dockerfile.scratch . ; \
done
endif

ifndef IGNORE_UBI
docker: docker-ubi
endif

docker: check-vars build
ifndef LOCAL
for arch in $(ARCHS); do \
docker build --build-arg "GOARCH=$$arch" --build-arg "VERSION=$(VERSION_MAJOR_MINOR_PATCH)" -t $(DOCKERIMAGE)-$$arch -f Dockerfile.scratch . ; \
docker push $(DOCKERIMAGE)-$$arch ; \
done
DOCKER_CLI_EXPERIMENTAL=enabled docker manifest create --amend $(DOCKERIMAGE) $(foreach arch,$(ARCHS),$(DOCKERIMAGE)-$(arch)) $(DOCKERIMAGE)-ubi
DOCKER_CLI_EXPERIMENTAL=enabled docker manifest push $(DOCKERIMAGE)
docker tag $(DOCKERIMAGE)-amd64 $(DOCKERIMAGE)
docker push $(DOCKERIMAGE)
else
for arch in $(ARCHS); do \
docker build --build-arg "GOARCH=$$arch" --build-arg "VERSION=$(VERSION_MAJOR_MINOR_PATCH)" -t $(DOCKERIMAGE)-$$arch -f Dockerfile.scratch . ; \
done
endif

$(RELEASE): $(GOBUILDDIR) $(SOURCES) $(GHRELEASE)
go build -o $(RELEASE) $(REPOPATH)/tools/release

$(GHRELEASE): $(GOBUILDDIR)
go build -o $(GHRELEASE) github.com/aktau/github-release
$(GHRELEASE): $(GOBUILDDIR)
@go build -mod='' -o "$(GHRELEASE)" github.com/aktau/github-release

github-release: $(GHRELEASE)

$(GOX):
go build -o $(GOX) github.com/mitchellh/gox
go build -mod='' -o $(GOX) github.com/mitchellh/gox

release-patch: $(RELEASE) $(GHRELEASE)
$(RELEASE) -type=patch
Expand All @@ -126,3 +148,48 @@ release-minor: $(RELEASE) $(GHRELEASE)
release-major: $(RELEASE) $(GHRELEASE)
$(RELEASE) -type=major

## LINT

GOLANGCI_ENABLED=deadcode gocyclo golint varcheck structcheck maligned errcheck \
ineffassign interfacer unconvert goconst \
megacheck

.PHONY: tools
tools: $(ADDLICENSE) $(GOIMPORTS) $(GHRELEASE)

$(ADDLICENSE):
@go build -mod='' -o "$(ADDLICENSE)" github.com/google/addlicense

.PHONY: license
license: $(ADDLICENSE)
@echo ">> Verify license of files"
@$(ADDLICENSE) -f "./LICENSE.BOILERPLATE" $(GO_SOURCES)

.PHONY: license-verify
license-verify: $(ADDLICENSE)
@echo ">> Ensuring license of files"
@$(ADDLICENSE) -f "./LICENSE.BOILERPLATE" -check $(GO_SOURCES)

$(GOIMPORTS):
@go build -mod='' -o "$(GOIMPORTS)" golang.org/x/tools/cmd/goimports

.PHONY: fmt
fmt: $(GOIMPORTS)
@echo ">> Ensuring style of files"
@$(GOIMPORTS) -w $(GO_SOURCES)

.PHONY: fmt-verify
fmt-verify: license-verify $(GOIMPORTS)
@echo ">> Verify files style"
@if [ X"$$($(GOIMPORTS) -l $(GO_SOURCES) | wc -l)" != X"0" ]; then echo ">> Style errors"; $(GOIMPORTS) -l $(GO_SOURCES); exit 1; fi

.PHONY: linter
linter: fmt
@golangci-lint run --no-config --issues-exit-code=1 --deadline=30m --disable-all \
$(foreach MODE,$(GOLANGCI_ENABLED),--enable $(MODE) ) \
--exclude-use-default=false \
$(GO_SOURCES_PACKAGES)

.PHONY: vendor
vendor:
@go mod vendor
51 changes: 51 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// DISCLAIMER
//
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Adam Janikowski
//

package main

import (
"io/ioutil"
"strings"
)

type Authentication func() (string, error)

func newAuthentication() Authentication {
if arangodbOptions.jwtFile != "" {
return func() (string, error) {

data, err := ioutil.ReadFile(arangodbOptions.jwtFile)
if err != nil {
return "", err
}

return strings.TrimSpace(string(data)), nil
}
} else if arangodbOptions.jwtSecret != "" {
return func() (string, error) {
return CreateArangodJWT(arangodbOptions.jwtSecret)
}
}
return func() (string, error) {
return "", nil
}
}
74 changes: 48 additions & 26 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func newMetric(group StatisticGroup, figure StatisticFigure) []prometheus.Collec
// Exporter collects ArangoDB statistics from the given endpoint and exports them using
// the prometheus metrics package.
type Exporter struct {
conn driver.Connection
factory connClientFactory
timeout time.Duration
mutex sync.RWMutex

Expand All @@ -102,31 +102,10 @@ type Exporter struct {
}

// NewExporter returns an initialized Exporter.
func NewExporter(arangodbEndpoint, jwt string, sslVerify bool, timeout time.Duration) (*Exporter, error) {
connCfg := driver_http.ConnectionConfig{
Endpoints: []string{arangodbEndpoint},
}
if !sslVerify {
connCfg.TLSConfig = &tls.Config{InsecureSkipVerify: true}
}
conn, err := driver_http.NewConnection(connCfg)
if err != nil {
return nil, maskAny(err)
}
if jwt != "" {
hdr, err := CreateArangodJwtAuthorizationHeader(jwt)
if err != nil {
return nil, maskAny(err)
}
auth := driver.RawAuthentication(hdr)
conn, err = conn.SetAuthentication(auth)
if err != nil {
return nil, maskAny(err)
}
}
func NewExporter(arangodbEndpoint string, jwt Authentication, sslVerify bool, timeout time.Duration) (*Exporter, error) {

return &Exporter{
conn: conn,
factory: newConnClientFactory(arangodbEndpoint, jwt, sslVerify, timeout),
timeout: timeout,
up: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Expand All @@ -147,6 +126,42 @@ func NewExporter(arangodbEndpoint, jwt string, sslVerify bool, timeout time.Dura
}, nil
}

type connClientFactory func() (driver.Connection, error)

func newConnClientFactory(arangodbEndpoint string, auth Authentication, sslVerify bool, timeout time.Duration) connClientFactory {
return func() (driver.Connection, error) {
connCfg := driver_http.ConnectionConfig{
Endpoints: []string{arangodbEndpoint},
}
if !sslVerify {
connCfg.TLSConfig = &tls.Config{InsecureSkipVerify: true}
}

jwt, err := auth()
if err != nil {
return nil, err
}

conn, err := driver_http.NewConnection(connCfg)
if err != nil {
return nil, maskAny(err)
}
if jwt != "" {
hdr, err := CreateArangodJwtAuthorizationHeader(jwt)
if err != nil {
return nil, maskAny(err)
}
auth := driver.RawAuthentication(hdr)
conn, err = conn.SetAuthentication(auth)
if err != nil {
return nil, maskAny(err)
}
}

return conn, nil
}
}

// Describe describes all the metrics ever exported by the HAProxy exporter. It
// implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
Expand Down Expand Up @@ -181,16 +196,23 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
func (e *Exporter) scrape(ctx context.Context) {
e.totalScrapes.Inc()

conn, err := e.factory()
if err != nil {
e.up.Set(0)
log.Errorf("Failed to create client: %v", err)
return
}

// Gather descriptions
descr, err := GetStatisticsDescription(ctx, e.conn)
descr, err := GetStatisticsDescription(ctx, conn)
if err != nil {
e.up.Set(0)
log.Errorf("Failed to fetch statistic descriptions: %v", err)
return
}

// Collect statistics
stats, err := GetStatistics(ctx, e.conn)
stats, err := GetStatistics(ctx, conn)
if err != nil {
e.up.Set(0)
log.Errorf("Failed to fetch statistics: %v", err)
Expand Down
25 changes: 4 additions & 21 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,20 @@ go 1.12
replace github.com/Sirupsen/logrus => github.com/sirupsen/logrus v1.4.1

require (
github.com/Sirupsen/logrus v0.0.0-00010101000000-000000000000 // indirect
github.com/aktau/github-release v0.7.2 // indirect
github.com/arangodb-helper/go-certificates v0.0.0-20180821055445-9fca24fc2680
github.com/arangodb/go-driver v0.0.0-20190430103524-b14f41496c3d
github.com/codegangsta/cli v1.20.0 // indirect
github.com/coreos/go-semver v0.3.0
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/docker/docker v1.13.1 // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-metrics v0.0.0-20181218153428-b84716841b82 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/estesp/manifest-tool v0.9.0 // indirect
github.com/go-yaml/yaml v2.1.0+incompatible // indirect
github.com/gorilla/mux v1.7.1 // indirect
github.com/google/addlicense v0.0.0-20200906110928-a0294312aa76 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mitchellh/gox v1.0.1 // indirect
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/opencontainers/runc v0.1.1 // indirect
github.com/opencontainers/runtime-spec v1.0.1 // indirect
github.com/pavel-v-chernykh/keystore-go v2.1.0+incompatible // indirect
github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v0.9.2
github.com/prometheus/common v0.3.0
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3 // indirect
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80 // indirect
github.com/vbatts/tar-split v0.11.1 // indirect
github.com/voxelbrain/goptions v0.0.0-20180630082107-58cddc247ea2 // indirect
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
google.golang.org/grpc v1.20.1 // indirect
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a // indirect
golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d // indirect
golang.org/x/tools v0.0.0-20201208233053-a543418bbed2 // indirect
)
Loading