Skip to content

Commit

Permalink
fix: fix problems related to config and stream creation
Browse files Browse the repository at this point in the history
  • Loading branch information
kianaza committed Jun 25, 2024
1 parent 41ff526 commit aab74a0
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:<release-tag>
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:<release-tag>
```
and then pass environment variables as needed.

Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Check warning on line 45 in internal/config/config.go

View check run for this annotation

Codecov / codecov/patch

internal/config/config.go#L45

Added line #L45 was not covered by tests
log.Printf("error loading config.yaml")
}

Expand Down
1 change: 1 addition & 0 deletions internal/config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func Default() Config {
Level: "debug",
},
NATS: natsclient.Config{
NewStreamAllow: true,

Check warning on line 19 in internal/config/default.go

View check run for this annotation

Codecov / codecov/patch

internal/config/default.go#L19

Added line #L19 was not covered by tests
Stream: natsclient.Stream{
Name: "stream",
Subject: "test",
Expand Down
1 change: 1 addition & 0 deletions internal/natsclient/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
28 changes: 19 additions & 9 deletions internal/natsclient/jetstream.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package natsclient

import (
"fmt"
"time"

"github.com/nats-io/nats.go"
Expand Down Expand Up @@ -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))

Check warning on line 75 in internal/natsclient/jetstream.go

View check run for this annotation

Codecov / codecov/patch

internal/natsclient/jetstream.go#L68-L75

Added lines #L68 - L75 were not covered by tests
}
} 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))

Check warning on line 83 in internal/natsclient/jetstream.go

View check run for this annotation

Codecov / codecov/patch

internal/natsclient/jetstream.go#L77-L83

Added lines #L77 - L83 were not covered by tests
}
} else {
j.logger.Panic("could not add subject", zap.Error(err))

Check warning on line 86 in internal/natsclient/jetstream.go

View check run for this annotation

Codecov / codecov/patch

internal/natsclient/jetstream.go#L85-L86

Added lines #L85 - L86 were not covered by tests
}
}

Expand Down
5 changes: 3 additions & 2 deletions config.example.yaml → setting/config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit aab74a0

Please sign in to comment.