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

config&notifier: Add option to use Alertmanager API v2 #5482

Merged
merged 1 commit into from
Jun 24, 2019

Conversation

mxinden
Copy link
Member

@mxinden mxinden commented Apr 18, 2019

With v0.16.0 Alertmanager introduced a new API (v2). This patch adds a
configuration option for Prometheus to send alerts to the v2 endpoint
instead of the defautl v1 endpoint.

Fixes #5167

@@ -490,13 +513,40 @@ func (n *Manager) sendAll(alerts ...*Alert) bool {
wg.Done()
}(ams, am)
}
ams.mtx.RUnlock()
Copy link
Member Author

@mxinden mxinden Apr 18, 2019

Choose a reason for hiding this comment

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

This would unlock the read lock, even though the spawned go routines still had references to ams, resulting in possible race conditions. Instead, the lock is dropped at the end of the function via defer now. This is safe as the function waits for all spawned goroutines before exiting.

Copy link
Member

Choose a reason for hiding this comment

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

Another option would be to still release the lock here and pass the necessary inputs to the goroutine:

ams.mtx.RLock()
for _, am := range ams.ams {
   ...
   go func(c *http.Client, u string) {
       if err := n.sendOne(ctx, c, u, b); err != nil {
       ...
      }
   }(ams.client, am.url().String())
}
ams.mtx.RUnlock()

Copy link
Member Author

Choose a reason for hiding this comment

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

That's even better, thanks @simonpasquier.

"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
config_util "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/prometheus/common/version"

"github.com/prometheus/alertmanager/api/v2/models"
Copy link
Member Author

Choose a reason for hiding this comment

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

This patch vendors the Alertmanager API v2 model. From my point of view, this gives us a strong contract between the two APIs. Let me know what you think.

Copy link
Member

Choose a reason for hiding this comment

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

Looks good to me.

@simonpasquier simonpasquier self-requested a review April 18, 2019 14:41
Copy link
Contributor

@brian-brazil brian-brazil left a comment

Choose a reason for hiding this comment

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

Docs need updating.

config/config.go Outdated
return err
}

// Default to Alertmanager API v1.
Copy link
Contributor

Choose a reason for hiding this comment

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

This isn't how we generally implement defaults.

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed to the default way of doing defaults. Thanks.

@@ -490,13 +513,40 @@ func (n *Manager) sendAll(alerts ...*Alert) bool {
wg.Done()
}(ams, am)
}
ams.mtx.RUnlock()
Copy link
Member

Choose a reason for hiding this comment

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

Another option would be to still release the lock here and pass the necessary inputs to the goroutine:

ams.mtx.RLock()
for _, am := range ams.ams {
   ...
   go func(c *http.Client, u string) {
       if err := n.sendOne(ctx, c, u, b); err != nil {
       ...
      }
   }(ams.client, am.url().String())
}
ams.mtx.RUnlock()

ams.mtx.RLock()
defer ams.mtx.RUnlock()

switch ams.cfg.APIVersion {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe it could be cached to avoid serializing several times the same data.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. Done.

"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
config_util "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/prometheus/common/version"

"github.com/prometheus/alertmanager/api/v2/models"
Copy link
Member

Choose a reason for hiding this comment

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

Looks good to me.

@mxinden mxinden force-pushed the am-v2 branch 4 times, most recently from 348ac70 to 585381b Compare April 26, 2019 13:15
@mxinden
Copy link
Member Author

mxinden commented Apr 26, 2019

@brian-brazil I updated docs/configuration/configuration.md. Any other docs that I am missing?

@brian-brazil
Copy link
Contributor

There's Alerting -> Clients, but that's not in this repo.

With v0.16.0 Alertmanager introduced a new API (v2). This patch adds a
configuration option for Prometheus to send alerts to the v2 endpoint
instead of the defautl v1 endpoint.

Signed-off-by: Max Leonard Inden <[email protected]>
@mxinden mxinden marked this pull request as ready for review June 21, 2019 14:44
@mxinden
Copy link
Member Author

mxinden commented Jun 21, 2019

@simonpasquier @brian-brazil any further thoughts on this?

@brian-brazil
Copy link
Contributor

👍

@mxinden
Copy link
Member Author

mxinden commented Jun 24, 2019

Tested this on a larger setup as well.

Thanks for the help.

@mxinden mxinden merged commit 10444e8 into prometheus:master Jun 24, 2019
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.

prometheus still send alert to alertmanager/api/v1
3 participants