diff --git a/README.md b/README.md index cae8966..6d05e85 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ You can deploy and use NATS Blackbox Exporter using Docker images or by building ### 1. Using Docker Image 📫 You can use pre-built Docker images from GitHub Container Registry (GHCR): ```bash -docker run -d -p 8080:8080 --name nats-blackbox-exporter -v ./config.yaml:/app/config.yaml:ro ghcr.io/snapp-incubator/nats-blackbox-exporter: +docker run -d -p 8080:8080 --name nats-blackbox-exporter -v ./setting/config.yaml:/app/setting/config.yaml:ro ghcr.io/snapp-incubator/nats-blackbox-exporter: ``` and then pass environment variables as needed. @@ -44,7 +44,7 @@ The exporter will generate Prometheus metrics on the port specified in the confi - **SuccessCounter:** A `prometheus.CounterVec` that counts successful publishes and consumes. ## 🎨 Configuration -You can check the list of parameters with default values in the [config.example.yaml](./config.example.yaml) file. The NATS Blackbox Exporter can be configured in three ways: +You can check the list of parameters with default values in the [config.example.yaml](./setting/config.example.yaml) file. The NATS Blackbox Exporter can be configured in three ways: 1. **Environment Variables:** Set the necessary environment variables before running the exporter. diff --git a/internal/config/config.go b/internal/config/config.go index 871e3c8..9596a02 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -42,7 +42,7 @@ func New() Config { } // load configuration from file - if err := k.Load(file.Provider("config.yaml"), yaml.Parser()); err != nil { + if err := k.Load(file.Provider("setting/config.yaml"), yaml.Parser()); err != nil { log.Printf("error loading config.yaml") } diff --git a/internal/config/default.go b/internal/config/default.go index 2386da8..0c521da 100644 --- a/internal/config/default.go +++ b/internal/config/default.go @@ -16,6 +16,7 @@ func Default() Config { Level: "debug", }, NATS: natsclient.Config{ + NewStreamAllow: true, Stream: natsclient.Stream{ Name: "stream", Subject: "test", diff --git a/internal/natsclient/config.go b/internal/natsclient/config.go index de064f4..db78074 100644 --- a/internal/natsclient/config.go +++ b/internal/natsclient/config.go @@ -3,6 +3,7 @@ package natsclient import "time" type Config struct { + NewStreamAllow bool `json:"new_stream_allow" koanf:"new_stream_allow"` Stream Stream `json:"stream,omitempty" koanf:"stream"` URL string `json:"url,omitempty" koanf:"url"` PublishInterval time.Duration `json:"publish_interval" koanf:"publish_interval"` diff --git a/internal/natsclient/jetstream.go b/internal/natsclient/jetstream.go index 57df8a2..3ca25a9 100644 --- a/internal/natsclient/jetstream.go +++ b/internal/natsclient/jetstream.go @@ -1,7 +1,6 @@ package natsclient import ( - "fmt" "time" "github.com/nats-io/nats.go" @@ -66,14 +65,25 @@ func (j *Jetstream) createJetstreamContext() { } func (j *Jetstream) createStream() { - fmt.Println([]string{j.config.Stream.Subject}) - fmt.Println(j.config.Stream.Name) - _, err := j.jetstream.AddStream(&nats.StreamConfig{ - Name: j.config.Stream.Name, - Subjects: []string{j.config.Stream.Subject}, - }) - if err != nil { - j.logger.Panic("could not add stream", zap.Error(err)) + _, err := j.jetstream.StreamInfo(j.config.Stream.Name) + if err == nil { + _, err = j.jetstream.UpdateStream(&nats.StreamConfig{ + Name: j.config.Stream.Name, + Subjects: []string{j.config.Stream.Subject}, + }) + if err != nil { + j.logger.Panic("could not add subject to existing stream", zap.Error(err)) + } + } else if err == nats.ErrStreamNotFound && j.config.NewStreamAllow { + _, err = j.jetstream.AddStream(&nats.StreamConfig{ + Name: j.config.Stream.Name, + Subjects: []string{j.config.Stream.Subject}, + }) + if err != nil { + j.logger.Panic("could not add stream", zap.Error(err)) + } + } else { + j.logger.Panic("could not add subject", zap.Error(err)) } } diff --git a/config.example.yaml b/setting/config.example.yaml similarity index 77% rename from config.example.yaml rename to setting/config.example.yaml index 8c7cc41..b028e62 100644 --- a/config.example.yaml +++ b/setting/config.example.yaml @@ -2,9 +2,10 @@ logger: level: "debug" nats: + new_stream_allow: true stream: - - name: "stream" - subject: "test" # optional + name: "stream" + subject: "test" # optional url: "localhost:4222" publish_interval: 2s request_timeout: 50ms