From 64027e9a967edb5d5015e69ec268909f9520be3f Mon Sep 17 00:00:00 2001 From: Ryo Takaishi Date: Wed, 18 Oct 2023 09:58:57 +0900 Subject: [PATCH] chore: upgrade datadog-api-client-go --- go.mod | 16 ++++++------- go.sum | 44 ++++++++++++++++++---------------- metrics_submitter.go | 50 +++++++++++++++++++-------------------- metrics_submitter_test.go | 14 +++++------ 4 files changed, 63 insertions(+), 61 deletions(-) diff --git a/go.mod b/go.mod index e31c220..84383d5 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/reproio/aws-lambda-functions/functions/send-alb-metrics-to-dat go 1.19 require ( - github.com/DataDog/datadog-api-client-go v1.15.1 + github.com/DataDog/datadog-api-client-go/v2 v2.18.0 github.com/aws/aws-lambda-go v1.29.0 github.com/aws/aws-sdk-go v1.40.2 github.com/pkg/errors v0.9.1 @@ -13,14 +13,14 @@ require ( ) require ( - github.com/DataDog/zstd v1.5.0 // indirect - github.com/golang/protobuf v1.5.2 // indirect + github.com/DataDog/zstd v1.5.2 // indirect + github.com/goccy/go-json v0.10.2 // indirect + github.com/golang/protobuf v1.5.3 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/stretchr/testify v1.7.0 // indirect - golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect - golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect - golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect - google.golang.org/appengine v1.4.0 // indirect - google.golang.org/protobuf v1.26.0 // indirect + golang.org/x/net v0.17.0 // indirect + golang.org/x/oauth2 v0.10.0 // indirect + google.golang.org/appengine v1.6.7 // indirect + google.golang.org/protobuf v1.31.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index 12e4d35..bb2fe50 100644 --- a/go.sum +++ b/go.sum @@ -1,20 +1,21 @@ -cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -github.com/DataDog/datadog-api-client-go v1.15.1 h1:JFlv1StngyD6qVvZY/Z3zBLJ6iOg2146hK3NGsoM4o0= -github.com/DataDog/datadog-api-client-go v1.15.1/go.mod h1:PgrP2ABuJWL3Auw2iEkemAJ/r72ghG4DQQmb5sgnKW4= -github.com/DataDog/zstd v1.5.0 h1:+K/VEwIAaPcHiMtQvpLD4lqW7f0Gk3xdYZmI1hD+CXo= -github.com/DataDog/zstd v1.5.0/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw= +github.com/DataDog/datadog-api-client-go/v2 v2.18.0 h1:QkX1vY1JtvDeF6N6EarPrJMHlerVGdTvGQOUWqfeVDs= +github.com/DataDog/datadog-api-client-go/v2 v2.18.0/go.mod h1:lHlfhsNQ2qZclvpVQTfrGowUDIdIzAao38A05f9EQpc= +github.com/DataDog/zstd v1.5.2 h1:vUG4lAyuPCXO0TLbXvPv7EB7cNK1QV/luu55UHLrrn8= +github.com/DataDog/zstd v1.5.2/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw= github.com/aws/aws-lambda-go v1.29.0 h1:u+sfZkvNBUgt0ZkO8Q/jOMBV22DqMDMbZu04oomM2no= github.com/aws/aws-lambda-go v1.29.0/go.mod h1:aakqVz9vDHhtbt0U2zegh/z9SI2+rJ+yRREZYNQLmWY= github.com/aws/aws-sdk-go v1.40.2 h1:iNaJUKjUeULTsuTGrGbAFG1H5AVSWgo5kwyUDmtJrwk= github.com/aws/aws-sdk-go v1.40.2/go.mod h1:585smgzpB/KqRA+K3y/NL/oYRqQvpNJYvLm+LY1U59Q= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= -github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= -github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= +github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= @@ -26,32 +27,33 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/exp v0.0.0-20220516143420-24438e51023a h1:tiLLxEjKNE6Hrah/Dp/cyHvsyjDLcMFSocOHO5XDmOM= golang.org/x/exp v0.0.0-20220516143420-24438e51023a/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE= -golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20210614182718-04defd469f4e h1:XpT3nA5TvE525Ne3hInMh6+GETgn27Zfm9dxsThnX2Q= +golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BGiEpg5IG2JkU5FkPIawgtw= -golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= -golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= +golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= +golang.org/x/oauth2 v0.10.0 h1:zHCpF2Khkwy4mMB4bv0U37YtJdTGW8jI0glAApi0Kh8= +golang.org/x/oauth2 v0.10.0/go.mod h1:kTpgurOux7LqtuxjuyZa4Gj2gdezIt/jQtGnNFfypQI= golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f h1:Ax0t5p6N38Ga0dThY21weqDEyz2oklo4IvDkpigvkD8= golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk= -golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8= -google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508= -google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= +google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/metrics_submitter.go b/metrics_submitter.go index 72b04a2..73c80f1 100644 --- a/metrics_submitter.go +++ b/metrics_submitter.go @@ -3,8 +3,9 @@ package main import ( "context" "fmt" - v1 "github.com/DataDog/datadog-api-client-go/api/v1/datadog" - "github.com/DataDog/datadog-api-client-go/api/v2/datadog" + "github.com/DataDog/datadog-api-client-go/v2/api/datadog" + "github.com/DataDog/datadog-api-client-go/v2/api/datadogV1" + "github.com/DataDog/datadog-api-client-go/v2/api/datadogV2" "github.com/pkg/errors" "golang.org/x/sync/errgroup" "os" @@ -27,30 +28,29 @@ func NewMetricsSubmitter(requestCountMetricName string, targetProcessingTimeMetr func (p *MetricsSubmitter) Submit(metrics map[string]*Metric, s3ObjectKey string) error { var eg errgroup.Group - // v1ctx and v1client are used to submit distribution points api. This api is defined by v1, so need client for it. - v1ctx := v1.NewDefaultContext(context.Background()) - v1client := v1.NewAPIClient(v1.NewConfiguration()) - - // v2ctx and v2client are used to submit metrics api. This api is defined by v1 and v2, but v1 api is deprecated. - v2ctx := datadog.NewDefaultContext(context.Background()) - v2client := datadog.NewAPIClient(datadog.NewConfiguration()) + ctx := datadog.NewDefaultContext(context.Background()) + apiClient := datadog.NewAPIClient(datadog.NewConfiguration()) + // v1Api is used to submit distribution points api. This api is defined by v1, so need client for it. + v1Api := datadogV1.NewMetricsApi(apiClient) + // v2Api is used to submit metrics api. This api is defined by v1 and v2, but v1 api is deprecated. + v2Api := datadogV2.NewMetricsApi(apiClient) for _, metric := range metrics { - payload := datadog.MetricPayload{ - Series: []datadog.MetricSeries{ + metricsPayload := datadogV2.MetricPayload{ + Series: []datadogV2.MetricSeries{ p.requestCountSeries(metric, s3ObjectKey), }, } - v1payload := v1.DistributionPointsPayload{} + distributionPointPayload := datadogV1.DistributionPointsPayload{} s, err := p.targetProcessingTime(metric) if err != nil { return err } - v1payload.Series = append(v1payload.Series, s...) + distributionPointPayload.Series = append(distributionPointPayload.Series, s...) eg.Go(func() error { - _, r, err := v1client.MetricsApi.SubmitDistributionPoints(v1ctx, v1payload, *v1.NewSubmitDistributionPointsOptionalParameters().WithContentEncoding(v1.DISTRIBUTIONPOINTSCONTENTENCODING_DEFLATE)) + _, r, err := v1Api.SubmitDistributionPoints(ctx, distributionPointPayload, *datadogV1.NewSubmitDistributionPointsOptionalParameters().WithContentEncoding(datadogV1.DISTRIBUTIONPOINTSCONTENTENCODING_DEFLATE)) if err != nil { fmt.Fprintf(os.Stderr, "Error when calling `MetricsApi.SubmitDistributionPoints`: %v\n", err) fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r) @@ -60,7 +60,7 @@ func (p *MetricsSubmitter) Submit(metrics map[string]*Metric, s3ObjectKey string }) eg.Go(func() error { - _, r, err := v2client.MetricsApi.SubmitMetrics(v2ctx, payload, *datadog.NewSubmitMetricsOptionalParameters()) + _, r, err := v2Api.SubmitMetrics(ctx, metricsPayload, *datadogV2.NewSubmitMetricsOptionalParameters()) if err != nil { fmt.Fprintf(os.Stderr, "Error when calling `MetricsApi.SubmitMetrics`: %v\n", err) fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r) @@ -77,16 +77,16 @@ func (p *MetricsSubmitter) Submit(metrics map[string]*Metric, s3ObjectKey string return nil } -func (p *MetricsSubmitter) requestCountSeries(metric *Metric, s3ObjectKey string) datadog.MetricSeries { - var points []datadog.MetricPoint +func (p *MetricsSubmitter) requestCountSeries(metric *Metric, s3ObjectKey string) datadogV2.MetricSeries { + var points []datadogV2.MetricPoint for timestamp, count := range metric.RequestCountMap { - points = append(points, datadog.MetricPoint{ + points = append(points, datadogV2.MetricPoint{ Timestamp: timestamp.PtrInt64(), Value: count.PtrFloat64(), }) } - series := datadog.NewMetricSeries(p.requestCountMetricName, points) - series.SetType(datadog.METRICINTAKETYPE_COUNT) + series := datadogV2.NewMetricSeries(p.requestCountMetricName, points) + series.SetType(datadogV2.METRICINTAKETYPE_COUNT) series.SetInterval(60) series.SetUnit("request") tags := []string{ @@ -105,18 +105,18 @@ func (p *MetricsSubmitter) requestCountSeries(metric *Metric, s3ObjectKey string return *series } -func (p *MetricsSubmitter) targetProcessingTime(metric *Metric) ([]v1.DistributionPointsSeries, error) { - seriesSlice := make([]v1.DistributionPointsSeries, 1) - points := make([][]v1.DistributionPointItem, 0, len(metric.TargetProcessingTimesMap)) +func (p *MetricsSubmitter) targetProcessingTime(metric *Metric) ([]datadogV1.DistributionPointsSeries, error) { + seriesSlice := make([]datadogV1.DistributionPointsSeries, 1) + points := make([][]datadogV1.DistributionPointItem, 0, len(metric.TargetProcessingTimesMap)) for timestamp, times := range metric.TargetProcessingTimesMap { - points = append(points, []v1.DistributionPointItem{ + points = append(points, []datadogV1.DistributionPointItem{ {DistributionPointTimestamp: timestamp.PtrFloat64()}, {DistributionPointData: times.Float64()}, }) } - series := v1.NewDistributionPointsSeries(p.targetProcessingTimeMetricName, points) + series := datadogV1.NewDistributionPointsSeries(p.targetProcessingTimeMetricName, points) tags := []string{ fmt.Sprintf("elb:%s", metric.Elb), fmt.Sprintf("target_group_arn:%s", metric.TargetGroupArn), diff --git a/metrics_submitter_test.go b/metrics_submitter_test.go index bf995fc..08b9736 100644 --- a/metrics_submitter_test.go +++ b/metrics_submitter_test.go @@ -1,8 +1,8 @@ package main import ( - v1 "github.com/DataDog/datadog-api-client-go/api/v1/datadog" - "github.com/DataDog/datadog-api-client-go/api/v2/datadog" + "github.com/DataDog/datadog-api-client-go/v2/api/datadog" + "github.com/DataDog/datadog-api-client-go/v2/api/datadogV1" "reflect" "testing" ) @@ -49,7 +49,7 @@ func TestMetricsSubmitter_loadBalancerIpAddress(t *testing.T) { } func TestMetricsSubmitter_targetProcessingTime(t *testing.T) { - var typeVar v1.DistributionPointsType = v1.DISTRIBUTIONPOINTSTYPE_DISTRIBUTION + var typeVar datadogV1.DistributionPointsType = datadogV1.DISTRIBUTIONPOINTSTYPE_DISTRIBUTION type fields struct { RequestCountMetricName string TargetProcessingTimeMetricName string @@ -62,7 +62,7 @@ func TestMetricsSubmitter_targetProcessingTime(t *testing.T) { name string fields fields args args - want []v1.DistributionPointsSeries + want []datadogV1.DistributionPointsSeries wantErr bool }{ { @@ -84,10 +84,10 @@ func TestMetricsSubmitter_targetProcessingTime(t *testing.T) { TargetGroupArn: "arn", }, }, - want: []v1.DistributionPointsSeries{ - v1.DistributionPointsSeries{ + want: []datadogV1.DistributionPointsSeries{ + datadogV1.DistributionPointsSeries{ Metric: "target_processing_time", - Points: [][]v1.DistributionPointItem{ + Points: [][]datadogV1.DistributionPointItem{ { {DistributionPointTimestamp: datadog.PtrFloat64(1)}, {DistributionPointData: &[]float64{1, 2}},