Skip to content

Commit

Permalink
Migrate to AWS SDK Go V2
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara committed May 15, 2024
1 parent cd34bd3 commit 3ef4431
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ updates:
interval: weekly
open-pull-requests-limit: 10
groups:
aws-sdk-go:
aws-sdk-go-v2:
patterns:
- "github.com/aws/aws-sdk-go*"
- "github.com/aws/aws-sdk-go-v2*"
otel:
patterns:
- "go.opentelemetry.io/otel*"
Expand Down
14 changes: 8 additions & 6 deletions backup.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package maprobe

import (
"context"
"encoding/json"
"log"

"github.com/aws/aws-sdk-go/service/firehose"
"github.com/aws/aws-sdk-go-v2/service/firehose"

Check failure on line 8 in backup.go

View workflow job for this annotation

GitHub Actions / Build (1.20)

missing go.sum entry for module providing package github.com/aws/aws-sdk-go-v2/service/firehose (imported by github.com/fujiwara/maprobe); to add:

Check failure on line 8 in backup.go

View workflow job for this annotation

GitHub Actions / Build (1.21)

missing go.sum entry for module providing package github.com/aws/aws-sdk-go-v2/service/firehose (imported by github.com/fujiwara/maprobe); to add:
"github.com/aws/aws-sdk-go-v2/service/firehose/types"

Check failure on line 9 in backup.go

View workflow job for this annotation

GitHub Actions / Build (1.20)

missing go.sum entry for module providing package github.com/aws/aws-sdk-go-v2/service/firehose/types (imported by github.com/fujiwara/maprobe); to add:

Check failure on line 9 in backup.go

View workflow job for this annotation

GitHub Actions / Build (1.21)

missing go.sum entry for module providing package github.com/aws/aws-sdk-go-v2/service/firehose/types (imported by github.com/fujiwara/maprobe); to add:
"github.com/mackerelio/mackerel-client-go"
)

type backupClient struct {
svc *firehose.Firehose
svc *firehose.Client
streamName string
}

Expand All @@ -28,9 +30,9 @@ func (c *backupClient) PostServiceMetricValues(service string, mvs []*mackerel.M
if err != nil {
return err
}
_, err = c.svc.PutRecord(&firehose.PutRecordInput{
_, err = c.svc.PutRecord(context.TODO(), &firehose.PutRecordInput{
DeliveryStreamName: &c.streamName,
Record: &firehose.Record{Data: data},
Record: &types.Record{Data: data},
})
return err
}
Expand All @@ -43,9 +45,9 @@ func (c *backupClient) PostHostMetricValues(mvs []*mackerel.HostMetricValue) err
if err != nil {
return err
}
_, err = c.svc.PutRecord(&firehose.PutRecordInput{
_, err = c.svc.PutRecord(context.TODO(), &firehose.PutRecordInput{
DeliveryStreamName: &c.streamName,
Record: &firehose.Record{Data: data},
Record: &types.Record{Data: data},
})
return err
}
12 changes: 8 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package maprobe

import (
"context"
"fmt"
"log"
"net/http"
"os"
"sync"
"time"

"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/firehose"
"github.com/aws/aws-sdk-go-v2/config"

Check failure on line 12 in client.go

View workflow job for this annotation

GitHub Actions / Build (1.20)

missing go.sum entry for module providing package github.com/aws/aws-sdk-go-v2/config (imported by github.com/fujiwara/maprobe); to add:

Check failure on line 12 in client.go

View workflow job for this annotation

GitHub Actions / Build (1.21)

missing go.sum entry for module providing package github.com/aws/aws-sdk-go-v2/config (imported by github.com/fujiwara/maprobe); to add:
"github.com/aws/aws-sdk-go-v2/service/firehose"
mackerel "github.com/mackerelio/mackerel-client-go"
)

Expand All @@ -24,9 +25,12 @@ func newClient(apiKey string, backupStream string) *Client {
}
if backupStream != "" {
log.Println("[info] setting backup firehose stream:", backupStream)
sess := session.Must(session.NewSession())
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}
c.backupClient = &backupClient{
svc: firehose.New(sess),
svc: firehose.NewFromConfig(cfg),
streamName: backupStream,
}
}
Expand Down
27 changes: 18 additions & 9 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package maprobe

import (
"bytes"
"context"
"crypto/sha256"
"encoding/json"
"fmt"
Expand All @@ -11,10 +13,9 @@ import (
"os"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/aws/aws-sdk-go-v2/aws"

Check failure on line 16 in config.go

View workflow job for this annotation

GitHub Actions / Build (1.20)

missing go.sum entry for module providing package github.com/aws/aws-sdk-go-v2/aws (imported by github.com/fujiwara/maprobe); to add:

Check failure on line 16 in config.go

View workflow job for this annotation

GitHub Actions / Build (1.21)

missing go.sum entry for module providing package github.com/aws/aws-sdk-go-v2/aws (imported by github.com/fujiwara/maprobe); to add:
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"

Check failure on line 18 in config.go

View workflow job for this annotation

GitHub Actions / Build (1.20)

missing go.sum entry for module providing package github.com/aws/aws-sdk-go-v2/service/s3 (imported by github.com/fujiwara/maprobe); to add:

Check failure on line 18 in config.go

View workflow job for this annotation

GitHub Actions / Build (1.21)

missing go.sum entry for module providing package github.com/aws/aws-sdk-go-v2/service/s3 (imported by github.com/fujiwara/maprobe); to add:
mackerel "github.com/mackerelio/mackerel-client-go"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -263,16 +264,24 @@ func fetchHTTP(u *url.URL) ([]byte, error) {

func fetchS3(u *url.URL) ([]byte, error) {
log.Println("[debug] fetching S3", u)
sess := session.Must(session.NewSession())
downloader := s3manager.NewDownloader(sess)
ctx := context.TODO()
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
return nil, err
}
svc := s3.NewFromConfig(cfg)

buf := &aws.WriteAtBuffer{}
_, err := downloader.Download(buf, &s3.GetObjectInput{
buf := &bytes.Buffer{}
out, err := svc.GetObject(ctx, &s3.GetObjectInput{
Bucket: aws.String(u.Host),
Key: aws.String(u.Path),
})
if err != nil {
return nil, fmt.Errorf("failed to fetch from S3, %s", err)
return nil, fmt.Errorf("failed to get from S3, %w", err)
}
defer out.Body.Close()
if _, err := io.Copy(buf, out.Body); err != nil {
return nil, fmt.Errorf("failed to download from s3, %w", err)
}
return buf.Bytes(), nil
}
Expand Down
21 changes: 20 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ go 1.20

require (
github.com/aws/aws-lambda-go v1.39.1
github.com/aws/aws-sdk-go v1.44.54
github.com/aws/aws-sdk-go v1.53.2
github.com/aws/aws-sdk-go-v2 v1.26.1
github.com/aws/aws-sdk-go-v2/config v1.27.13
github.com/aws/aws-sdk-go-v2/service/firehose v1.28.7
github.com/aws/aws-sdk-go-v2/service/s3 v1.54.0
github.com/fujiwara/ridge v0.6.2
github.com/google/go-cmp v0.5.9
github.com/google/gops v0.3.25
Expand All @@ -24,6 +28,21 @@ require (
require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.13 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.5 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.3.7 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.7 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.5 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.20.6 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.24.0 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.28.7 // indirect
github.com/aws/smithy-go v1.20.2 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand Down

0 comments on commit 3ef4431

Please sign in to comment.