Skip to content

Commit 82033a3

Browse files
authored
Merge pull request #1 from code-tool/golang
golang
2 parents 9bd97ef + e04ddd8 commit 82033a3

File tree

14 files changed

+1217
-1
lines changed

14 files changed

+1217
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

README.md

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,93 @@
11
# inventor
2+
23
Prometheus HTTP SD implementation
34

4-
## helm chart
5+
## Description
6+
7+
The Inventor is a Prometheus HTTP SD Server allows users to dynamcially add or remove prometheus targets and labels and expose it to a [Prometheus HTTP SD](https://prometheus.io/docs/prometheus/latest/http_sd/) job.
8+
9+
## Usage
10+
11+
Running the server:
12+
```
13+
cd src
14+
go run main.go
15+
```
16+
17+
Installing with Helm
518
```bash
619
helm repo add inventor https://code-tool.github.io/inventor/
720
```
21+
22+
Registering new target:
23+
```bash
24+
curl -X PUT -H "x-api-token: secret" http://127.0.0.1:9101/target \
25+
-d '{"static_config": {"targets": ["10.0.10.2:9100",], "labels": {"__meta_datacenter": "dc-01", "__meta_prometheus_job": "node"}, "target_group": "mygroup"}}'
26+
```
27+
28+
More examples: `./test/end-to-end`
29+
30+
Prometheus SD config example
31+
```yaml
32+
scrape_configs:
33+
- job_name: http_sd
34+
http_sd_configs:
35+
- url: http://127.0.0.1:9101/discover
36+
# if SD_TOKEN env variable is set
37+
headers:
38+
- "x-sd-token: REDACTED"
39+
40+
```
41+
42+
Prometheus SD config with groups example
43+
```yaml
44+
scrape_configs:
45+
- job_name: http_sd_mygroup
46+
http_sd_configs:
47+
- url: http://127.0.0.1:9101/group?name=mygroup
48+
# if SD_TOKEN env variable is set
49+
headers:
50+
- "x-sd-token: REDACTED"
51+
52+
```
53+
54+
55+
## Configuration Environmet Valiables
56+
57+
* `REDIS_ADDR`: redis server addres to store metrics and targets
58+
* `REDIS_PORT`: redis server port
59+
* `REDIS_DBNO`: redis server keyspace
60+
* `TTL_SECONDS`: ttl for storing target, default is 6h (21600 seconds)
61+
* `API_TOKEN`: API token for manipulating targets
62+
* `SD_TOKEN`: Options token for Prometheus HTTP SD, is empty by default and not validating (header `x-sd-token`)
63+
64+
## API Methods
65+
66+
* **GET /discover**
67+
* Returning the list of targets in Prometheus HTTP SD format
68+
* **GET /group**
69+
* Returning targets by group name `/group?name=mygroup`
70+
* **PUT /target**
71+
* Adds the new target
72+
* **GET /target**
73+
* Returning target by ID
74+
* **DELETE /target**
75+
* Removing target by ID
76+
* **GET /metrics**
77+
* Metrics in prometheus format
78+
* **GET /healthcheck**
79+
* Health Check for kubernetes deployments
80+
81+
82+
## Build Docker image
83+
```bash
84+
docker build -t ghcr.io/code-tool/inventor/inventor:$(cat VERSION.txt) --build-arg BUILD_VERSION=$(cat VERSION.txt) -f docker/Dockerfile .
85+
```
86+
pulling image:
87+
```bash
88+
ghcr.io/code-tool/inventor/inventor:0.0.3
89+
```
90+
91+
## License
92+
93+
Covered under the [MIT license](LICENSE.md).

docker/Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM golang:1.21.11-bullseye AS builder
2+
ARG BUILD_VERSION='1.2.3'
3+
ENV LISTEN_PORT=9101
4+
WORKDIR /opt/inventor/
5+
COPY go.mod ./
6+
COPY go.sum .
7+
COPY src ./src
8+
RUN go mod download
9+
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags "-X main.buildVersion=${BUILD_VERSION}" -a -installsuffix cgo -o inventor src/main.go
10+
11+
FROM debian:bullseye
12+
WORKDIR /opt/inventor/
13+
RUN apt-get update \
14+
&& apt-get install -y --no-install-recommends \
15+
curl \
16+
&& rm -rf /var/cache/apt/archives/*
17+
COPY --from=builder /opt/inventor/inventor .
18+
19+
EXPOSE ${LISTEN_PORT}
20+
CMD ["/opt/inventor/inventor"]

go.mod

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module inventor
2+
3+
go 1.21
4+
5+
require (
6+
github.com/go-redis/redis/v9 v9.0.0-beta.2
7+
github.com/google/uuid v1.0.0
8+
github.com/joho/godotenv v1.4.0
9+
github.com/prometheus/client_golang v1.13.0
10+
)
11+
12+
require (
13+
github.com/beorn7/perks v1.0.1 // indirect
14+
github.com/cespare/xxhash/v2 v2.1.2 // indirect
15+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
16+
github.com/golang/protobuf v1.5.2 // indirect
17+
github.com/gorilla/mux v1.8.1
18+
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
19+
github.com/prometheus/client_model v0.2.0 // indirect
20+
github.com/prometheus/common v0.37.0 // indirect
21+
github.com/prometheus/procfs v0.8.0 // indirect
22+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
23+
google.golang.org/protobuf v1.28.1 // indirect
24+
)

go.sum

Lines changed: 502 additions & 0 deletions
Large diffs are not rendered by default.

src/.env

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
APP_ENV="dev"
2+
API_TOKEN="secret"
3+
SD_TOKEN=""
4+
LISTEN_PORT="9101"
5+
REDIS_ADDR="127.0.0.1"
6+
REDIS_PORT="6379"
7+
REDIS_DBNO="7"
8+
TTL_SECONDS="300"

src/config/config.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package config
2+
3+
import (
4+
"log"
5+
"os"
6+
"reflect"
7+
8+
"github.com/joho/godotenv"
9+
)
10+
11+
type Config struct {
12+
APP_ENV string
13+
API_TOKEN string
14+
SD_TOKEN string
15+
LISTEN_PORT string
16+
REDIS_ADDR string
17+
REDIS_PORT string
18+
REDIS_DBNO string
19+
TTL_SECONDS string
20+
}
21+
22+
var config *Config
23+
24+
func GetConfig() Config {
25+
return *config
26+
}
27+
28+
func loadEnvFile() {
29+
log.Println("Loading .env file.")
30+
err := godotenv.Load(".env")
31+
if err != nil {
32+
panic("Error loading .env file.")
33+
}
34+
}
35+
36+
func init() {
37+
config = &Config{}
38+
_, found := os.LookupEnv("APP_ENV")
39+
if !found {
40+
loadEnvFile()
41+
}
42+
_, found = os.LookupEnv("LISTEN_PORT")
43+
if !found {
44+
os.Setenv("LISTEN_PORT", "80")
45+
}
46+
_, found = os.LookupEnv("SD_TOKEN")
47+
if !found {
48+
os.Setenv("SD_TOKEN", "")
49+
}
50+
refl := reflect.ValueOf(config).Elem()
51+
numFields := refl.NumField()
52+
for i := 0; i < numFields; i++ {
53+
envName := refl.Type().Field(i).Name
54+
envVal, foud := os.LookupEnv(envName)
55+
if !foud {
56+
panic("Environment [" + envName + "] not found.")
57+
}
58+
refl.Field(i).SetString(envVal)
59+
}
60+
}

0 commit comments

Comments
 (0)