Skip to content

Conversation

@Bre77
Copy link

@Bre77 Bre77 commented Apr 2, 2025

Description

Adds a producer for NATS. I am using this in my environment so am sharing it back.

Fixes # (issue)

Type of change

Please select all options that apply to this change:

  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Bug fix (non-breaking change which fixes an issue)
  • Documentation update

Checklist:

Confirm you have completed the following steps:

  • My code follows the style of this project.
  • I have performed a self-review of my code.
  • I have made corresponding updates to the documentation.
  • I have added/updated unit tests to cover my changes.
  • I have added/updated integration tests to cover my changes.

Bre77 and others added 16 commits April 2, 2025 14:17
Add a NATS publisher implementation and corresponding tests.

* **config/config.go**
  - Add NATS configuration struct to the Config struct.
  - Add NATS configuration to the ConfigureProducers method.
* **datastore/nats/nats.go**
  - Implement Producer struct for NATS.
  - Implement NewProducer function to initialize NATS producer.
  - Implement Produce method to send messages to NATS.
  - Implement Close method to close NATS connection.
  - Implement ProcessReliableAck method to handle reliable acknowledgements.
  - Implement ReportError method to report errors.
* **datastore/nats/nats_test.go**
  - Implement unit tests for NewProducer function.
  - Implement unit tests for Produce method.
  - Implement unit tests for Close method.
  - Implement unit tests for ProcessReliableAck method.
  - Implement unit tests for ReportError method.
* **.devcontainer/devcontainer.json**
  - Add devcontainer configuration for building the project.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/Teslemetry/tesla-fleet-telemetry?shareId=XXXX-XXXX-XXXX-XXXX).
* Add `nats.go` dependency to `go.mod`
* Add `nats.go`, `nkeys`, `nuid`, `compress`, `crypto`, `sync`, and `text` dependencies to `go.sum`
* Add import for `github.com/teslamotors/fleet-telemetry/metrics/adapter`
* Add tests for error handling, metrics, and different payload types in `TestProduce`
* Fix references to `metricsRegistry` fields to use exported fields
* Remove unused import for `context`

Update `.devcontainer/devcontainer.json`

* Add "test" task with command "make test"
This reverts commit 1ab5c27.
This reverts commit 5c1ce35.
func NewProducer(config *Config, namespace string, prometheusEnabled bool, metricsCollector metrics.MetricCollector, airbrakeHandler *airbrake.Handler, ackChan chan (*telemetry.Record), reliableAckTxTypes map[string]interface{}, logger *logrus.Logger) (telemetry.Producer, error) {
registerMetricsOnce(metricsCollector)

natsConn, err := NatsConnect(config.URL)
Copy link

Choose a reason for hiding this comment

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

I think it'd be a good idea to add some options:

nats.RetryOnFailedConnect(true), 
nats.MaxReconnects(-1), // so that it will not stop reconnecting

and potentially logging using

ClosedHandler
ErrorHandler
ConnectHandler
ReconnectErrHandler
DisconnectErrHandler

for visibility.

@zlymeda
Copy link

zlymeda commented Apr 2, 2025

nice, I was also thinking about this, but was kinda busy. currently, I just use mqtt on NATS and then use a jetstream to persist the messages and move them to KV.

@Bre77
Copy link
Author

Bre77 commented Apr 2, 2025

nice, I was also thinking about this, but was kinda busy. currently, I just use mqtt on NATS and then use a jetstream to persist the messages and move them to KV.

I started with that but disliked the way the data was structured, and lost some of the other metadata.

@joeblew999
Copy link

I use nats also and have a Tesla.

@Bre77 Bre77 closed this Jan 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants