Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Commit

Permalink
feat: Allow labels and deployment types as placeholders for SLIs (#265)
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Kreuzberger <[email protected]>
  • Loading branch information
christian-kreuzberger-dtx authored Feb 10, 2022
1 parent 94e57f3 commit ac86eed
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
2 changes: 2 additions & 0 deletions eventhandling/configureEvent.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ func (eh ConfigureMonitoringEventHandler) createPrometheusAlertsIfSLOsAndRemedia
Service: eventData.Service,
Stage: stage.Name,
},
"primary", // only create alerts for primary deployments
nil,
nil,
)

Expand Down
2 changes: 2 additions & 0 deletions eventhandling/getSliEvent.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ func retrieveMetrics(event cloudevents.Event, eventData *keptnv2.GetSLITriggered
prometheusHandler := utils.NewPrometheusHandler(
prometheusAPIURL,
&eventData.EventData,
eventData.Deployment, // "canary", "primary" or "" (or "direct" or "user_managed")
eventData.Labels,
eventData.GetSLI.CustomFilters,
)

Expand Down
47 changes: 28 additions & 19 deletions utils/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@ type prometheusResponse struct {

// Handler interacts with a prometheus API endpoint
type Handler struct {
ApiURL string
Username string
Password string
Project string
Stage string
Service string
HTTPClient *http.Client
CustomFilters []*keptnv2.SLIFilter
CustomQueries map[string]string
ApiURL string
Username string
Password string
Project string
Stage string
Service string
DeploymentType string
Labels map[string]string
HTTPClient *http.Client
CustomFilters []*keptnv2.SLIFilter
CustomQueries map[string]string
}

const alertManagerYml = `global:
Expand Down Expand Up @@ -162,14 +164,16 @@ func (p *PrometheusHelper) UpdateAMConfigMap(name string, filename string, names
}

// NewPrometheusHandler returns a new prometheus handler that interacts with the Prometheus REST API
func NewPrometheusHandler(apiURL string, eventData *keptnv2.EventData, customFilters []*keptnv2.SLIFilter) *Handler {
func NewPrometheusHandler(apiURL string, eventData *keptnv2.EventData, deploymentType string, labels map[string]string, customFilters []*keptnv2.SLIFilter) *Handler {
ph := &Handler{
ApiURL: apiURL,
Project: eventData.Project,
Stage: eventData.Stage,
Service: eventData.Service,
HTTPClient: &http.Client{},
CustomFilters: customFilters,
ApiURL: apiURL,
Project: eventData.Project,
Stage: eventData.Stage,
Service: eventData.Service,
DeploymentType: deploymentType,
Labels: labels,
HTTPClient: &http.Client{},
CustomFilters: customFilters,
}

return ph
Expand Down Expand Up @@ -265,9 +269,14 @@ func (ph *Handler) replaceQueryParameters(query string, start time.Time, end tim
query = strings.Replace(query, "$PROJECT", ph.Project, -1)
query = strings.Replace(query, "$STAGE", ph.Stage, -1)
query = strings.Replace(query, "$SERVICE", ph.Service, -1)
query = strings.Replace(query, "$project", ph.Project, -1)
query = strings.Replace(query, "$stage", ph.Stage, -1)
query = strings.Replace(query, "$service", ph.Service, -1)
query = strings.Replace(query, "$DEPLOYMENT", ph.DeploymentType, -1)

// replace labels
for key, value := range ph.Labels {
query = strings.Replace(query, "$LABEL."+key, value, -1)
}

// replace duration
durationString := strconv.FormatInt(getDurationInSeconds(start, end), 10) + "s"

query = strings.Replace(query, "$DURATION_SECONDS", durationString, -1)
Expand Down

0 comments on commit ac86eed

Please sign in to comment.