Skip to content

Commit

Permalink
Merge pull request #7 from ricoberger/prepare-next-release
Browse files Browse the repository at this point in the history
Prepare next release
  • Loading branch information
ricoberger authored Jul 4, 2019
2 parents 16da63e + 8a6e8d6 commit bda189f
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 72 deletions.
79 changes: 56 additions & 23 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,45 +1,78 @@
WHAT := script_exporter
BUILDTIME := $(shell date +%FT%T%Z)
VERSION=`git describe --tags`
COMMIT=`git rev-parse HEAD`
BUILDUSER := $(shell id -un)
WHAT := curltest script_exporter

.PHONY: build build-darwin-amd64 build-linux-amd64 build-windows-amd64 release
PROJECT ?= script_exporter
REPO ?= github.com/ricoberger/script_exporter
PWD ?= $(shell pwd)
VERSION ?= $(shell git describe --tags)
REVISION ?= $(shell git rev-parse HEAD)
BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD)
BUILDUSER ?= $(shell id -un)
BUILDTIME ?= $(shell date '+%Y%m%d-%H:%M:%S')

.PHONY: build build-darwin-amd64 build-linux-amd64 build-windows-amd64 clean release release-major release-minor release-patch

build:
for target in $(WHAT); do \
go build -ldflags "-X github.com/ricoberger/script_exporter/pkg/version.GitCommit=${COMMIT} \
-X github.com/ricoberger/script_exporter/pkg/version.Version=${VERSION} \
-X github.com/ricoberger/script_exporter/pkg/version.BuildTime=${BUILDTIME} \
-X github.com/ricoberger/script_exporter/pkg/version.BuildUser=${BUILDUSER}" \
go build -ldflags "-X ${REPO}/pkg/version.Version=${VERSION} \
-X ${REPO}/pkg/version.Revision=${REVISION} \
-X ${REPO}/pkg/version.Branch=${BRANCH} \
-X ${REPO}/pkg/version.BuildUser=${BUILDUSER} \
-X ${REPO}/pkg/version.BuildDate=${BUILDTIME}" \
-o ./bin/$$target ./cmd/$$target; \
done

build-darwin-amd64:
for target in $(WHAT); do \
CGO_ENABLED=0 GOARCH=amd64 GOOS=darwin go build -a -installsuffix cgo -ldflags "-X github.com/ricoberger/script_exporter/pkg/version.GitCommit=${COMMIT} \
-X github.com/ricoberger/script_exporter/pkg/version.Version=${VERSION} \
-X github.com/ricoberger/script_exporter/pkg/version.BuildTime=${BUILDTIME} \
-X github.com/ricoberger/script_exporter/pkg/version.BuildUser=${BUILDUSER}" \
CGO_ENABLED=0 GOARCH=amd64 GOOS=darwin go build -a -installsuffix cgo -ldflags "-X ${REPO}/pkg/version.Version=${VERSION} \
-X ${REPO}/pkg/version.Revision=${REVISION} \
-X ${REPO}/pkg/version.Branch=${BRANCH} \
-X ${REPO}/pkg/version.BuildUser=${BUILDUSER} \
-X ${REPO}/pkg/version.BuildDate=${BUILDTIME}" \
-o ./bin/$$target-${VERSION}-darwin-amd64 ./cmd/$$target; \
done

build-linux-amd64:
for target in $(WHAT); do \
CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -a -installsuffix cgo -ldflags "-X github.com/ricoberger/script_exporter/pkg/version.GitCommit=${COMMIT} \
-X github.com/ricoberger/script_exporter/pkg/version.Version=${VERSION} \
-X github.com/ricoberger/script_exporter/pkg/version.BuildTime=${BUILDTIME} \
-X github.com/ricoberger/script_exporter/pkg/version.BuildUser=${BUILDUSER}" \
CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -a -installsuffix cgo -ldflags "-X ${REPO}/pkg/version.Version=${VERSION} \
-X ${REPO}/pkg/version.Revision=${REVISION} \
-X ${REPO}/pkg/version.Branch=${BRANCH} \
-X ${REPO}/pkg/version.BuildUser=${BUILDUSER} \
-X ${REPO}/pkg/version.BuildDate=${BUILDTIME}" \
-o ./bin/$$target-${VERSION}-linux-amd64 ./cmd/$$target; \
done

