Skip to content
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

[usage] Report upload/download metrics for usage report #12564

Merged
merged 1 commit into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions components/usage/pkg/contentservice/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"time"

"github.com/gitpod-io/gitpod/common-go/log"
"github.com/gitpod-io/gitpod/content-service/api"
Expand All @@ -29,7 +30,12 @@ func New(service api.UsageReportServiceClient) *Client {
return &Client{service: service}
}

func (c *Client) UploadUsageReport(ctx context.Context, filename string, report UsageReport) error {
func (c *Client) UploadUsageReport(ctx context.Context, filename string, report UsageReport) (err error) {
start := time.Now()
defer func() {
observeReportUploadDuration(time.Since(start), err)
}()

uploadURLResp, err := c.service.UploadURL(ctx, &api.UsageReportUploadURLRequest{Name: filename})
if err != nil {
return fmt.Errorf("failed to get upload URL from usage report service: %w", err)
Expand Down Expand Up @@ -67,7 +73,12 @@ func (c *Client) UploadUsageReport(ctx context.Context, filename string, report
return nil
}

func (c *Client) DownloadUsageReport(ctx context.Context, filename string) (UsageReport, error) {
func (c *Client) DownloadUsageReport(ctx context.Context, filename string) (report UsageReport, err error) {
start := time.Now()
defer func() {
observeReportDownloadDuration(time.Since(start), err)
}()

downloadURlResp, err := c.service.DownloadURL(ctx, &api.UsageReportDownloadURLRequest{
Name: filename,
})
Expand Down Expand Up @@ -104,7 +115,6 @@ func (c *Client) DownloadUsageReport(ctx context.Context, filename string) (Usag
defer decompressor.Close()

decoder := json.NewDecoder(decompressor)
var report UsageReport
if err := decoder.Decode(&report); err != nil {
return UsageReport{}, fmt.Errorf("failed to deserialize report: %w", err)
}
Expand Down
63 changes: 63 additions & 0 deletions components/usage/pkg/contentservice/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.

package contentservice

import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"time"
)

const (
namespace = "gitpod"
subsystem = "usage"
)

var (
reportUploadDurationSeconds = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "report_upload_duration_seconds",
Help: "Histogram of time it takes (in seconds) to upload a usage report to content service",
}, []string{"outcome"})

reportDownloadDurationSeconds = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "report_download_duration_seconds",
Help: "Histogram of time it takes (in seconds) to download usage report from content service",
}, []string{"outcome"})
)

func RegisterMetrics(reg *prometheus.Registry) error {
metrics := []prometheus.Collector{
reportUploadDurationSeconds,
reportDownloadDurationSeconds,
}
for _, metric := range metrics {
err := reg.Register(metric)
if err != nil {
return fmt.Errorf("failed to register metric: %w", err)
}
}

return nil
}

func observeReportUploadDuration(d time.Duration, err error) {
outcome := "ok"
if err != nil {
outcome = "error"
}
reportUploadDurationSeconds.WithLabelValues(outcome).Observe(d.Seconds())
}

func observeReportDownloadDuration(d time.Duration, err error) {
outcome := "ok"
if err != nil {
outcome = "error"
}
reportDownloadDurationSeconds.WithLabelValues(outcome).Observe(d.Seconds())
}
4 changes: 4 additions & 0 deletions components/usage/pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ func Start(cfg Config) error {
log.Info("No controller schedule specified, controller will be disabled.")
}

err = contentservice.RegisterMetrics(srv.MetricsRegistry())
if err != nil {
return fmt.Errorf("failed to register content service metrics: %w", err)
}
var contentService contentservice.Interface = &contentservice.NoOpClient{}
if cfg.ContentServiceAddress != "" {
contentServiceConn, err := grpc.Dial(cfg.ContentServiceAddress, grpc.WithTransportCredentials(insecure.NewCredentials()))
Expand Down