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

feat(observability-lib): builder to create independently grafana resources #915

Merged
merged 4 commits into from
Nov 5, 2024

Conversation

Atrax1
Copy link
Collaborator

@Atrax1 Atrax1 commented Nov 4, 2024

Builder refactor to allow creating contact points and notification policies without creating a dashboard

Refresh(options.Refresh).
Time(options.TimeFrom, options.TimeTo).
Timezone(options.TimeZone),
if options.Name != "" {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Only creating a dashboard builder if we provide a name

Comment on lines +61 to +75
builder := grafana.NewBuilder(&grafana.BuilderOptions{})
builder.AddContactPoint(grafana.NewContactPoint(&grafana.ContactPointOptions{
Name: "slack",
Type: "slack",
}))

o, err := builder.Build()
if err != nil {
t.Errorf("Error during build: %v", err)
}

require.Empty(t, o.Dashboard)
require.Empty(t, o.Alerts)
require.NotEmpty(t, o.ContactPoints)
require.Empty(t, o.NotificationPolicies)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We can now use the library to create a single contact point

Comment on lines +79 to 97
builder := grafana.NewBuilder(&grafana.BuilderOptions{})
builder.AddNotificationPolicy(grafana.NewNotificationPolicy(&grafana.NotificationPolicyOptions{
Receiver: "slack",
GroupBy: []string{"grafana_folder", "alertname"},
ObjectMatchers: []alerting.ObjectMatcher{
{"team", "=", "chainlink"},
},
}))

o, err := builder.Build()
if err != nil {
t.Errorf("Error during build: %v", err)
}

require.Empty(t, o.Dashboard)
require.Empty(t, o.Alerts)
require.Empty(t, o.ContactPoints)
require.NotEmpty(t, o.NotificationPolicies)
})
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We can now use the library to create a single notification policy

Comment on lines -20 to 25
type Dashboard struct {
type Observability struct {
Dashboard *dashboard.Dashboard
Alerts []alerting.Rule
ContactPoints []alerting.ContactPoint
NotificationPolicies []alerting.NotificationPolicy
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

renaming as the struct is holding all type of grafana resources not only dashboard anymore

for j := range b {
if reflect.DeepEqual(a[i], b[j]) {
foundMatch = true
break
Copy link
Contributor

Choose a reason for hiding this comment

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

this doesn't look like an exact match - could you leave a comment on the method explaining when a and b should be equal to each other?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Contributor

@patrickhuie19 patrickhuie19 Nov 5, 2024

Choose a reason for hiding this comment

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

yes, the tests are good 👍 - a comment saying that this is an unordered equal should suffice for the reader

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

agree Im adding the comment

if b.dashboardBuilder != nil {
db, errBuildDashboard := b.dashboardBuilder.Build()
if errBuildDashboard != nil {
return nil, errBuildDashboard
Copy link
Contributor

Choose a reason for hiding this comment

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

/nit, but when passing errors up the stack, it can be helpful for debugging to wrap the error with a fmt.Errorf with a description of what the consumer observed with that error, i.e.

return nil, fmt.Errorf("failure in building dashboard: %w, errBuildDashboard")

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

right I'll take care of this in future iterations

// Add common tags to alerts
if b.alertsTags != nil && len(b.alertsTags) > 0 {
tags := maps.Clone(b.alertsTags)
maps.Copy(tags, alert.Labels)
Copy link
Contributor

Choose a reason for hiding this comment

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

what is the difference between the tags and alert.Labels at this point?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Its the same I use tags as a name but grafana calls them Labels I could rename vars

Copy link
Contributor

Choose a reason for hiding this comment

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

non-blocking, but if you could clarify in a future iteration please

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

sure

@Atrax1 Atrax1 merged commit f1c901f into main Nov 5, 2024
11 checks passed
@Atrax1 Atrax1 deleted the observability-lib-builder-refactor branch November 5, 2024 18:30
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