Skip to content

Commit 34f3586

Browse files
committed
Major optimizations and improvements
1 parent 9e35e3d commit 34f3586

File tree

12 files changed

+424
-203
lines changed

12 files changed

+424
-203
lines changed

.github/workflows/release.yml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Release prom-exporter
2+
3+
on:
4+
push:
5+
branches-ignore:
6+
- '**'
7+
tags:
8+
- 'v*.*.*'
9+
10+
permissions:
11+
contents: write
12+
13+
jobs:
14+
goreleaser:
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@v4
20+
with:
21+
fetch-depth: 0
22+
23+
- name: Set up Go
24+
uses: actions/setup-go@master
25+
with:
26+
go-version: 1.21.x
27+
28+
- name: Prepare
29+
id: prepare
30+
run: echo "tag_name=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
31+
32+
- name: Release
33+
uses: goreleaser/goreleaser-action@v5
34+
with:
35+
version: latest
36+
args: release --clean --timeout=5m
37+
env:
38+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
39+
VERSION: ${{ steps.prepare.outputs.tag_name }}

.goreleaser.yml

+2
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ builds:
1414
- -s -w
1515
archives:
1616
- format: binary
17+
checksum:
18+
disable: true
1719
snapshot:
1820
name_template: "{{ .Tag }}-next"

exporter/builders.go

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package exporter
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/prometheus/client_golang/prometheus"
7+
)
8+
9+
var (
10+
fqname string
11+
labels prometheus.Labels = nil
12+
)
13+
14+
func (e *Exporter) build(m []string, k int) (string, prometheus.Labels) {
15+
metricSlices := split(m[0], "_")
16+
17+
if len(metricSlices) == 1 {
18+
fqname, labels = e.buildShortMetrics(m, k, metricSlices[0])
19+
} else if len(metricSlices) >= 2 {
20+
fqname, labels = e.buildLongMetrics(m, metricSlices, k)
21+
}
22+
23+
return fqname, labels
24+
}
25+
26+
func (e *Exporter) buildShortMetrics(m []string, k int, part string) (string, prometheus.Labels) {
27+
labels = nil
28+
29+
if k < (len(e.data) - 1) {
30+
future := split(e.data[k+1], RAW_METRIC_DELIM)
31+
if m[0] == future[0] {
32+
fqname = prometheus.BuildFQName(e.name, "", part)
33+
labels = prometheus.Labels{"metric": m[1]}
34+
35+
} else {
36+
if k == 0 {
37+
fqname = prometheus.BuildFQName(e.name, part, m[1])
38+
39+
} else {
40+
future = split(e.data[k-1], RAW_METRIC_DELIM)
41+
if m[0] == future[0] {
42+
fqname = prometheus.BuildFQName(e.name, "", part)
43+
labels = prometheus.Labels{"metric": m[1]}
44+
45+
} else {
46+
fqname = prometheus.BuildFQName(e.name, part, m[1])
47+
}
48+
}
49+
}
50+
} else if k == (len(e.data) - 1) {
51+
future := split(e.data[k-1], RAW_METRIC_DELIM)
52+
if m[0] == future[0] {
53+
fqname = prometheus.BuildFQName(e.name, "", part)
54+
labels = prometheus.Labels{"metric": m[1]}
55+
56+
} else {
57+
fqname = prometheus.BuildFQName(e.name, part, m[1])
58+
}
59+
} else {
60+
fqname = prometheus.BuildFQName(e.name, part, m[1])
61+
}
62+
63+
return fqname, labels
64+
}
65+
66+
func (e *Exporter) buildLongMetrics(m, slices []string, k int) (string, prometheus.Labels) {
67+
labels = nil
68+
69+
if k < (len(e.data) - 1) {
70+
future := split(e.data[k+1], RAW_METRIC_DELIM)
71+
if m[0] == future[0] {
72+
fqname = prometheus.BuildFQName(
73+
e.name, fmt.Sprintf("%s", join(slices[0:len(slices)-1], "_")), slices[len(slices)-1])
74+
labels = prometheus.Labels{"metric": m[1]}
75+
76+
} else {
77+
if k == 0 {
78+
fqname = prometheus.BuildFQName(e.name, fmt.Sprintf("%s", join(slices[0:], "_")), m[1])
79+
80+
} else {
81+
future := split(e.data[k-1], RAW_METRIC_DELIM)
82+
if m[0] == future[0] {
83+
fqname = prometheus.BuildFQName(
84+
e.name, fmt.Sprintf("%s", join(slices[0:len(slices)-1], "_")), slices[len(slices)-1])
85+
labels = prometheus.Labels{"metric": m[1]}
86+
87+
} else {
88+
fqname = prometheus.BuildFQName(e.name, fmt.Sprintf("%s", join(slices[0:], "_")), m[1])
89+
}
90+
}
91+
}
92+
} else if k == (len(e.data) - 1) {
93+
future := split(e.data[k-1], RAW_METRIC_DELIM)
94+
if m[0] == future[0] {
95+
fqname = prometheus.BuildFQName(
96+
e.name, fmt.Sprintf("%s", join(slices[0:len(slices)-1], "_")), slices[len(slices)-1])
97+
labels = prometheus.Labels{"metric": m[1]}
98+
99+
} else {
100+
fqname = prometheus.BuildFQName(e.name, fmt.Sprintf("%s", join(slices[0:], "_")), m[1])
101+
}
102+
}
103+
104+
return fqname, labels
105+
}

exporter/constants.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package exporter
2+
3+
const (
4+
// HELP describe for service metric [service_up]
5+
SERVICE_UP_HELP = "Target Up"
6+
7+
// key to be injected on aggregated metric
8+
METRIC_STRING = "metric"
9+
10+
// delimiter for raw data extracted from remote service
11+
RAW_METRIC_DELIM = "::"
12+
)

0 commit comments

Comments
 (0)