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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
88 changes: 87 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,93 @@
# inventor

Prometheus HTTP SD implementation

## helm chart
## Description

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.

## Usage

Running the server:
```
cd src
go run main.go
```

Installing with Helm
```bash
helm repo add inventor https://code-tool.github.io/inventor/
```

Registering new target:
```bash
curl -X PUT -H "x-api-token: secret" http://127.0.0.1:9101/target \
-d '{"static_config": {"targets": ["10.0.10.2:9100",], "labels": {"__meta_datacenter": "dc-01", "__meta_prometheus_job": "node"}, "target_group": "mygroup"}}'
```

More examples: `./test/end-to-end`

Prometheus SD config example
```yaml
scrape_configs:
- job_name: http_sd
http_sd_configs:
- url: http://127.0.0.1:9101/discover
# if SD_TOKEN env variable is set
headers:
- "x-sd-token: REDACTED"

```

Prometheus SD config with groups example
```yaml
scrape_configs:
- job_name: http_sd_mygroup
http_sd_configs:
- url: http://127.0.0.1:9101/group?name=mygroup
# if SD_TOKEN env variable is set
headers:
- "x-sd-token: REDACTED"

```


## Configuration Environmet Valiables

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Configuration Environmet Valiables
## Configuration Environment Valiables


* `REDIS_ADDR`: redis server addres to store metrics and targets
* `REDIS_PORT`: redis server port
* `REDIS_DBNO`: redis server keyspace
* `TTL_SECONDS`: ttl for storing target, default is 6h (21600 seconds)
* `API_TOKEN`: API token for manipulating targets
* `SD_TOKEN`: Options token for Prometheus HTTP SD, is empty by default and not validating (header `x-sd-token`)

## API Methods

* **GET /discover**
* Returning the list of targets in Prometheus HTTP SD format
* **GET /group**
* Returning targets by group name `/group?name=mygroup`
* **PUT /target**
* Adds the new target
* **GET /target**
* Returning target by ID
* **DELETE /target**
* Removing target by ID
* **GET /metrics**
* Metrics in prometheus format
* **GET /healthcheck**
* Health Check for kubernetes deployments


## Build Docker image
```bash
docker build -t ghcr.io/code-tool/inventor/inventor:$(cat VERSION.txt) --build-arg BUILD_VERSION=$(cat VERSION.txt) -f docker/Dockerfile .
```
pulling image:
```bash
ghcr.io/code-tool/inventor/inventor:0.0.3

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ghcr.io/code-tool/inventor/inventor:0.0.3
docker pull ghcr.io/code-tool/inventor/inventor:0.0.3

```

## License

Covered under the [MIT license](LICENSE.md).
20 changes: 20 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM golang:1.21.11-bullseye AS builder
ARG BUILD_VERSION='1.2.3'
ENV LISTEN_PORT=9101
WORKDIR /opt/inventor/
COPY go.mod ./
COPY go.sum .
COPY src ./src
RUN go mod download
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags "-X main.buildVersion=${BUILD_VERSION}" -a -installsuffix cgo -o inventor src/main.go

FROM debian:bullseye
WORKDIR /opt/inventor/
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
curl \
&& rm -rf /var/cache/apt/archives/*
COPY --from=builder /opt/inventor/inventor .

EXPOSE ${LISTEN_PORT}
CMD ["/opt/inventor/inventor"]
24 changes: 24 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module inventor

go 1.21

require (
github.com/go-redis/redis/v9 v9.0.0-beta.2
github.com/google/uuid v1.0.0
github.com/joho/godotenv v1.4.0
github.com/prometheus/client_golang v1.13.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/mux v1.8.1
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
502 changes: 502 additions & 0 deletions go.sum

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
APP_ENV="dev"
API_TOKEN="secret"
SD_TOKEN=""
LISTEN_PORT="9101"
REDIS_ADDR="127.0.0.1"
REDIS_PORT="6379"
REDIS_DBNO="7"
TTL_SECONDS="300"
60 changes: 60 additions & 0 deletions src/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package config

import (
"log"
"os"
"reflect"

"github.com/joho/godotenv"
)

type Config struct {
APP_ENV string
API_TOKEN string
SD_TOKEN string
LISTEN_PORT string
REDIS_ADDR string
REDIS_PORT string
REDIS_DBNO string
TTL_SECONDS string
}

var config *Config

func GetConfig() Config {
return *config
}

func loadEnvFile() {
log.Println("Loading .env file.")
err := godotenv.Load(".env")
if err != nil {
panic("Error loading .env file.")
}
}

func init() {
config = &Config{}
_, found := os.LookupEnv("APP_ENV")
if !found {
loadEnvFile()
}
_, found = os.LookupEnv("LISTEN_PORT")
if !found {
os.Setenv("LISTEN_PORT", "80")
}
_, found = os.LookupEnv("SD_TOKEN")
if !found {
os.Setenv("SD_TOKEN", "")
}
refl := reflect.ValueOf(config).Elem()
numFields := refl.NumField()
for i := 0; i < numFields; i++ {
envName := refl.Type().Field(i).Name
envVal, foud := os.LookupEnv(envName)
if !foud {
panic("Environment [" + envName + "] not found.")
}
refl.Field(i).SetString(envVal)
}
}
Loading