Skip to content
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

Add flag for specifying configuration file path and include cluster name in metric labels #8

Merged
merged 6 commits into from
Jul 7, 2024
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ WORKDIR /app/

COPY --from=builder /nats-blackbox-exporter .

ENTRYPOINT ["./nats-blackbox-exporter"]
CMD [ "./nats-blackbox-exporter"]
13 changes: 11 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 ./setting/config.yaml:/app/setting/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> --configPath ./setting/config.yaml
```
and then pass environment variables as needed.

Expand All @@ -50,7 +50,16 @@ You can check the list of parameters with default values in the [config.example.
Set the necessary environment variables before running the exporter.

2. **Configuration File:**
Use a `config.yaml` file to specify the configuration parameters.

Use a `config.yaml` file to specify configuration parameters. You can specify the path to the config file using the `--configPath` flag.

Example usage:

```bash
./nats-blackbox-exporter --configPath /path/to/config.yaml
```

Replace `/path/to/config.yaml` with the actual path to your configuration file.

3. **Default Values:**
If neither environment variables nor a configuration file is provided, the exporter will use default values.
Expand Down
File renamed without changes.
3 changes: 0 additions & 3 deletions internal/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import (
func main(cfg config.Config, logger *zap.Logger) {
natsConfig := cfg.NATS

// client := natsclient.New(natsConfig, logger)
// client.StartMessaging()

jetstreamClient := natsclient.NewJetstream(natsConfig, logger)
jetstreamClient.StartBlackboxTest()

Expand Down
8 changes: 7 additions & 1 deletion internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
"github.com/snapp-incubator/nats-blackbox-exporter/internal/logger"
"github.com/snapp-incubator/nats-blackbox-exporter/internal/metric"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

// ExitFailure status code.
const ExitFailure = 1

var configPath string

func Execute() {
cfg := config.New()
pflag.StringVar(&configPath, "configPath", "./config.yaml", "Path to config file")
pflag.Parse()

Check warning on line 20 in internal/cmd/root.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/root.go#L19-L20

Added lines #L19 - L20 were not covered by tests

cfg := config.New(configPath)

Check warning on line 22 in internal/cmd/root.go

View check run for this annotation

Codecov / codecov/patch

internal/cmd/root.go#L22

Added line #L22 was not covered by tests

logger := logger.New(cfg.Logger)

Expand Down
6 changes: 3 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
)

// New reads configuration with koanf.
func New() Config {
func New(configPath string) Config {

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

View check run for this annotation

Codecov / codecov/patch

internal/config/config.go#L34

Added line #L34 was not covered by tests
var instance Config

k := koanf.New(".")
Expand All @@ -42,8 +42,8 @@
}

// load configuration from file
if err := k.Load(file.Provider("setting/config.yaml"), yaml.Parser()); err != nil {
log.Printf("error loading config.yaml")
if err := k.Load(file.Provider(configPath), yaml.Parser()); err != nil {
log.Printf("error loading config.yaml from: %s", configPath)

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

View check run for this annotation

Codecov / codecov/patch

internal/config/config.go#L45-L46

Added lines #L45 - L46 were not covered by tests
}

// load environment variables
Expand Down
23 changes: 14 additions & 9 deletions internal/natsclient/jetstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
j := &Jetstream{
config: &config,
logger: logger,
metrics: NewMetrics(config.ClientName),
metrics: NewMetrics(),

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

View check run for this annotation

Codecov / codecov/patch

internal/natsclient/jetstream.go#L37

Added line #L37 was not covered by tests
}

j.connect()
Expand Down Expand Up @@ -140,6 +140,7 @@
}

func (j *Jetstream) jetstreamSubscribe(h chan *Message, streamName string) {
clusterName := j.connection.ConnectedClusterName()

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

View check run for this annotation

Codecov / codecov/patch

internal/natsclient/jetstream.go#L143

Added line #L143 was not covered by tests
for msg := range h {
var publishTime time.Time
err := publishTime.UnmarshalBinary(msg.Data)
Expand All @@ -150,27 +151,30 @@
}
latency := time.Since(publishTime).Seconds()
j.metrics.Latency.With(prometheus.Labels{
"stream": streamName,
"stream": streamName,
"cluster": clusterName,

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

View check run for this annotation

Codecov / codecov/patch

internal/natsclient/jetstream.go#L154-L155

Added lines #L154 - L155 were not covered by tests
}).Observe(latency)
j.metrics.SuccessCounter.With(prometheus.Labels{
"type": successfulSubscribe,
"stream": streamName,
"type": successfulSubscribe,
"stream": streamName,
"cluster": clusterName,

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

View check run for this annotation

Codecov / codecov/patch

internal/natsclient/jetstream.go#L158-L160

Added lines #L158 - L160 were not covered by tests
}).Add(1)
j.logger.Info("Received message: ", zap.String("subject", msg.Subject), zap.Float64("latency", latency))
}
}

func (j *Jetstream) jetstreamPublish(subject string, streamName string) {
clusterName := j.connection.ConnectedClusterName()

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

View check run for this annotation

Codecov / codecov/patch

internal/natsclient/jetstream.go#L167

Added line #L167 was not covered by tests
for {
t, err := time.Now().MarshalBinary()
if err != nil {
j.logger.Error("could not marshal current time.", zap.Error(err))
}

if ack, err := j.jetstream.Publish(subject, t); err != nil {
j.metrics.SuccessCounter.With(prometheus.Labels{
"type": failedPublish,
"stream": streamName,
"type": failedPublish,
"stream": streamName,
"cluster": clusterName,

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

View check run for this annotation

Codecov / codecov/patch

internal/natsclient/jetstream.go#L175-L177

Added lines #L175 - L177 were not covered by tests
}).Add(1)
if err == nats.ErrTimeout {
j.logger.Error("Request timeout: No response received within the timeout period.")
Expand All @@ -181,8 +185,9 @@
}
} else {
j.metrics.SuccessCounter.With(prometheus.Labels{
"type": successfulPublish,
"stream": streamName,
"type": successfulPublish,
"stream": streamName,
"cluster": clusterName,

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

View check run for this annotation

Codecov / codecov/patch

internal/natsclient/jetstream.go#L188-L190

Added lines #L188 - L190 were not covered by tests
}).Add(1)
j.logger.Info("receive ack", zap.String("stream", ack.Stream))
}
Expand Down
15 changes: 8 additions & 7 deletions internal/natsclient/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

const (
Namespace = "nats_blackbox_exporter"
Subsystem = "client"
)

var latencyBuckets = []float64{
Expand Down Expand Up @@ -68,30 +69,30 @@
return *ev
}

func NewMetrics(clinetName string) Metrics {
func NewMetrics() Metrics {

Check warning on line 72 in internal/natsclient/metric.go

View check run for this annotation

Codecov / codecov/patch

internal/natsclient/metric.go#L72

Added line #L72 was not covered by tests
return Metrics{
Connection: newCounterVec(prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: clinetName,
Subsystem: Subsystem,

Check warning on line 76 in internal/natsclient/metric.go

View check run for this annotation

Codecov / codecov/patch

internal/natsclient/metric.go#L76

Added line #L76 was not covered by tests
Name: "connection_errors_total",
Help: "total number of disconnections and reconnections",
ConstLabels: nil,
}, []string{"type"}),
}, []string{"type", "cluster"}),

Check warning on line 80 in internal/natsclient/metric.go

View check run for this annotation

Codecov / codecov/patch

internal/natsclient/metric.go#L80

Added line #L80 was not covered by tests
// nolint: exhaustruct
Latency: newHistogramVec(prometheus.HistogramOpts{
Namespace: Namespace,
Subsystem: clinetName,
Subsystem: Subsystem,

Check warning on line 84 in internal/natsclient/metric.go

View check run for this annotation

Codecov / codecov/patch

internal/natsclient/metric.go#L84

Added line #L84 was not covered by tests
Name: "latency",
Help: "from publish to consume duration in seconds",
ConstLabels: nil,
Buckets: latencyBuckets,
}, []string{"stream"}),
}, []string{"stream", "cluster"}),

Check warning on line 89 in internal/natsclient/metric.go

View check run for this annotation

Codecov / codecov/patch

internal/natsclient/metric.go#L89

Added line #L89 was not covered by tests
SuccessCounter: newCounterVec(prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: clinetName,
Subsystem: Subsystem,

Check warning on line 92 in internal/natsclient/metric.go

View check run for this annotation

Codecov / codecov/patch

internal/natsclient/metric.go#L92

Added line #L92 was not covered by tests
Name: "success_counter",
Help: "publish and consume success rate",
ConstLabels: nil,
}, []string{"type", "stream"}),
}, []string{"type", "stream", "cluster"}),

Check warning on line 96 in internal/natsclient/metric.go

View check run for this annotation

Codecov / codecov/patch

internal/natsclient/metric.go#L96

Added line #L96 was not covered by tests
}
}
Loading