-
Notifications
You must be signed in to change notification settings - Fork 0
golang #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
golang #1
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| .idea |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||||||
|
|
||||||
| * `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 | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ``` | ||||||
|
|
||||||
| ## License | ||||||
|
|
||||||
| Covered under the [MIT license](LICENSE.md). | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.