-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
opsgenie: Add visible_to #3958
base: main
Are you sure you want to change the base?
opsgenie: Add visible_to #3958
Conversation
dc1641f
to
656407e
Compare
Add visible_to with the same support for templating and fields as the existing responders field. Opsgenie documentation: Teams and users that the alert will become visible to without sending any notification. type field is mandatory for each item, where possible values are team and user. In addition to the type field, either id or name should be given for teams and either id or username should be given for users. Please note: that alert will be visible to the teams that are specified within responders field by default, so there is no need to re-specify them within visibleTo field. You can refer below for example values. The functionality has been tested with unit tests, and with the following configuration: route: receiver: og group_wait: 30s group_interval: 5m repeat_interval: 12h receivers: - name: og opsgenie_configs: - send_resolved: true api_key: ...top secret... api_url: https://api.eu.opsgenie.com/ message: 'Lab message' description: 'Static description' source: lab details: foo: bar priority: P4 visible_to: - type: user username: [email protected] Fixes prometheus#3953 Signed-off-by: Carl Henrik Lunde <[email protected]>
656407e
to
db0f43f
Compare
return fmt.Errorf("opsGenieConfig visible_to %v has to have at least one of id, username or name specified", v) | ||
} | ||
|
||
if strings.Contains(v.Type, "{{") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid this check doesn't work. Consider the following example:
this is an invalid template }}
I suppose you could also check for strings.Contains(v.Type, "}}")
?
Perhaps it would be better to just remove this if statement and check all VisibleTo's using template.New("").Parse()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to match the behaviour for receivers, this code was introduced by 9ae6113.
In the case of this is an invalid template }}
it will not be considered as a valid template, and ender the "else" case below, which will complain that it does not match the regex for valid types.
But you're right, at least I should not have reused opsgenieTypeMatcher
, which is ^(team|teams|user|escalation|schedule)$
. Fixed in new commit.
@@ -638,6 +657,16 @@ type OpsGenieConfigResponder struct { | |||
Type string `yaml:"type,omitempty" json:"type,omitempty"` | |||
} | |||
|
|||
type OpsGenieConfigVisibleTo struct { | |||
// One of those 3 should be filled. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment doesn't match the behavior in the code. The code allows for at least one to be filled, not at most one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to match the battle tested code for receivers here too. After considering changing the code, I think it is right.
We want to send exactly one of those fields to the OpsGenie API.
We must verify that at least one is set.
We probably should not disallow setting two or three as templates, as after templating, it is likely that exactly one has a value.
Of course, we could add a lot of validation to check that exactly one is set if it is not templates, and allow 1-3 set if they are all templates, but I'm not sure if it is worth the complexity?
docs/configuration.md
Outdated
#### `<visible_to>` | ||
|
||
```yaml | ||
# Exactly one of these fields should be defined. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copied from the receivers docs, but we could try to be more precise.
Something like:
Exactly one of these fields must have a value after templating. If none of the fields, including
type
, are set, the array item will be ignored.
Fixed in new commit.
Type: tmpl(v.Type), | ||
} | ||
|
||
if visibleTo == (opsGenieCreateMessageVisibleTo{}) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would someone want to do this? Is it to catch badly written templates that produce no output?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is identical to receivers, for consistent user experience.
Seems like this was introduced in 9ddc5f1
There is an explaination in the comment below. Is this OK?
// Filter out empty responders. This is useful if you want to fill
// responders dynamically from alert's common labels.
notify/opsgenie/opsgenie.go
Outdated
teams := safeSplit(visibleTo.Name, ",") | ||
for _, team := range teams { | ||
newVisibleTo := opsGenieCreateMessageVisibleTo{ | ||
Name: tmpl(team), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why template the name a second time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copy and paste from receivers, I did not catch that, thanks! Would you like me to remove the double templating in the code above too? Or have a dedicated PR for that? It does not change the results of the unit tests.
Fixed in new commit.
notify/opsgenie/opsgenie.go
Outdated
for _, team := range teams { | ||
newVisibleTo := opsGenieCreateMessageVisibleTo{ | ||
Name: tmpl(team), | ||
Type: tmpl("team"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't make sense to me. You could template it back to user
after we've checked that it is teams
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should just be Type: "team"
. Fixed in new commit.
Not sure if I understand the reference to user
here?
continue | ||
} | ||
|
||
if visibleTo.Type == "teams" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where does teams
plural come from btw? In the documentation all I can see is team
and user
?
type field is mandatory for each item, where possible values are team and user
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is identical to receivers, for consistent user experience.
Seems like this was introduced in 05490d8
It is an extension in alert manager not covered by the opsgenie API and documentation, but it is described in configuration.md:
# The `teams` type is configured using the `name` field above.
# This field can contain a comma-separated list of team names.
# If the list is empty, no additional visibility will be configured.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review, sorry for the delay. I added a new commit that addresses some of the code review comments.
But a lot of the comments apply to the existing code, so I would like to get your input again on them before making further changes. Could you mark the ones you think are OK as resolved, and comment on the others?
Type: tmpl(v.Type), | ||
} | ||
|
||
if visibleTo == (opsGenieCreateMessageVisibleTo{}) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is identical to receivers, for consistent user experience.
Seems like this was introduced in 9ddc5f1
There is an explaination in the comment below. Is this OK?
// Filter out empty responders. This is useful if you want to fill
// responders dynamically from alert's common labels.
return fmt.Errorf("opsGenieConfig visible_to %v has to have at least one of id, username or name specified", v) | ||
} | ||
|
||
if strings.Contains(v.Type, "{{") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to match the behaviour for receivers, this code was introduced by 9ae6113.
In the case of this is an invalid template }}
it will not be considered as a valid template, and ender the "else" case below, which will complain that it does not match the regex for valid types.
But you're right, at least I should not have reused opsgenieTypeMatcher
, which is ^(team|teams|user|escalation|schedule)$
. Fixed in new commit.
@@ -638,6 +657,16 @@ type OpsGenieConfigResponder struct { | |||
Type string `yaml:"type,omitempty" json:"type,omitempty"` | |||
} | |||
|
|||
type OpsGenieConfigVisibleTo struct { | |||
// One of those 3 should be filled. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to match the battle tested code for receivers here too. After considering changing the code, I think it is right.
We want to send exactly one of those fields to the OpsGenie API.
We must verify that at least one is set.
We probably should not disallow setting two or three as templates, as after templating, it is likely that exactly one has a value.
Of course, we could add a lot of validation to check that exactly one is set if it is not templates, and allow 1-3 set if they are all templates, but I'm not sure if it is worth the complexity?
docs/configuration.md
Outdated
#### `<visible_to>` | ||
|
||
```yaml | ||
# Exactly one of these fields should be defined. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copied from the receivers docs, but we could try to be more precise.
Something like:
Exactly one of these fields must have a value after templating. If none of the fields, including
type
, are set, the array item will be ignored.
Fixed in new commit.
continue | ||
} | ||
|
||
if visibleTo.Type == "teams" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is identical to receivers, for consistent user experience.
Seems like this was introduced in 05490d8
It is an extension in alert manager not covered by the opsgenie API and documentation, but it is described in configuration.md:
# The `teams` type is configured using the `name` field above.
# This field can contain a comma-separated list of team names.
# If the list is empty, no additional visibility will be configured.
notify/opsgenie/opsgenie.go
Outdated
teams := safeSplit(visibleTo.Name, ",") | ||
for _, team := range teams { | ||
newVisibleTo := opsGenieCreateMessageVisibleTo{ | ||
Name: tmpl(team), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copy and paste from receivers, I did not catch that, thanks! Would you like me to remove the double templating in the code above too? Or have a dedicated PR for that? It does not change the results of the unit tests.
Fixed in new commit.
notify/opsgenie/opsgenie.go
Outdated
for _, team := range teams { | ||
newVisibleTo := opsGenieCreateMessageVisibleTo{ | ||
Name: tmpl(team), | ||
Type: tmpl("team"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should just be Type: "team"
. Fixed in new commit.
Not sure if I understand the reference to user
here?
Signed-off-by: Carl Henrik Lunde <[email protected]>
653dac0
to
ff034bc
Compare
@grobinson-grafana Could you take another look? Or should we involve the authors of the original code? Alternatively I could make a stripped down version of the PR but that would make this field behave differently from receivers |
Add visible_to with the same support for templating and fields as the
existing responders field.
Opsgenie documentation:
The functionality has been tested with unit tests, and with the following
configuration:
Fixes #3953