-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from tukaelu/support-service-metrics
Support service metrics
- Loading branch information
Showing
13 changed files
with
494 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package subcommand | ||
|
||
import ( | ||
"github.com/mackerelio/mackerel-client-go" | ||
"github.com/urfave/cli/v2" | ||
|
||
"github.com/tukaelu/sabadashi/internal/converter" | ||
"github.com/tukaelu/sabadashi/internal/validator" | ||
) | ||
|
||
func NewServiceSubCommand() *cli.Command { | ||
return &cli.Command{ | ||
Name: "service", | ||
Usage: "Retrieves service metrics", | ||
Action: func(ctx *cli.Context) error { | ||
|
||
from := converter.ToStartDayOfUnixtime(ctx.String("from")) | ||
to := converter.ToEndDayOfUnixtime(ctx.String("to")) | ||
|
||
if err := validator.ValidateTheDateBeforeAndAfter(from, to); err != nil { | ||
return err | ||
} | ||
if err := validator.ValidateMetricRetantionPeriod(from, to); err != nil { | ||
return err | ||
} | ||
|
||
cmd := &serviceCommand{ | ||
baseCommand: baseCommand{ | ||
client: mackerel.NewClient(ctx.String("apikey")), | ||
from: from, | ||
to: to, | ||
granularity: ctx.String("granularity"), | ||
withFriendly: ctx.Bool("with-friendly-date-format"), | ||
rawFrom: ctx.String("from"), | ||
rawTo: ctx.String("to"), | ||
}, | ||
name: ctx.String("name"), | ||
withExternal: ctx.Bool("with-external-monitors"), | ||
} | ||
|
||
return doService(ctx, cmd) | ||
}, | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "name", | ||
Aliases: []string{"n"}, | ||
Usage: "Name of the service from which to retrieve metric", | ||
Required: true, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "from", | ||
Usage: "Specify the date to start retrieving metrics in YYYYMMDD format. (e.g. 20230101)", | ||
Required: true, | ||
Action: func(ctx *cli.Context, s string) error { | ||
return validator.ValidateDateFormat(s) | ||
}, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "to", | ||
Usage: "Specify the date to end retrieving metrics in YYYYMMDD format. (e.g. 20231231)", | ||
Required: true, | ||
Action: func(ctx *cli.Context, s string) error { | ||
return validator.ValidateDateFormat(s) | ||
}, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "granularity", | ||
Aliases: []string{"g"}, | ||
Usage: "Specify the granularity of metric data. Choose from 1m, 5m, 10m, 1h, 2h, 4h or 1d.", | ||
Value: "1m", | ||
DefaultText: "1m", | ||
Action: func(ctx *cli.Context, s string) error { | ||
return validator.ValidateGranularity(s) | ||
}, | ||
}, | ||
&cli.BoolFlag{ | ||
Name: "with-friendly-date-format", | ||
Aliases: []string{"f"}, | ||
Usage: "If this flag is enabled, an additional column with a friendly date format is output at the beginning of the CSV line.", | ||
}, | ||
&cli.BoolFlag{ | ||
Name: "with-external-monitors", | ||
Aliases: []string{"e"}, | ||
Usage: "If this flag is enabled, it also includes the metric measured in the external monitoring.", | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package subcommand | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"sync" | ||
"time" | ||
|
||
"github.com/mackerelio/mackerel-client-go" | ||
"github.com/schollz/progressbar/v3" | ||
"github.com/urfave/cli/v2" | ||
|
||
"github.com/tukaelu/sabadashi/internal/converter" | ||
"github.com/tukaelu/sabadashi/internal/definition" | ||
"github.com/tukaelu/sabadashi/internal/exporter" | ||
"github.com/tukaelu/sabadashi/internal/fileutil" | ||
"github.com/tukaelu/sabadashi/internal/retriever" | ||
) | ||
|
||
type hostCommand struct { | ||
baseCommand | ||
host string | ||
} | ||
|
||
func doHost(ctx *cli.Context, h *hostCommand) error { | ||
|
||
exportDir, err := fileutil.CreateExportDir(h.host, h.rawFrom, h.rawTo) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
interval := converter.GranularityToInterval(h.granularity) | ||
attempts := (h.to-h.from)/interval + 1 | ||
|
||
metricNames, err := h.client.ListHostMetricNames(h.host) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
progress := progressbar.Default(attempts * int64(len(metricNames))) | ||
|
||
ch := make(chan exporter.Result, definition.CONCURRENT_FILE_OPERATION) | ||
defer close(ch) | ||
|
||
go func(ch chan exporter.Result) { | ||
for { | ||
select { | ||
case res := <-ch: | ||
if err := fileutil.WriteFile(exportDir, res.MetricName, "csv", res.Records, h.withFriendly); err != nil { | ||
fmt.Printf("Failed to write CSV file. (reason: %s)\n", err) | ||
return | ||
} | ||
case <-ctx.Done(): | ||
fmt.Println("Operation canceled.") | ||
os.Exit(125) | ||
} | ||
} | ||
}(ch) | ||
|
||
from := h.from | ||
to := int64(0) | ||
wg := &sync.WaitGroup{} | ||
for i := int64(0); i < attempts; i++ { | ||
to = from + interval | ||
if to > h.to { | ||
to = h.to | ||
} | ||
|
||
for _, metricName := range metricNames { | ||
wg.Add(1) | ||
go func(ctx *context.Context, metricName string) { | ||
defer wg.Done() | ||
|
||
metrics, err := retriever.Retrieve(ctx, metricName, func() ([]mackerel.MetricValue, error) { | ||
metricValues, err := h.client.FetchHostMetricValues(h.host, metricName, from, to) | ||
if err != nil { | ||
return nil, fmt.Errorf("[ERROR] failed to retrieve metrics (metric: %s, from: %d, to: %d) (reason: %s)", metricName, from, to, err) | ||
} | ||
return metricValues, nil | ||
}) | ||
|
||
if err != nil { | ||
// skip retrieve metrics... | ||
fmt.Sprintln(err) | ||
} | ||
|
||
ch <- exporter.Result{ | ||
MetricName: metricName, | ||
Records: metrics, | ||
} | ||
}(&ctx.Context, metricName) | ||
|
||
time.Sleep(350 * time.Millisecond) | ||
|
||
_ = progress.Add(1) | ||
} | ||
wg.Wait() | ||
|
||
from += interval | ||
_ = progress.Add(1) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.