forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupload.go
47 lines (41 loc) · 1.33 KB
/
upload.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
package image
import (
"context"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/grafana/grafana/pkg/components/imguploader"
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
)
type UploadingService struct {
uploader imguploader.ImageUploader
failures prometheus.Counter
successes prometheus.Counter
}
func NewUploadingService(uploader imguploader.ImageUploader, r prometheus.Registerer) *UploadingService {
return &UploadingService{
uploader: uploader,
failures: promauto.With(r).NewCounter(prometheus.CounterOpts{
Name: "image_upload_failures_total",
Namespace: "grafana",
Subsystem: "alerting",
}),
successes: promauto.With(r).NewCounter(prometheus.CounterOpts{
Name: "image_upload_successes_total",
Namespace: "grafana",
Subsystem: "alerting",
}),
}
}
// Upload uploads an image and returns a new image with the unmodified path and a URL.
// It returns the unmodified image on error.
func (s *UploadingService) Upload(ctx context.Context, image ngmodels.Image) (ngmodels.Image, error) {
url, err := s.uploader.Upload(ctx, image.Path)
if err != nil {
defer s.failures.Inc()
return image, fmt.Errorf("failed to upload screenshot: %w", err)
}
image.URL = url
defer s.successes.Inc()
return image, nil
}