Skip to content

Commit

Permalink
fix: Remove extra metric & add information to existing metric.
Browse files Browse the repository at this point in the history
  • Loading branch information
gowizzard committed Jan 5, 2025
1 parent bae2dd2 commit f2ad94c
Showing 1 changed file with 49 additions and 59 deletions.
108 changes: 49 additions & 59 deletions internal/handler/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
type Metric struct {
Name string
Labels []Labels
Value interface{}
Value int
}

// Labels is to define the labels' struct. It has the key and value fields.
Expand Down Expand Up @@ -65,75 +65,65 @@ func Metrics(w http.ResponseWriter, _ *http.Request) {
tag = image.RepoTags[0]
}

labels := []Labels{
{
Key: "id",
Value: container.Id,
},
{
Key: "name",
Value: strings.Replace(container.Name, "/", "", 1),
},
{
Key: "image",
Value: tag,
},
{
Key: "status",
Value: container.State.Status,
},
{
Key: "created",
Value: strconv.FormatInt(container.Created.Unix(), 10),
},
{
Key: "started_at",
Value: strconv.FormatInt(container.State.StartedAt.Unix(), 10),
metric := Metric{
Name: "container_restart_count",
Labels: []Labels{
{
Key: "id",
Value: container.Id,
},
{
Key: "name",
Value: strings.Replace(container.Name, "/", "", 1),
},
{
Key: "image",
Value: tag,
},
{
Key: "status",
Value: container.State.Status,
},
{
Key: "health",
Value: container.State.Health.Status,
},
{
Key: "created",
Value: strconv.FormatInt(container.Created.Unix(), 10),
},
{
Key: "started_at",
Value: strconv.FormatInt(container.State.StartedAt.Unix(), 10),
},
},
Value: container.RestartCount,
}

metrics := []Metric{
{
Name: "container_restart_count",
Labels: labels,
Value: container.RestartCount,
},
{
Name: "container_state_health_status",
Labels: labels,
Value: container.State.Health.Status,
},
}

for _, value := range metrics {

var b strings.Builder
var b strings.Builder

b.WriteString(value.Name)
b.WriteString("{")
b.WriteString(metric.Name)
b.WriteString("{")

label := len(value.Labels) - 1
for index, value := range value.Labels {
for index, value := range metric.Labels {

_, err = fmt.Fprintf(&b, `%s="%s"`, value.Key, value.Value)
if err != nil {
write.Logger.Error("Write the container restart count metric.", "err", err)
}

if index < label {
b.WriteString(",")
}
_, err = fmt.Fprintf(&b, `%s="%s"`, value.Key, value.Value)
if err != nil {
write.Logger.Error("Write the container restart count metric.", "err", err)
}

if index < len(metric.Labels)-1 {
b.WriteString(",")
}

b.WriteString("}")
b.WriteString(fmt.Sprintf(" %v\n", value.Value))
}

_, err = fmt.Fprintf(w, b.String())
if err != nil {
write.Logger.Error("Write the container restart count metric.", "err", err)
}
b.WriteString("}")
b.WriteString(fmt.Sprintf(" %v\n", metric.Value))

_, err = fmt.Fprintf(w, b.String())
if err != nil {
write.Logger.Error("Write the container restart count metric.", "err", err)
}

}
Expand Down

0 comments on commit f2ad94c

Please sign in to comment.