Skip to content

Commit

Permalink
Add pushgateway support
Browse files Browse the repository at this point in the history
  • Loading branch information
mcorbin committed Jul 6, 2024
1 parent ef689b0 commit e86acb7
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 4 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: publish docker image
on:
push:
tags:
- "*"

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
# Allow goreleaser to access older tag information.
fetch-depth: 0
- uses: actions/setup-go@v4
with:
go-version-file: 'go.mod'
cache: true
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
with:
args: release --rm-dist
env:
# GitHub sets the GITHUB_TOKEN secret automatically.
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion cmd/healthcheck_dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func createDNSHealthcheckCmd() *cobra.Command {

createDNSHealthcheck.PersistentFlags().StringVar(&description, "description", "", "healthcheck description")

createDNSHealthcheck.PersistentFlags().StringSliceVar(&labels, "labels", []string{}, "healthchecks labels (example: foo=bar)")
createDNSHealthcheck.PersistentFlags().StringSliceVar(&labels, "labels", []string{}, "healthcheck labels (example: foo=bar)")

createDNSHealthcheck.PersistentFlags().StringVar(&interval, "interval", "60s", "healthcheck interval (examples: 30s, 3m)")

Expand Down
175 changes: 175 additions & 0 deletions cmd/pushgateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package cmd

import (
"context"
"encoding/json"
"errors"
"fmt"
"os"

goclient "github.com/appclacks/go-client"
"github.com/cheynewallace/tabby"
"github.com/spf13/cobra"
)

func createPushgatewayMetricCmd() *cobra.Command {
var name string
var description string
var labels []string
var ttl string
var metricType string
var value float32

var createPushgatewayMetric = &cobra.Command{
Use: "create",
Short: "Create (or update) a new metric in the push gateway. ",
Run: func(cmd *cobra.Command, args []string) {
client := buildClient()
labelsMap, err := toMap(labels)
exitIfError(err)

payload := goclient.CreateOrUpdatePushgatewayMetricInput{
Name: name,
Description: description,
Labels: labelsMap,
TTL: ttl,
Type: metricType,
Value: value,
}
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
result, err := client.CreateOrUpdatePushgatewayMetric(ctx, payload)
exitIfError(err)
if outputFormat == "json" {
json, err := json.Marshal(result)
exitIfError(err)
fmt.Println(string(json))
os.Exit(0)
}
t := tabby.New()
t.AddHeader("Messages")
for _, message := range result.Messages {
t.AddLine(message)
}
t.Print()
os.Exit(0)
},
}
createPushgatewayMetric.PersistentFlags().StringVar(&name, "name", "", "metric name")
err := createPushgatewayMetric.MarkPersistentFlagRequired("name")
exitIfError(err)

createPushgatewayMetric.PersistentFlags().StringVar(&description, "description", "", "metric description")

createPushgatewayMetric.PersistentFlags().StringSliceVar(&labels, "labels", []string{}, "metric labels (example: foo=bar)")

createPushgatewayMetric.PersistentFlags().StringVar(&ttl, "ttl", "", "metric timeout")

createPushgatewayMetric.PersistentFlags().StringVar(&metricType, "type", "", "metric type")

createPushgatewayMetric.PersistentFlags().Float32Var(&value, "value", 0, "metric value")
err = createPushgatewayMetric.MarkPersistentFlagRequired("value")
exitIfError(err)

return createPushgatewayMetric
}

func listPushgatewayMetricsCmd() *cobra.Command {
var listPushgatewayMetrics = &cobra.Command{
Use: "list",
Short: "List push gateway metrics",
Run: func(cmd *cobra.Command, args []string) {
client := buildClient()
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
result, err := client.ListPushgatewayMetrics(ctx)
exitIfError(err)
if outputFormat == "json" {
json, err := json.Marshal(result)
exitIfError(err)
fmt.Println(string(json))
os.Exit(0)
}
t := tabby.New()
t.AddHeader("ID", "Name", "Description", "Type", "Labels", "TTL", "Created At", "Expires At", "Value")
for _, metric := range result.Result {
jsonLabels, err := json.Marshal(metric.Labels)
exitIfError(err)
t.AddLine(metric.ID, metric.Name, metric.Description, metric.Type, string(jsonLabels), metric.TTL, metric.CreatedAt, metric.ExpiresAt, metric.Value)
}
t.Print()
os.Exit(0)
},
}
return listPushgatewayMetrics
}

func deletePushgatewayMetricCmd() *cobra.Command {
var metricID string
var metricName string
var deleteMetric = &cobra.Command{
Use: "delete",
Short: "Delete a push gateway metric by name or by ID",
Run: func(cmd *cobra.Command, args []string) {
identifier := metricID
if identifier == "" {
identifier = metricName
}
if identifier == "" {
exitIfError(errors.New("you should pass the metric name or ID (using --name or --id) you want to delete"))
}
input := goclient.DeletePushgatewayMetricInput{
Identifier: identifier,
}
client := buildClient()
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
result, err := client.DeletePushgatewayMetric(ctx, input)
exitIfError(err)
if outputFormat == "json" {
json, err := json.Marshal(result)
exitIfError(err)
fmt.Println(string(json))
os.Exit(0)
}
t := tabby.New()
t.AddHeader("Messages")
for _, message := range result.Messages {
t.AddLine(message)
}
t.Print()
os.Exit(0)
},
}
deleteMetric.PersistentFlags().StringVar(&metricID, "id", "", "Metric ID")
deleteMetric.PersistentFlags().StringVar(&metricName, "name", "", "Metric Name")
return deleteMetric
}

func deleteAllPushgatewayMetricsCmd() *cobra.Command {
var deleteMetric = &cobra.Command{
Use: "delete-all",
Short: "Delete all push gateway metrics",
Run: func(cmd *cobra.Command, args []string) {
client := buildClient()
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
result, err := client.DeleteAllPushgatewayMetrics(ctx)
exitIfError(err)
if outputFormat == "json" {
json, err := json.Marshal(result)
exitIfError(err)
fmt.Println(string(json))
os.Exit(0)
}
t := tabby.New()
t.AddHeader("Messages")
for _, message := range result.Messages {
t.AddLine(message)
}
t.Print()
os.Exit(0)
},
}
return deleteMetric
}
10 changes: 10 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,23 @@ func Execute() error {
command.AddCommand(createCommandHealthcheckCmd())
command.AddCommand(updateCommandHealthcheckCmd())

var pushgateway = &cobra.Command{
Use: "pushgateway",
Short: "Manage pushgateway metrics",
}
pushgateway.AddCommand(createPushgatewayMetricCmd())
pushgateway.AddCommand(listPushgatewayMetricsCmd())
pushgateway.AddCommand(deletePushgatewayMetricCmd())
pushgateway.AddCommand(deleteAllPushgatewayMetricsCmd())

healthcheck.AddCommand(dns)
healthcheck.AddCommand(command)
healthcheck.AddCommand(tls)
healthcheck.AddCommand(tcp)
healthcheck.AddCommand(http)

rootCmd.AddCommand(healthcheck)
rootCmd.AddCommand(pushgateway)

return rootCmd.Execute()
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/appclacks/cli
go 1.22.0

require (
github.com/appclacks/go-client v0.0.0-20240602163908-0de41c983ab7
github.com/appclacks/go-client v0.0.0-20240704120510-358af014f040
github.com/cheynewallace/tabby v1.1.1
github.com/spf13/cobra v1.8.0
)
Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ github.com/appclacks/go-client v0.0.0-20240602132011-291c0d8ca225 h1:Bi2DlXudM58
github.com/appclacks/go-client v0.0.0-20240602132011-291c0d8ca225/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU=
github.com/appclacks/go-client v0.0.0-20240602163908-0de41c983ab7 h1:kTNVmMjdtr/X0ljUCyDCXUzDYnP3JL5fdDMwBSUveOg=
github.com/appclacks/go-client v0.0.0-20240602163908-0de41c983ab7/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU=
github.com/appclacks/go-client v0.0.0-20240618204225-51a556fa72ba h1://S8sLRrM/2eJLfYXcFOpsP7eDGwKDrHj0rqlXj6uSo=
github.com/appclacks/go-client v0.0.0-20240618204225-51a556fa72ba/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU=
github.com/appclacks/go-client v0.0.0-20240703201525-421246966bba h1:alp3NoMSmJ9bAw6Ubo02F3z4aBVM5HIkXplKOuaj4mc=
github.com/appclacks/go-client v0.0.0-20240703201525-421246966bba/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU=
github.com/appclacks/go-client v0.0.0-20240703203820-f7d948812083 h1:ESPN+6uiockjKr2XnTRQBkleMQsT5PH9S/CjVUIUdqg=
github.com/appclacks/go-client v0.0.0-20240703203820-f7d948812083/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU=
github.com/appclacks/go-client v0.0.0-20240703204325-c8a3257d9e08 h1:vjf4pQGIx8BCj1+wEsNBR04lyVzje74O7ozJQn6JO3w=
github.com/appclacks/go-client v0.0.0-20240703204325-c8a3257d9e08/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU=
github.com/appclacks/go-client v0.0.0-20240703205617-8b0425724375 h1:eVJ+l4qRByZ2L9O4xl2HC009T7TMw2z5EPnf+ArUH3A=
github.com/appclacks/go-client v0.0.0-20240703205617-8b0425724375/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU=
github.com/appclacks/go-client v0.0.0-20240704120510-358af014f040 h1:1Y8zxzWnIJJTN35I1m19Pf3Li7eRLQHAoAjjAta5ck4=
github.com/appclacks/go-client v0.0.0-20240704120510-358af014f040/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU=
github.com/cheynewallace/tabby v1.1.1 h1:JvUR8waht4Y0S3JF17G6Vhyt+FRhnqVCkk8l4YrOU54=
github.com/cheynewallace/tabby v1.1.1/go.mod h1:Pba/6cUL8uYqvOc9RkyvFbHGrQ9wShyrn6/S/1OYVys=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions vendor/github.com/appclacks/go-client/pushgateway.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# github.com/appclacks/go-client v0.0.0-20240602163908-0de41c983ab7
# github.com/appclacks/go-client v0.0.0-20240704120510-358af014f040
## explicit; go 1.22.0
github.com/appclacks/go-client
# github.com/cheynewallace/tabby v1.1.1
Expand Down

0 comments on commit e86acb7

Please sign in to comment.