Skip to content

Commit

Permalink
Merge pull request #23 from FindHotel/aws-sdk-v2
Browse files Browse the repository at this point in the history
[sc-142233] upgrade aws sdk v2 & use datadog-statsd client
  • Loading branch information
gumuz authored Jan 30, 2025
2 parents a558db7 + f30e878 commit b63be7b
Show file tree
Hide file tree
Showing 12 changed files with 220 additions and 415 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Latest stable branch is [master](https://github.com/FindHotel/analytics-go/tree/
If you use [go modules](https://blog.golang.org/v2-go-modules):

```shell
go get -u github.com/FindHotel/analytics-go@3.7.0
go get -u github.com/FindHotel/analytics-go@v4.0.0
```

## Documentation
Expand All @@ -38,7 +38,7 @@ package main
import (
"os"

analytics "github.com/FindHotel/analytics-go"
analytics "github.com/FindHotel/analytics-go/v4"
)

func main() {
Expand Down
59 changes: 30 additions & 29 deletions analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
"sync"
"time"

metrics "github.com/rcrowley/go-metrics"
)

// Version of the client.
const Version = "3.5.0"
const Version = "4.0.0"

// Client is the main API exposed by the analytics package.
// Values that satsify this interface are returned by the client constructors
Expand Down Expand Up @@ -60,12 +57,6 @@ type client struct {
// This HTTP client is used to send requests to the backend, it uses the
// HTTP transport provided in the configuration.
http http.Client

metricsRegistry metrics.Registry

successCounters countersFunc
failureCounters countersFunc
droppedCounters countersFunc
}

// NewDiscardClient returns client which discards all messages.
Expand Down Expand Up @@ -102,7 +93,6 @@ func NewWithConfig(writeKey string, config Config) (Client, error) {
return nil, err
}
go c.loop()
go c.loopMetrics()

return c, nil
}
Expand All @@ -113,19 +103,14 @@ func newWithConfig(writeKey string, config Config) (*client, error) {
}

c := &client{
Config: makeConfig(config),
key: writeKey,
msgs: make(chan Message, 100),
quit: make(chan struct{}),
shutdown: make(chan struct{}),
http: makeHTTPClient(config.Transport),
metricsRegistry: metrics.NewRegistry(),
Config: makeConfig(config),
key: writeKey,
msgs: make(chan Message, 100),
quit: make(chan struct{}),
shutdown: make(chan struct{}),
http: makeHTTPClient(config.Transport),
}

c.successCounters = c.newCounters("submitted.success")
c.failureCounters = c.newCounters("submitted.failure")
c.droppedCounters = c.newCounters("dropped")

return c, nil
}

