Skip to content

Commit

Permalink
Remove objects from cache when processing Icinga 2 delete event
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab authored and julianbrost committed Jun 25, 2024
1 parent f8c3125 commit 683ea69
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/icinga2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"github.com/google/uuid"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/icinga/icinga-notifications/internal/event"
"github.com/icinga/icinga-notifications/internal/object"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"net/http"
"net/url"
"strings"
"time"
)

Expand Down Expand Up @@ -190,6 +192,17 @@ func (client *Client) deleteExtraTagsCacheFor(result *ObjectCreatedDeleted) erro
return fmt.Errorf("cannot delete object extra tags for unknown object_type %q", result.ObjectType)
}

if result.EventType == typeObjectDeleted {
names := strings.Split(result.ObjectName, "!")
tags := map[string]string{"host": names[0]}
if len(names) == 2 {
tags["service"] = names[1]
}

// Delete the object from our global cache to avoid having huge dangling objects that don't exist in Icinga 2.
object.DeleteFromCache(object.ID(client.EventSourceId, tags))
}

// The checkable has just been either deleted or created, so delete all existing extra tags from our cache
// store as well and will be refreshed on the next access when Icinga 2 emits any other event for that object.
client.eventExtraTagsCache.Remove(result.ObjectName)
Expand Down
8 changes: 8 additions & 0 deletions internal/object/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ var (
cacheMu sync.Mutex
)

// DeleteFromCache deletes the Object from the global cache store matching the given ID (if any).
func DeleteFromCache(id types.Binary) {
cacheMu.Lock()
defer cacheMu.Unlock()

delete(cache, id.String())
}

// RestoreMutedObjects restores all muted objects and their extra / ID tags from the database.
// Note, this function only retrieves muted objects without non-recovered incident.
//
Expand Down

0 comments on commit 683ea69

Please sign in to comment.