mirror of
https://github.com/prymitive/karma
synced 2026-08-19 11:36:24 +00:00
feat(api): intern string to save memory
This commit is contained in:
committed by
Łukasz Mierzwa
parent
f6391e9b40
commit
cbeda8daaf
@@ -0,0 +1,16 @@
|
||||
package intern
|
||||
|
||||
type StringInterner map[string]string
|
||||
|
||||
func New() StringInterner {
|
||||
return StringInterner{}
|
||||
}
|
||||
|
||||
func (si StringInterner) Intern(s string) string {
|
||||
interned, ok := si[s]
|
||||
if ok {
|
||||
return interned
|
||||
}
|
||||
si[s] = s
|
||||
return s
|
||||
}
|
||||
+15
-10
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
httptransport "github.com/go-openapi/runtime/client"
|
||||
|
||||
"github.com/prymitive/karma/internal/intern"
|
||||
"github.com/prymitive/karma/internal/mapper"
|
||||
"github.com/prymitive/karma/internal/mapper/v017/client"
|
||||
"github.com/prymitive/karma/internal/mapper/v017/client/alertgroup"
|
||||
@@ -41,6 +42,8 @@ func newClient(uri string, headers map[string]string, httpTransport http.RoundTr
|
||||
|
||||
// Alerts will fetch all alert groups from the API
|
||||
func groups(c *client.AlertmanagerAPI, timeout time.Duration) ([]models.AlertGroup, error) {
|
||||
interner := intern.New()
|
||||
|
||||
groups, err := c.Alertgroup.GetAlertGroups(alertgroup.NewGetAlertGroupsParamsWithTimeout(timeout))
|
||||
if err != nil {
|
||||
return []models.AlertGroup{}, err
|
||||
@@ -51,28 +54,28 @@ func groups(c *client.AlertmanagerAPI, timeout time.Duration) ([]models.AlertGro
|
||||
for _, group := range groups.Payload {
|
||||
ls := models.Labels{}
|
||||
for k, v := range group.Labels {
|
||||
ls = ls.Set(k, v)
|
||||
ls = ls.Set(interner.Intern(k), interner.Intern(v))
|
||||
}
|
||||
sort.Sort(ls)
|
||||
g := models.AlertGroup{
|
||||
Receiver: *group.Receiver.Name,
|
||||
Receiver: interner.Intern(*group.Receiver.Name),
|
||||
Labels: ls,
|
||||
Alerts: make(models.AlertList, 0, len(group.Alerts)),
|
||||
}
|
||||
for _, alert := range group.Alerts {
|
||||
ls := models.Labels{}
|
||||
for k, v := range alert.Labels {
|
||||
ls = ls.Set(k, v)
|
||||
ls = ls.Set(interner.Intern(k), interner.Intern(v))
|
||||
}
|
||||
sort.Sort(ls)
|
||||
a := models.Alert{
|
||||
Fingerprint: *alert.Fingerprint,
|
||||
Receiver: *group.Receiver.Name,
|
||||
Receiver: interner.Intern(*group.Receiver.Name),
|
||||
Annotations: models.AnnotationsFromMap(alert.Annotations),
|
||||
Labels: ls,
|
||||
StartsAt: time.Time(*alert.StartsAt),
|
||||
GeneratorURL: alert.GeneratorURL.String(),
|
||||
State: *alert.Status.State,
|
||||
GeneratorURL: interner.Intern(alert.GeneratorURL.String()),
|
||||
State: interner.Intern(*alert.Status.State),
|
||||
InhibitedBy: alert.Status.InhibitedBy,
|
||||
SilencedBy: alert.Status.SilencedBy,
|
||||
}
|
||||
@@ -88,6 +91,8 @@ func groups(c *client.AlertmanagerAPI, timeout time.Duration) ([]models.AlertGro
|
||||
}
|
||||
|
||||
func silences(c *client.AlertmanagerAPI, timeout time.Duration) ([]models.Silence, error) {
|
||||
interner := intern.New()
|
||||
|
||||
silences, err := c.Silence.GetSilences(silence.NewGetSilencesParamsWithTimeout(timeout))
|
||||
if err != nil {
|
||||
return []models.Silence{}, err
|
||||
@@ -100,13 +105,13 @@ func silences(c *client.AlertmanagerAPI, timeout time.Duration) ([]models.Silenc
|
||||
ID: *s.ID,
|
||||
StartsAt: time.Time(*s.StartsAt),
|
||||
EndsAt: time.Time(*s.EndsAt),
|
||||
CreatedBy: *s.CreatedBy,
|
||||
Comment: *s.Comment,
|
||||
CreatedBy: interner.Intern(*s.CreatedBy),
|
||||
Comment: interner.Intern(*s.Comment),
|
||||
}
|
||||
for _, m := range s.Matchers {
|
||||
sm := models.SilenceMatcher{
|
||||
Name: *m.Name,
|
||||
Value: *m.Value,
|
||||
Name: interner.Intern(*m.Name),
|
||||
Value: interner.Intern(*m.Value),
|
||||
IsRegex: *m.IsRegex,
|
||||
IsEqual: *m.IsEqual,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user