Skip to content

Commit

Permalink
Add load balancer status codes to dash
Browse files Browse the repository at this point in the history
  • Loading branch information
ipmb committed Apr 26, 2022
1 parent c21fe80 commit 1bc0774
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
13 changes: 9 additions & 4 deletions cmd/dash.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,22 @@ func populateLineChart(appMetrics metrics.AppMetrics, lc *linechart.LineChart) (
}
var legend []*LegendItem
for _, metric := range metrics.MetricDataResults {
name := metric.Id
color := appMetrics.MetricColor(name)
// "mm" is a special prefix for metrics that start with numbers to make them valid for Cloudwatch (e.g. "mm2xx")
name := strings.TrimPrefix(*metric.Id, "mm")
color := appMetrics.MetricColor(&name)
var values []float64
for _, v := range metric.Values {
values = append(values, *v)
}
labels := labelsFromTimestamps(metric.Timestamps, appMetrics.GetOptions().UTC)
err = lc.Series(*name, values,
err = lc.Series(name, values,
linechart.SeriesCellOpts(cell.FgColor(color)),
linechart.SeriesXLabels(labels),
)
if err != nil {
return nil, err
}
legend = append(legend, &LegendItem{Name: *metric.Id, Color: color})
legend = append(legend, &LegendItem{Name: name, Color: color})
}
return legend, nil
}
Expand Down Expand Up @@ -283,6 +284,10 @@ var dashCmd = &cobra.Command{
appMetrics = append(appMetrics, &metrics.ServiceUtilizationMetrics{App: a, Options: &options, Service: svc})
}
appMetrics = append(appMetrics,
&metrics.StatusCodeMetrics{App: a, Options: &options, Code: "2xx"},
&metrics.StatusCodeMetrics{App: a, Options: &options, Code: "3xx"},
&metrics.StatusCodeMetrics{App: a, Options: &options, Code: "4xx"},
&metrics.StatusCodeMetrics{App: a, Options: &options, Code: "5xx"},
&metrics.ResponseTimeMetrics{App: a, Options: &options, Stat: "Average"},
&metrics.ResponseTimeMetrics{App: a, Options: &options, Stat: "p99"},
)
Expand Down
63 changes: 63 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,69 @@ func (s *ResponseTimeMetrics) MetricDataQueries() []*cloudwatch.MetricDataQuery
}
}

// status codes
type StatusCodeMetrics struct {
App *app.App
Options *MetricOptions
Code string
}

func (m *StatusCodeMetrics) GetApp() *app.App { return m.App }

func (m *StatusCodeMetrics) GetOptions() *MetricOptions { return m.Options }

func (m *StatusCodeMetrics) GetService() string { return "web" }

func (m *StatusCodeMetrics) Title() string { return fmt.Sprintf("%s responses (count)", m.Code) }

func (m *StatusCodeMetrics) ShortName() string { return fmt.Sprintf("%s responses", m.Code) }

func (m *StatusCodeMetrics) MetricColor(name *string) cell.Color {
switch *name {
case "2xx":
return cell.ColorGreen
case "3xx":
return cell.ColorBlue
case "4xx":
return cell.ColorYellow
case "5xx":
return cell.ColorRed
default:
return cell.ColorGray
}
}

func (s *StatusCodeMetrics) LineChartOptions() []linechart.Option {
return []linechart.Option{}
}

func (s *StatusCodeMetrics) MetricDataQueries() []*cloudwatch.MetricDataQuery {
metricDataQueries := []*cloudwatch.MetricDataQuery{
{
Id: aws.String(fmt.Sprintf("mm%s", s.Code)),
MetricStat: &cloudwatch.MetricStat{
Metric: &cloudwatch.Metric{
Namespace: aws.String("AWS/ApplicationELB"),
MetricName: aws.String(fmt.Sprintf("HTTPCode_Target_%s_Count", strings.ToUpper(s.Code))),
Dimensions: []*cloudwatch.Dimension{
{
Name: aws.String("TargetGroup"),
Value: aws.String(s.App.Settings.TargetGroup.Suffix),
},
{
Name: aws.String("LoadBalancer"),
Value: aws.String(s.App.Settings.LoadBalancer.Suffix),
},
},
},
Period: aws.Int64(s.Options.Timeframe.Period()),
Stat: aws.String("Sum"),
},
},
}
return metricDataQueries
}

func FetchMetrics(metrics AppMetrics) (*cloudwatch.GetMetricDataOutput, error) {
app := metrics.GetApp()
options := metrics.GetOptions()
Expand Down

0 comments on commit 1bc0774

Please sign in to comment.