build-windows-amd64:
for target in $(WHAT); do \
CGO_ENABLED=0 GOARCH=amd64 GOOS=windows go build -a -installsuffix cgo -ldflags "-X github.com/ricoberger/script_exporter/pkg/version.GitCommit=${COMMIT} \
-X github.com/ricoberger/script_exporter/pkg/version.Version=${VERSION} \
-X github.com/ricoberger/script_exporter/pkg/version.BuildTime=${BUILDTIME} \
-X github.com/ricoberger/script_exporter/pkg/version.BuildUser=${BUILDUSER}" \
CGO_ENABLED=0 GOARCH=amd64 GOOS=windows go build -a -installsuffix cgo -ldflags "-X ${REPO}/pkg/version.Version=${VERSION} \
-X ${REPO}/pkg/version.Revision=${REVISION} \
-X ${REPO}/pkg/version.Branch=${BRANCH} \
-X ${REPO}/pkg/version.BuildUser=${BUILDUSER} \
-X ${REPO}/pkg/version.BuildDate=${BUILDTIME}" \
-o ./bin/$$target-${VERSION}-windows-amd64.exe ./cmd/$$target; \
done

release: build-darwin-amd64 build-linux-amd64 build-windows-amd64
clean:
rm -rf ./bin

release: clean build-darwin-amd64 build-linux-amd64 build-windows-amd64