Expand All @@ -141,7 +126,7 @@ func makeHTTPClient(transport http.RoundTripper) http.Client {

func (c *client) Enqueue(msg Message) (err error) {
if err = msg.validate(); err != nil {
c.droppedCounters(msg.tags()...).Inc(1)
c.notifyDropped(msg, err, 1)
return
}

Expand Down Expand Up @@ -200,8 +185,8 @@ func (c *client) Enqueue(msg Message) (err error) {
// and instead report that the client has been closed and shouldn't be
// used anymore.
if recover() != nil {
c.droppedCounters(msg.tags()...).Inc(1)
err = ErrClosed
c.notifyDropped(msg, err, 1)
}
}()

Expand Down Expand Up @@ -316,7 +301,7 @@ func (c *client) report(res *http.Response) (err error) {
return
}

if body, err = ioutil.ReadAll(res.Body); err != nil {
if body, err = io.ReadAll(res.Body); err != nil {
c.errorf("response %d %s - %s", res.StatusCode, res.Status, err)
return
}
Expand Down Expand Up @@ -416,9 +401,21 @@ func (c *client) maxBatchBytes() int {
return maxBatchBytes - len(b)
}

func (c *client) reportMetrics(name string, value int64, tags []string) {
statsd := c.Config.DDStatsdClient
if statsd == nil {
return
}

err := statsd.Count(name, value, tags, 1)
if err != nil {
c.errorf("error submitting metric %s - %s", name, err)
}
}

func (c *client) notifySuccess(msgs []message) {
for _, m := range msgs {
c.successCounters(m.Msg().tags()...).Inc(1)
c.reportMetrics("submitted.success", 1, m.Msg().tags())
}
if c.Callback != nil {
for _, m := range msgs {
Expand All @@ -429,7 +426,7 @@ func (c *client) notifySuccess(msgs []message) {

func (c *client) notifyFailure(msgs []message, err error) {
for _, m := range msgs {
c.failureCounters(m.Msg().tags()...).Inc(1)
c.reportMetrics("submitted.failure", 1, m.Msg().tags())
}
if c.Callback != nil {
for _, m := range msgs {
Expand All @@ -438,15 +435,19 @@ func (c *client) notifyFailure(msgs []message, err error) {
}
}

func (c *client) notifyDropped(m Message, err error, count int64) {
c.reportMetrics("dropped", count, m.tags())
}

func (c *client) notifyFailureMsg(m Message, err error, count int64) {
c.failureCounters(m.tags()...).Inc(count)
c.reportMetrics("submitted.failure", count, m.tags())
if c.Callback != nil {
c.Callback.Failure(m, err)
}
}

func (c *client) notifySuccessMsg(m Message, count int64) {
c.successCounters(m.tags()...).Inc(count)
c.reportMetrics("submitted.success", count, m.tags())
if c.Callback != nil {
c.Callback.Success(m)
}
Expand Down
9 changes: 4 additions & 5 deletions analytics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -86,7 +85,7 @@ var (
Proto: r.Proto,
ProtoMajor: r.ProtoMajor,
ProtoMinor: r.ProtoMinor,
Body: ioutil.NopCloser(strings.NewReader("")),
Body: io.NopCloser(strings.NewReader("")),
Request: r,
}, nil
})
Expand All @@ -105,7 +104,7 @@ var (
Proto: r.Proto,
ProtoMajor: r.ProtoMajor,
ProtoMinor: r.ProtoMinor,
Body: ioutil.NopCloser(strings.NewReader("")),
Body: io.NopCloser(strings.NewReader("")),
Request: r,
}, nil
})
Expand All @@ -118,7 +117,7 @@ var (
Proto: r.Proto,
ProtoMajor: r.ProtoMajor,
ProtoMinor: r.ProtoMinor,
Body: ioutil.NopCloser(readFunc(func(b []byte) (int, error) { return 0, testError })),
Body: io.NopCloser(readFunc(func(b []byte) (int, error) { return 0, testError })),
Request: r,
}, nil
})
Expand All @@ -135,7 +134,7 @@ func fixture(name string) string {
panic(err)
}
defer f.Close()
b, err := ioutil.ReadAll(f)
b, err := io.ReadAll(f)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"os"

"github.com/FindHotel/analytics-go"
"github.com/FindHotel/analytics-go/v4"
"github.com/segmentio/conf"
)

Expand Down
6 changes: 3 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"time"

ddstatsd "github.com/DataDog/datadog-go/v5/statsd"
"github.com/segmentio/backo-go"
"github.com/xtgo/uuid"
)
Expand Down Expand Up @@ -61,9 +62,8 @@ type Config struct {
// If not set the client will fallback to use a default retry policy.
RetryAfter func(int) time.Duration

// Reporters are used to report metrics to external reporting system such
// as DataDog. Useful implementations are DatadogReporter and LogReporter.
Reporters []Reporter
// DDStatsdClient is the client used to send metrics to Datadog.
DDStatsdClient ddstatsd.ClientInterface

// A function called by the client to generate unique message identifiers.
// The client uses a UUID generator if none is provided.
Expand Down
2 changes: 1 addition & 1 deletion examples/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"fmt"

"github.com/FindHotel/analytics-go"
"github.com/FindHotel/analytics-go/v4"
)
import "time"

Expand Down
50 changes: 38 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,23 +1,49 @@
module github.com/FindHotel/analytics-go
module github.com/FindHotel/analytics-go/v4

go 1.14
go 1.21

toolchain go1.23.3

require (
github.com/avast/retry-go v2.1.0+incompatible
github.com/aws/aws-sdk-go v1.19.1
github.com/cenkalti/backoff v2.1.0+incompatible // indirect
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
github.com/DataDog/datadog-go/v5 v5.5.0
github.com/aws/aws-sdk-go-v2 v1.34.0
github.com/aws/aws-sdk-go-v2/config v1.29.2
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.17.54
github.com/aws/aws-sdk-go-v2/service/s3 v1.74.1
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a
github.com/segmentio/backo-go v0.0.0-20160424052352-204274ad699c
github.com/segmentio/conf v1.0.0
github.com/stretchr/testify v1.8.1
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c
)

require (
github.com/Microsoft/go-winio v0.5.0 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.8 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.55 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.25 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.29 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.29 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.2 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.29 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.2 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.5.3 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.10 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.10 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.24.12 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.11 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.33.10 // indirect
github.com/aws/smithy-go v1.22.2 // indirect
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/segmentio/go-snakecase v1.0.0 // indirect
github.com/segmentio/objconv v1.0.1 // indirect
github.com/stretchr/objx v0.3.0 // indirect
github.com/stretchr/testify v1.6.1
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c
github.com/zorkian/go-datadog-api v2.18.0+incompatible
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
gopkg.in/go-playground/mold.v2 v2.2.0 // indirect
gopkg.in/validator.v2 v2.0.0-20180514200540-135c24b11c19 // indirect
gopkg.in/yaml.v2 v2.2.1 // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit b63be7b

Please sign in to comment.