Skip to content

Commit

Permalink
Fix some nitpicks
Browse files Browse the repository at this point in the history
  • Loading branch information
Kidswiss committed Jun 13, 2023
1 parent a2405cf commit 037f94f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
18 changes: 9 additions & 9 deletions cmd/slareport.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ func (c *controller) runReport(cmd *cobra.Command, _ []string) error {
return err
}

uploader := slareport.PDFUploader{}
err = uploader.Login(cmd.Context(),
uploader, err := slareport.NewPDFUploader(cmd.Context(),
endpointURL,
bucket,
awsaccesskeyid,
Expand All @@ -80,17 +79,18 @@ func (c *controller) runReport(cmd *cobra.Command, _ []string) error {
return err
}

for customer, metrics := range metrics {
parsedDate, err := time.Parse(time.RFC3339, date)
if err != nil {
return err
}

for customer, instanceMetrics := range metrics {

l.Info("Rendering PDF for customer", "customer", customer)
parsedDate, err := time.Parse(time.RFC3339, date)
if err != nil {
return err
}

renderer := slareport.SLARenderer{
Customer: customer,
SI: metrics,
SI: instanceMetrics,
Month: parsedDate.Month(),
Year: parsedDate.Year(),
ExceptionLink: "https://products.vshn.ch/service_levels.html#_exceptions_to_availability_guarantee",
Expand All @@ -102,7 +102,7 @@ func (c *controller) runReport(cmd *cobra.Command, _ []string) error {
}

l.Info("Uploading PDF", "customer", customer, "endpoint", endpointURL, "bucket", bucket)
err = uploader.Upload(slareport.PDF{
err = uploader.Upload(cmd.Context(), slareport.PDF{
Customer: customer,
Date: parsedDate,
PDFData: pdf,
Expand Down
14 changes: 8 additions & 6 deletions pkg/slareport/prom.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (
metricQuery = `(1 - max without(prometheus_replica) (sum_over_time(slo:sli_error:ratio_rate5m{sloth_service=~"appcat-.+"}[{{DURATION}}])
/ ignoring (sloth_window) count_over_time(slo:sli_error:ratio_rate5m{sloth_service=~"appcat-.+"}[{{DURATION}}])
) >= 0) * 100`
slaQuery = `slo:objective:ratio{sloth_id="{{SLOTHID}}"}`
slaQuery = `100*slo:objective:ratio{sloth_id="{{SLOTHID}}"}`
promClientFunc = getPrometheusAPIClient
getMetricsFunc = getSLAMetrics
getTargetSLAFunc = getTargetSLA
Expand Down Expand Up @@ -147,7 +147,8 @@ func getSLAMetrics(ctx context.Context, startDate, endDate *time.Time, timeRange
}

if len(warnings) != 0 {
fmt.Println(warnings)
warns := strings.Join(warnings, ",")
log.FromContext(ctx).Info("There were warnings during the prom query", "warnings", warns)
}

samples, ok := value.(model.Matrix)
Expand All @@ -161,13 +162,14 @@ func getSLAMetrics(ctx context.Context, startDate, endDate *time.Time, timeRange
func getTargetSLA(ctx context.Context, slothID string, client apiv1.API, endDate *time.Time) (float64, error) {
query := strings.Replace(slaQuery, "{{SLOTHID}}", slothID, 1)

res, warns, err := client.Query(ctx, query, *endDate)
res, warnings, err := client.Query(ctx, query, *endDate)
if err != nil {
return 0, err
}

if len(warns) != 0 {
fmt.Println(warns)
if len(warnings) != 0 {
warns := strings.Join(warnings, ",")
log.FromContext(ctx).Info("There were warnings during the prom query", "warnings", warns)
}

samples, ok := res.(model.Vector)
Expand All @@ -179,5 +181,5 @@ func getTargetSLA(ctx context.Context, slothID string, client apiv1.API, endDate
return 0, errors.New("no target SLA found in prometheus")
}

return float64(samples[len(samples)-1].Value) * 100, nil
return float64(samples[len(samples)-1].Value), nil
}
8 changes: 8 additions & 0 deletions pkg/slareport/prom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ func TestRunQuery(t *testing.T) {
assert.Equal(t, tt.want, got)
})
}

resetGlobalFunctions()
}

func getDummyPromClient(promURL string, thanosAllowPartialResponses bool, orgID string) (apiv1.API, error) {
Expand All @@ -201,3 +203,9 @@ func getDummyPromClient(promURL string, thanosAllowPartialResponses bool, orgID
func getDummySLA(ctx context.Context, slothID string, client apiv1.API, endDate *time.Time) (float64, error) {
return 99.9, nil
}

func resetGlobalFunctions() {
promClientFunc = getPrometheusAPIClient
getMetricsFunc = getSLAMetrics
getTargetSLAFunc = getTargetSLA
}
20 changes: 10 additions & 10 deletions pkg/slareport/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
type PDFUploader struct {
client *minio.Client
bucket string
ctx context.Context
}

// PDF is contains all metainformation to upload a PDF to S3.
Expand All @@ -27,44 +26,45 @@ type PDF struct {
PDFData io.ReadCloser
}

// Login initializes an S3 client.
func (p *PDFUploader) Login(ctx context.Context, endpoint, bucket, keyID, secretKey string) error {
// NewPDFUploader initializes an S3 client.
func NewPDFUploader(ctx context.Context, endpoint, bucket, keyID, secretKey string) (PDFUploader, error) {

log.FromContext(ctx).V(1).Info("Logging into S3 endpoint", "endpointurl", endpoint)

p := PDFUploader{}

url, err := url.Parse(endpoint)
if err != nil {
return err
return p, nil
}

S3Client, err := minio.New(url.Host, &minio.Options{
Creds: credentials.NewStaticV4(keyID, secretKey, ""),
Secure: url.Scheme == "https",
})
if err != nil {
return err
return p, err
}

p.client = S3Client
p.bucket = bucket
p.ctx = ctx
return nil
return p, nil
}

// Upload uploads the given PDF to the logged in S3 enspoint.
// It will create an object with the pattern `year/month/customer.pdf`.
func (p *PDFUploader) Upload(pdf PDF) error {
func (p *PDFUploader) Upload(ctx context.Context, pdf PDF) error {

obj := fmt.Sprintf("%d/%s/%s.pdf", pdf.Date.Year(), pdf.Date.Month(), pdf.Customer)

log.FromContext(p.ctx).V(1).Info("Uploading PDF", "object", obj)
log.FromContext(ctx).V(1).Info("Uploading PDF", "object", obj)

buf := &bytes.Buffer{}
size, err := io.Copy(buf, pdf.PDFData)
if err != nil {
return err
}

_, err = p.client.PutObject(p.ctx, p.bucket, obj, buf, size, minio.PutObjectOptions{})
_, err = p.client.PutObject(ctx, p.bucket, obj, buf, size, minio.PutObjectOptions{})
return err
}

0 comments on commit 037f94f

Please sign in to comment.