Skip to content

Add Elasticsearch notification#1452

Merged
crazy-max merged 2 commits intocrazy-max:masterfrom
robin-moser:master
Aug 29, 2025
Merged

Add Elasticsearch notification#1452
crazy-max merged 2 commits intocrazy-max:masterfrom
robin-moser:master

Conversation

@robin-moser
Copy link
Contributor

This PR adds the option to send the update information to an elasticsearch instance.
I used the message.RenderJSON to generate the same body which the webhook notifier uses, but to differentiate multiple diun instances who can send to the same Elasticsearch index, I added a configurable client field that get's also sent.

I tested on a local one-node elasticsearch Cluster, I will test again the next days on a production Cluster.

@robin-moser
Copy link
Contributor Author

robin-moser commented Jul 15, 2025

I optimized the module a bit, now the tests should succeed. I now also tested it on an authenticated multi-node cluster and tested the error handling, if elasticsearch returns errors itself. So from my site it's ready to review.

The image robinmoser/diun:dev is built with the latest commits of this PR.

Bildschirmfoto 2025-07-15 um 13 00 01

@robin-moser robin-moser marked this pull request as ready for review July 15, 2025 11:03
@robin-moser robin-moser requested a review from crazy-max as a code owner July 15, 2025 11:03
Copy link
Owner

@crazy-max crazy-max left a comment

Choose a reason for hiding this comment

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

Sorry for the delay, overall LGTM thanks!

Left a review related to recent changes. When addressed please squash your commits 🙏

@robin-moser
Copy link
Contributor Author

Awesome, thanks for reviewing, I'll get to it the next few days!

@crazy-max
Copy link
Owner

I optimized the module a bit, now the tests should succeed. I now also tested it on an authenticated multi-node cluster and tested the error handling, if elasticsearch returns errors itself. So from my site it's ready to review.

The image robinmoser/diun:dev is built with the latest commits of this PR.
Bildschirmfoto 2025-07-15 um 13 00 01

And feel free to add this screenshot in the docs page as well in a "Sample" section similar to https://crazymax.dev/diun/notif/mail/#sample or https://crazymax.dev/diun/notif/discord/#sample

@robin-moser robin-moser force-pushed the master branch 2 times, most recently from 24e0140 to f0f141e Compare August 9, 2025 13:27
Elsasticsearch notification: add test cases

Elasticsearch notification: make timeout configurable

Elsasticsearch notification: add @timestamp field to JSON data

Elsasticsearch notification: improve error handling

use context.WithTimeoutCause

Co-authored-by: CrazyMax <[email protected]>

better comment the elaticsearch api endpoint

add screenshot to documentation
@robin-moser
Copy link
Contributor Author

@crazy-max
I committed your requested changes, added the screenshot and squashed my commits.

Copy link
Owner

@crazy-max crazy-max left a comment

Choose a reason for hiding this comment

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

Thanks, left a review about the client, let me know what you think.

Comment on lines 85 to 88
// Build the Elasticsearch indexing URL
// This uses the Index API (POST /{index}/_doc) to create a document with an auto-generated _id:
// https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-create
url := fmt.Sprintf("%s://%s:%d/%s/_doc", c.cfg.Scheme, c.cfg.Host, c.cfg.Port, c.cfg.Index)
Copy link
Owner

Choose a reason for hiding this comment

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

I looked a bit more at the docs and wonder if we could use their go client instead? https://github.com/elastic/go-elasticsearch

Also I think we should remove Scheme, Host and Port and replace them with an Address one that makes more sense looking at the API docs https://pkg.go.dev/github.com/elastic/go-elasticsearch#pkg-overview

So instead of:

  elasticsearch:
    scheme: https
    host: localhost
    port: 9200

It would be:

  elasticsearch:
    address: https://localhost:9200

Seems also they support multiple addresses, maybe we should too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are good points. I initially oriented myself at the mqtt and webhook implementation and tried to stay as close to them as possible to have uniform code, but it makes sense to implement it properly with their client. I'll look into it, thanks for your input.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd have to test, how forgiving their client is regarding elasticsearch version compatibility. The _doc endpoint is probably the most essential and basic endpoint, generally unchanged since version 5. So by using their go client, hopefully they don't restrict version compatibility, which would make it harder to maitain the integration.

I'll come with some tests, probably by the end of next week.

Copy link
Owner

Choose a reason for hiding this comment

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

Thanks, I pushed an extra commit to at least switch to Address field and also validate it using url package.

Comment on lines 32 to 34
s.Scheme = "http"
s.Host = "localhost"
s.Port = 9200
Copy link
Owner

Choose a reason for hiding this comment

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

http://localhost:9200 looks good as default looking at https://pkg.go.dev/github.com/elastic/go-elasticsearch#NewClient but I would prefer to just have an Address related to my previous comment.

Copy link
Owner

@crazy-max crazy-max left a comment

Choose a reason for hiding this comment

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

Getting this one in, we can look at using the official go client https://github.com/elastic/go-elasticsearch as follow-up.

@crazy-max crazy-max merged commit f97bdb7 into crazy-max:master Aug 29, 2025
44 checks passed
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.

2 participants