|
| 1 | +// Copyright 2024 Prometheus Team |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package notify |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "sync" |
| 19 | + |
| 20 | + "github.com/prometheus/alertmanager/nflog/nflogpb" |
| 21 | +) |
| 22 | + |
| 23 | +// MetadataStore is a temporary in-memory store for notification metadata |
| 24 | +// (e.g., Slack message_ts, Jira issue keys) until protobuf Entry supports metadata field. |
| 25 | +// This allows integrations to update existing notifications instead of creating new ones. |
| 26 | +type MetadataStore struct { |
| 27 | + mtx sync.RWMutex |
| 28 | + data map[string]map[string]string // key: stateKey(groupKey, receiver) -> metadata map |
| 29 | +} |
| 30 | + |
| 31 | +// NewMetadataStore creates a new MetadataStore. |
| 32 | +func NewMetadataStore() *MetadataStore { |
| 33 | + return &MetadataStore{ |
| 34 | + data: make(map[string]map[string]string), |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// Set stores metadata for a given receiver and group key. |
| 39 | +func (s *MetadataStore) Set(receiver *nflogpb.Receiver, groupKey string, metadata map[string]string) { |
| 40 | + s.mtx.Lock() |
| 41 | + defer s.mtx.Unlock() |
| 42 | + |
| 43 | + key := stateKey(groupKey, receiver) |
| 44 | + s.data[key] = metadata |
| 45 | +} |
| 46 | + |
| 47 | +// Get retrieves metadata for a given receiver and group key. |
| 48 | +func (s *MetadataStore) Get(receiver *nflogpb.Receiver, groupKey string) (map[string]string, bool) { |
| 49 | + s.mtx.RLock() |
| 50 | + defer s.mtx.RUnlock() |
| 51 | + |
| 52 | + key := stateKey(groupKey, receiver) |
| 53 | + metadata, ok := s.data[key] |
| 54 | + return metadata, ok |
| 55 | +} |
| 56 | + |
| 57 | +// Delete removes metadata for a given receiver and group key. |
| 58 | +func (s *MetadataStore) Delete(receiver *nflogpb.Receiver, groupKey string) { |
| 59 | + s.mtx.Lock() |
| 60 | + defer s.mtx.Unlock() |
| 61 | + |
| 62 | + key := stateKey(groupKey, receiver) |
| 63 | + delete(s.data, key) |
| 64 | +} |
| 65 | + |
| 66 | +// stateKey returns a string key for a log entry consisting of the group key and receiver. |
| 67 | +// This matches the key generation in nflog. |
| 68 | +func stateKey(gkey string, r *nflogpb.Receiver) string { |
| 69 | + return receiverKey(gkey, r) |
| 70 | +} |
| 71 | + |
| 72 | +// receiverKey creates a unique key from group key and receiver. |
| 73 | +// Format matches nflog's internal stateKey format: "groupKey:groupName/integration/idx" |
| 74 | +func receiverKey(groupKey string, r *nflogpb.Receiver) string { |
| 75 | + return groupKey + ":" + receiverString(r) |
| 76 | +} |
| 77 | + |
| 78 | +// receiverString returns a string representation of the receiver. |
| 79 | +func receiverString(r *nflogpb.Receiver) string { |
| 80 | + return fmt.Sprintf("%s/%s/%d", r.GroupName, r.Integration, r.Idx) |
| 81 | +} |
| 82 | + |
0 commit comments