-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[usage] Upload usage reports to cloud storage #11519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,14 +5,13 @@ | |
package controller | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"context" | ||
"database/sql" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"math" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/gitpod-io/gitpod/common-go/log" | ||
|
@@ -76,36 +75,26 @@ func (u *UsageReconciler) Reconcile() (err error) { | |
} | ||
log.WithField("usage_reconcile_status", status).Info("Reconcile completed.") | ||
|
||
// For now, write the report to a temp directory so we can manually retrieve it | ||
dir := os.TempDir() | ||
f, err := ioutil.TempFile(dir, fmt.Sprintf("%s-*", now.Format(time.RFC3339))) | ||
if err != nil { | ||
return fmt.Errorf("failed to create temporary file: %w", err) | ||
} | ||
defer f.Close() | ||
|
||
enc := json.NewEncoder(f) | ||
err = enc.Encode(report) | ||
reportBytes := &bytes.Buffer{} | ||
gz := gzip.NewWriter(reportBytes) | ||
err = json.NewEncoder(gz).Encode(report) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is where the json report is gzipped. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd recommend moving this into the Storage Client. As it stands it's confusing because the client sets the content type gzipped, but nothing in the API signature indicates it should be gzipped ahead of it. Ideally, the Client takes a bytes buffer and wraps it in a gzip writer, then writes the whole thing as a stream There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or even can take an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The As such, I've renamed the method on the interface and changed its argument to be a report rather than an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also moved the json encoding and compression of the report away from the caller and into the method. |
||
if err != nil { | ||
return fmt.Errorf("failed to marshal report to JSON: %w", err) | ||
} | ||
|
||
stat, err := f.Stat() | ||
filePath := filepath.Join(dir, stat.Name()) | ||
err = gz.Close() | ||
if err != nil { | ||
return fmt.Errorf("failed to get file stats: %w", err) | ||
return fmt.Errorf("failed to compress usage report: %w", err) | ||
} | ||
log.Infof("Wrote usage report into %s", filePath) | ||
|
||
uploadURL, err := u.contentService.GetSignedUploadUrl(ctx) | ||
err = db.CreateUsageRecords(ctx, u.conn, usageReportToUsageRecords(report, u.pricer, u.nowFunc().UTC())) | ||
if err != nil { | ||
return fmt.Errorf("failed to obtain signed upload URL: %w", err) | ||
return fmt.Errorf("failed to write usage records to database: %s", err) | ||
} | ||
log.Infof("signed upload url: %s", uploadURL) | ||
|
||
err = db.CreateUsageRecords(ctx, u.conn, usageReportToUsageRecords(report, u.pricer, u.nowFunc().UTC())) | ||
filename := fmt.Sprintf("%s.gz", now.Format(time.RFC3339)) | ||
err = u.contentService.UploadFile(ctx, filename, reportBytes) | ||
if err != nil { | ||
return fmt.Errorf("failed to write usage records to database: %s", err) | ||
return fmt.Errorf("failed to upload usage report: %w", err) | ||
} | ||
|
||
return nil | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this cause the content to be gzipped on the server? I would've expected you set this to indicate that the content you're uploading is
gzip
but I don't see us compressing in this implementation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See this comment.