release-major:
$(eval MAJORVERSION=$(shell git describe --tags --abbrev=0 | sed s/v// | awk -F. '{print "v"$$1+1".0.0"}'))
git checkout master
git pull
git tag -a $(MAJORVERSION) -m 'release $(MAJORVERSION)'
git push origin --tags

release-minor:
$(eval MINORVERSION=$(shell git describe --tags --abbrev=0 | sed s/v// | awk -F. '{print "v"$$1"."$$2+1".0"}'))
git checkout master
git pull
git tag -a $(MINORVERSION) -m 'release $(MINORVERSION)'
git push origin --tags

release-patch:
$(eval PATCHVERSION=$(shell git describe --tags --abbrev=0 | sed s/v// | awk -F. '{print "v"$$1"."$$2"."$$3+1}'))
git checkout master
git pull
git tag -a $(PATCHVERSION) -m 'release $(PATCHVERSION)'
git push origin --tags
25 changes: 5 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,12 @@ Running:
./bin/script_exporter
```

Then visit [http://localhost:9469/metrics?script=test&prefix=test](http://localhost:9469/metrics?script=test&prefix=test) in the browser of your choice. There you should see the following output:
Then visit [http://localhost:9469](http://localhost:9469) in the browser of your choice. There you have access to the following examples:

```
# HELP script_success Script exit status (0 = error, 1 = success).
# TYPE script_success gauge
script_success{} 1
# HELP script_duration_seconds Script execution time, in seconds.
# TYPE script_duration_seconds gauge
script_duration_seconds{} 0.006133
# HELP test_first_test
# TYPE test_first_test gauge
test_first_test{label="test_1_label_1"} 1
# HELP test_second_test
# TYPE test_second_test gauge
test_second_test{label="test_2_label_1",label="test_2_label_2"} 2.71828182846
# HELP test_third_test
# TYPE test_third_test gauge
test_third_test{} 3.14159265359
```

You can also visit the following url for a more complex example. The `ping` example uses the `params` parameter to check if a `target` is reachable: [http://localhost:9469/metrics?script=ping&prefix=test&params=target&target=example.com](http://localhost:9469/metrics?script=ping&prefix=test&params=target&target=example.com)
- [test](http://localhost:9469/metrics?script=test&prefix=test): Invalid values which are returned by the script are omitted.
- [ping](http://localhost:9469/metrics?script=ping&prefix=test&params=target&target=example.com): Pings the specified address in the `target` parameter and returns if it was successful or not.
- [helloworld](http://localhost:9469/metrics?script=helloworld): Returns the specified argument in the `script` as label.
- [curltest](http://localhost:9469/metrics?script=curltest&params=target&target=https://example.com): Runs a binary, which performs a get request against the specified `target` and returns the status code.

## Usage and configuration

Expand Down
25 changes: 25 additions & 0 deletions cmd/curltest/curltest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"
"net/http"
"os"
)

func main() {
if len(os.Args) != 2 {
os.Exit(1)
}

t := os.Args[1]
res, err := http.Get(t)
if err != nil {
os.Exit(1)
}

defer res.Body.Close()

fmt.Printf("# HELP curl_status_code returns the status code of the target\n")
fmt.Printf("# TYPE curl_status_code gauge\n")
fmt.Printf("curl_status_code{target=\"%s\"} %d\n", t, res.StatusCode)
}
30 changes: 14 additions & 16 deletions cmd/script_exporter/script_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func metricsHandler(w http.ResponseWriter, r *http.Request) {

// Get and run script
script := exporterConfig.GetScript(scriptName)
if scriptName == "" {
if script == "" {
log.Printf("Script not found\n")
http.Error(w, "Script not found", http.StatusBadRequest)
return
Expand Down Expand Up @@ -135,16 +135,14 @@ func main() {
// Parse command-line flags
flag.Parse()

// Format build time
buildTime, _ := time.Parse("2006-01-02T15:04:05MST", version.BuildTime)

// Show version information
if *showVersion {
fmt.Printf("script_exporter, version %s, by %s\n", version.Version, version.Author)
fmt.Printf(" build user: %s\n", version.BuildUser)
fmt.Printf(" build date: %s\n", buildTime.Format(time.RFC1123))
fmt.Printf(" build commit: %s\n", version.GitCommit)
fmt.Printf(" go version: %s\n", version.GoVersion)
v, err := version.Print("script_exporter")
if err != nil {
log.Fatalf("Failed to print version information: %#v", err)
}

fmt.Fprintln(os.Stdout, v)
os.Exit(0)
}

Expand All @@ -166,9 +164,9 @@ func main() {
}

// Start exporter
log.Printf("Starting script_exporter, version %s\n", version.Version)
log.Printf("Build go=%s, user=%s, date=%s, commit=%s\n", version.GoVersion, version.BuildUser, buildTime.Format(time.RFC1123), version.GitCommit)
log.Printf("Listening on %s\n", *listenAddress)
fmt.Printf("Starting server %s\n", version.Info())
fmt.Printf("Build context %s\n", version.BuildContext())
fmt.Printf("script_exporter listening on %s\n", *listenAddress)

http.HandleFunc(*metricsPath, use(metricsHandler, auth))
http.HandleFunc("/", use(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -179,11 +177,11 @@ func main() {
<p><a href='` + *metricsPath + `'>Metrics</a></p>
<p><ul>
<li>version: ` + version.Version + `</li>
<li>author: ` + version.Author + `</li>
<li>build user: ` + version.BuildUser + `</li>
<li>build date: ` + buildTime.Format(time.RFC1123) + `</li>
<li>build commit: ` + version.GitCommit + `</li>
<li>branch: ` + version.Branch + `</li>
<li>revision: ` + version.Revision + `</li>
<li>go version: ` + version.GoVersion + `</li>
<li>build user: ` + version.BuildUser + `</li>
<li>build date: ` + version.BuildDate + `</li>
</ul></p>
</body>
</html>`))
Expand Down
2 changes: 2 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ scripts:
script: ./examples/ping.sh
- name: helloworld
script: ./examples/helloworld.sh test
- name: curltest
script: ./bin/curltest
61 changes: 48 additions & 13 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,60 @@
package version

import (
"bytes"
"fmt"
"runtime"
"strings"
"text/template"
)

// Build information. Populated at build-time.
var (
// Version contains the version which is defined in the MAKEFILE
Version string
Version string
Revision string
Branch string
BuildUser string
BuildDate string
GoVersion = runtime.Version()
)

// BuildTime is the time when the binaries where build
BuildTime string
// versionInfoTmpl contains the template used by Print.
var versionInfoTmpl = `
{{.program}}, version {{.version}} (branch: {{.branch}}, revision: {{.revision}})
build user: {{.buildUser}}
build date: {{.buildDate}}
go version: {{.goVersion}}
`

// BuildUser is the name of the user which build the binaries
BuildUser string
// Print returns version information.
func Print(program string) (string, error) {
m := map[string]string{
"program": program,
"version": Version,
"revision": Revision,
"branch": Branch,
"buildUser": BuildUser,
"buildDate": BuildDate,
"goVersion": GoVersion,
}
t, err := template.New("version").Parse(versionInfoTmpl)
if err != nil {
return "", err
}

// GitCommit is the git commit on which the binaries where build
GitCommit string
var buf bytes.Buffer
if err := t.ExecuteTemplate(&buf, "version", m); err != nil {
return "", err
}
return strings.TrimSpace(buf.String()), nil
}

// GoVersion is the version of go with which the program was build
GoVersion = runtime.Version()
// Info returns version, branch and revision information.
func Info() string {
return fmt.Sprintf("(version=%s, branch=%s, revision=%s)", Version, Branch, Revision)
}

// Author hold author information
Author = "Rico Berger <mail@ricoberger.de>"
)
// BuildContext returns goVersion, buildUser and buildDate information.
func BuildContext() string {
return fmt.Sprintf("(go=%s, user=%s, date=%s)", GoVersion, BuildUser, BuildDate)
}

0 comments on commit bda189f

Please sign in to comment.