s3_client.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package multitenant
  2. import (
  3. "bytes"
  4. "sync"
  5. "context"
  6. "github.com/aws/aws-sdk-go/aws"
  7. "github.com/aws/aws-sdk-go/aws/session"
  8. "github.com/aws/aws-sdk-go/service/s3"
  9. "github.com/prometheus/client_golang/prometheus"
  10. "github.com/weaveworks/common/instrument"
  11. "github.com/weaveworks/scope/report"
  12. )
  13. var (
  14. s3RequestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
  15. Namespace: "scope",
  16. Name: "s3_request_duration_seconds",
  17. Help: "Time in seconds spent doing S3 requests.",
  18. Buckets: prometheus.DefBuckets,
  19. }, []string{"method", "status_code"})
  20. )
  21. // S3Store is an S3 client that stores and retrieves Reports.
  22. type S3Store struct {
  23. s3 *s3.S3
  24. bucketName string
  25. }
  26. func registerS3ClientMetrics() {
  27. prometheus.MustRegister(s3RequestDuration)
  28. }
  29. var registerS3ClientMetricsOnce sync.Once
  30. // NewS3Client creates a new S3 client.
  31. func NewS3Client(config *aws.Config, bucketName string) S3Store {
  32. registerS3ClientMetricsOnce.Do(registerS3ClientMetrics)
  33. return S3Store{
  34. s3: s3.New(session.New(config)),
  35. bucketName: bucketName,
  36. }
  37. }
  38. // FetchReports fetches multiple reports in parallel from S3.
  39. func (store *S3Store) FetchReports(ctx context.Context, keys []string) (map[string]report.Report, []string, error) {
  40. type result struct {
  41. key string
  42. report *report.Report
  43. err error
  44. }
  45. ch := make(chan result, len(keys))
  46. for _, key := range keys {
  47. go func(key string) {
  48. r := result{key: key}
  49. r.report, r.err = store.fetchReport(ctx, key)
  50. ch <- r
  51. }(key)
  52. }
  53. reports := map[string]report.Report{}
  54. for range keys {
  55. r := <-ch
  56. if r.err != nil {
  57. return nil, []string{}, r.err
  58. }
  59. reports[r.key] = *r.report
  60. }
  61. return reports, []string{}, nil
  62. }
  63. func (store *S3Store) fetchReport(ctx context.Context, key string) (*report.Report, error) {
  64. var resp *s3.GetObjectOutput
  65. err := instrument.TimeRequestHistogram(ctx, "S3.Get", s3RequestDuration, func(_ context.Context) error {
  66. var err error
  67. resp, err = store.s3.GetObject(&s3.GetObjectInput{
  68. Bucket: aws.String(store.bucketName),
  69. Key: aws.String(key),
  70. })
  71. return err
  72. })
  73. if err != nil {
  74. return nil, err
  75. }
  76. defer resp.Body.Close()
  77. return report.MakeFromBinary(ctx, resp.Body, true, true)
  78. }
  79. // StoreReportBytes stores a report.
  80. func (store *S3Store) StoreReportBytes(ctx context.Context, key string, buf []byte) (int, error) {
  81. err := instrument.TimeRequestHistogram(ctx, "S3.Put", s3RequestDuration, func(_ context.Context) error {
  82. _, err := store.s3.PutObject(&s3.PutObjectInput{
  83. Body: bytes.NewReader(buf),
  84. Bucket: aws.String(store.bucketName),
  85. Key: aws.String(key),
  86. })
  87. return err
  88. })
  89. return len(buf), err
  90. }