Commit 5f117ec1 authored by Sabnock01's avatar Sabnock01

generalize metrics defs

parent 0d643f0c
......@@ -8,6 +8,7 @@ import (
"github.com/ethereum-optimism/optimism/op-batcher/batcher"
"github.com/ethereum-optimism/optimism/op-batcher/flags"
"github.com/ethereum-optimism/optimism/op-batcher/metrics"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum-optimism/optimism/op-service/metrics/doc"
"github.com/ethereum/go-ethereum/log"
......@@ -32,7 +33,7 @@ func main() {
app.Commands = []*cli.Command{
{
Name: "doc",
Subcommands: doc.Subcommands,
Subcommands: doc.NewSubcommands(metrics.NewMetrics("default")),
},
}
......
package doc
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/ethereum-optimism/optimism/op-node/metrics"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli/v2"
)
var Subcommands = cli.Commands{
{
Name: "metrics",
Usage: "Dumps a list of supported metrics to stdout",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "format",
Value: "markdown",
Usage: "Output format (json|markdown)",
},
},
Action: func(ctx *cli.Context) error {
m := metrics.NewMetrics("default")
supportedMetrics := m.Document()
format := ctx.String("format")
if format != "markdown" && format != "json" {
return fmt.Errorf("invalid format: %s", format)
}
if format == "json" {
enc := json.NewEncoder(os.Stdout)
return enc.Encode(supportedMetrics)
}
table := tablewriter.NewWriter(os.Stdout)
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.SetAutoWrapText(false)
table.SetHeader([]string{"Metric", "Description", "Labels", "Type"})
var data [][]string
for _, metric := range supportedMetrics {
labels := strings.Join(metric.Labels, ",")
data = append(data, []string{metric.Name, metric.Help, labels, metric.Type})
}
table.AppendBulk(data)
table.Render()
return nil
},
},
}
......@@ -7,7 +7,7 @@ import (
"strconv"
"github.com/ethereum-optimism/optimism/op-node/chaincfg"
"github.com/ethereum-optimism/optimism/op-node/cmd/doc"
"github.com/ethereum-optimism/optimism/op-service/metrics/doc"
"github.com/urfave/cli/v2"
......@@ -70,7 +70,7 @@ func main() {
},
{
Name: "doc",
Subcommands: doc.Subcommands,
Subcommands: doc.NewSubcommands(metrics.NewMetrics("default")),
},
}
......
......@@ -7,6 +7,7 @@ import (
"github.com/urfave/cli/v2"
"github.com/ethereum-optimism/optimism/op-proposer/flags"
"github.com/ethereum-optimism/optimism/op-proposer/metrics"
"github.com/ethereum-optimism/optimism/op-proposer/proposer"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum-optimism/optimism/op-service/metrics/doc"
......@@ -32,7 +33,7 @@ func main() {
app.Commands = []*cli.Command{
{
Name: "doc",
Subcommands: doc.Subcommands,
Subcommands: doc.NewSubcommands(metrics.NewMetrics("default")),
},
}
......
......@@ -6,12 +6,18 @@ import (
"os"
"strings"
"github.com/ethereum-optimism/optimism/op-node/metrics"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli/v2"
"github.com/ethereum-optimism/optimism/op-service/metrics"
)
var Subcommands = cli.Commands{
type Metrics interface {
Document() []metrics.DocumentedMetric
}
func NewSubcommands(m Metrics) cli.Commands {
return cli.Commands{
{
Name: "metrics",
Usage: "Dumps a list of supported metrics to stdout",
......@@ -23,7 +29,6 @@ var Subcommands = cli.Commands{
},
},
Action: func(ctx *cli.Context) error {
m := metrics.NewMetrics("default")
supportedMetrics := m.Document()
format := ctx.String("format")
......@@ -51,4 +56,5 @@ var Subcommands = cli.Commands{
return nil
},
},
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment