-
Notifications
You must be signed in to change notification settings - Fork 1
/
tenantmapping_command.go
129 lines (108 loc) · 4.87 KB
/
tenantmapping_command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"context"
"fmt"
"time"
"github.com/go-logr/logr"
"github.com/jmoiron/sqlx"
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/urfave/cli/v2"
"github.com/appuio/appuio-cloud-reporting/pkg/db"
"github.com/appuio/appuio-cloud-reporting/pkg/tenantmapping"
)
type tmapCommand struct {
DatabaseURL string
PrometheusURL string
Begin *time.Time
RepeatUntil *time.Time
PromQueryTimeout time.Duration
DryRun bool
AdditionalMetricSelector string
ThanosAllowPartialResponses bool
OrgId string
}
var tenantmappingCommandName = "tenantmapping"
func newTmapCommand() *cli.Command {
command := &tmapCommand{}
return &cli.Command{
Name: tenantmappingCommandName,
Usage: "Update the tenant mapping (source, target) in the database for a given time",
Before: command.before,
Action: command.execute,
Flags: []cli.Flag{
newDbURLFlag(&command.DatabaseURL),
newPromURLFlag(&command.PrometheusURL),
&cli.TimestampFlag{Name: "begin", Usage: fmt.Sprintf("Beginning timestamp of the report period in the form of RFC3339 (%s)", time.RFC3339),
EnvVars: envVars("BEGIN"), Layout: time.RFC3339, Required: true, DefaultText: defaultTestForRequiredFlags},
&cli.TimestampFlag{Name: "repeat-until", Usage: fmt.Sprintf("Repeat running the report until reaching this timestamp (%s)", time.RFC3339),
EnvVars: envVars("REPEAT_UNTIL"), Layout: time.RFC3339, Required: false},
&cli.DurationFlag{Name: "prom-query-timeout", Usage: "Timeout when querying prometheus (example: 1m)",
EnvVars: envVars("PROM_QUERY_TIMEOUT"), Destination: &command.PromQueryTimeout, Required: false},
&cli.BoolFlag{Name: "thanos-allow-partial-responses", Usage: "Allows partial responses from Thanos. Can be helpful when querying a Thanos cluster with lost data.",
EnvVars: envVars("THANOS_ALLOW_PARTIAL_RESPONSES"), Destination: &command.ThanosAllowPartialResponses, Required: false, DefaultText: "false"},
&cli.BoolFlag{Name: "dry-run", Usage: "Does not commit results if set.",
EnvVars: envVars("DRY_RUN"), Destination: &command.DryRun, Required: false, DefaultText: "false"},
&cli.StringFlag{Name: "additional-metric-selector", Usage: "Allows further specifying which metrics to choose. Example: --additional-metric-selector='namespace=\"testing\"'",
EnvVars: envVars("ADDITIONAL_METRIC_SELECTOR"), Destination: &command.AdditionalMetricSelector, Required: false, DefaultText: "false"},
&cli.StringFlag{Name: "org-id", Usage: "Sets the X-Scope-OrgID header to this value on requests to Prometheus", Value: "",
EnvVars: envVars("ORG_ID"), Destination: &command.OrgId, Required: false, DefaultText: "empty"},
},
}
}
func (cmd *tmapCommand) before(context *cli.Context) error {
cmd.Begin = context.Timestamp("begin")
cmd.RepeatUntil = context.Timestamp("repeat-until")
return LogMetadata(context)
}
func (cmd *tmapCommand) execute(cliCtx *cli.Context) error {
ctx := cliCtx.Context
log := AppLogger(ctx).WithName(tenantmappingCommandName)
// We really need to fix the inane dance around the AppLogger which needs custom plumbing and can't be used from packages because of import cycles.
ctx = logr.NewContext(ctx, log)
promClient, err := newPrometheusAPIClient(cmd.PrometheusURL, cmd.ThanosAllowPartialResponses, cmd.OrgId)
if err != nil {
return fmt.Errorf("could not create prometheus client: %w", err)
}
log.V(1).Info("Opening database connection", "url", cmd.DatabaseURL)
rdb, err := db.Openx(cmd.DatabaseURL)
if err != nil {
return fmt.Errorf("could not open database connection: %w", err)
}
defer rdb.Close()
o := make([]tenantmapping.Option, 0)
if cmd.PromQueryTimeout != 0 {
o = append(o, tenantmapping.WithPrometheusQueryTimeout(cmd.PromQueryTimeout))
}
if cmd.AdditionalMetricSelector != "" {
o = append(o, tenantmapping.WithMetricSelector(cmd.AdditionalMetricSelector))
}
if cmd.RepeatUntil == nil {
return runTenantMapping(ctx, rdb, promClient, *cmd.Begin, cmd.DryRun, o...)
}
for currentTime := *cmd.Begin; cmd.RepeatUntil.After(currentTime); currentTime = currentTime.Add(time.Hour) {
if err := runTenantMapping(ctx, rdb, promClient, currentTime, cmd.DryRun, o...); err != nil {
return fmt.Errorf("error running report at %s: %w", currentTime.Format(time.RFC3339), err)
}
}
return nil
}
func runTenantMapping(ctx context.Context, rdb *sqlx.DB, promClient apiv1.API, begin time.Time, dryRun bool, o ...tenantmapping.Option) error {
log := AppLogger(ctx).WithName(tenantmappingCommandName)
log.V(1).Info("Begin transaction")
tx, err := rdb.Beginx()
if err != nil {
return err
}
defer tx.Rollback()
log.Info("Running mapper...")
err = tenantmapping.MapTenantTarget(ctx, tx, promClient, begin, o...)
if err != nil {
return err
}
if dryRun {
log.Info("Dry run, not committing transaction")
return nil
}
log.V(1).Info("Commit transaction")
return tx.Commit()
}