Skip to content

Commit

Permalink
Add request telemetry for go sdk (#133)
Browse files Browse the repository at this point in the history
* adding telemetry for requests

* add test for request metrics queue

* tidied test

* give option to enable telemetry in invoke function as well

* add integration test for telemetry

* seperate sending http request into seperate function

* add readme for telemetry

* fixed formatting in readme

* fixed whitespace
  • Loading branch information
sanchayata-jain-cko authored Nov 4, 2024
1 parent 654c8e4 commit f669aba
Show file tree
Hide file tree
Showing 37 changed files with 567 additions and 159 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,20 @@ The execution of integration tests require the following environment variables s
* For default account systems (OAuth): `CHECKOUT_DEFAULT_OAUTH_CLIENT_ID` & `CHECKOUT_DEFAULT_OAUTH_CLIENT_SECRET`
* For Previous account systems (ABC): `CHECKOUT_PREVIOUS_PUBLIC_KEY` & `CHECKOUT_PREVIOUS_SECRET_KEY`

## Telemetry
Request telementry is enabled by default in the Go SDK. Request latency is included in the telemetry data. Recording the request latency allows Checkout.com to continuously monitor and imporove the merchant experience.

Request telemetry can be disabled by opting out during checkout_sdk_builder builder step:

```
api := checkout.Builder().
Previous().
WithSecretKey("CHECKOUT_PREVIOUS_SECRET_KEY").
WithEnvironment(configuration.Sandbox()).
WithEnableTelemetry(false).
Build()
```

## Code of Conduct

Please refer to [Code of Conduct](CODE_OF_CONDUCT.md)
Expand Down
7 changes: 6 additions & 1 deletion abc/checkout_previous_sdk_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ type CheckoutPreviousSdkBuilder struct {
configuration.StaticKeysBuilder
}

func (b *CheckoutPreviousSdkBuilder) WithEnableTelemetry(telemetry bool) *CheckoutPreviousSdkBuilder {
b.EnableTelemetry = &telemetry
return b
}

func (b *CheckoutPreviousSdkBuilder) WithEnvironment(environment configuration.Environment) *CheckoutPreviousSdkBuilder {
b.Environment = environment
return b
Expand Down Expand Up @@ -53,7 +58,7 @@ func (b *CheckoutPreviousSdkBuilder) Build() (*Api, error) {

sdkCredentials := configuration.NewPreviousKeysSdkCredentials(b.SecretKey, b.PublicKey)

newConfiguration := configuration.NewConfiguration(sdkCredentials, b.Environment, b.HttpClient, b.Logger)
newConfiguration := configuration.NewConfiguration(sdkCredentials, b.EnableTelemetry, b.Environment, b.HttpClient, b.Logger)

if b.EnvironmentSubdomain != nil {
newConfiguration = configuration.NewConfigurationWithSubdomain(sdkCredentials, b.Environment, b.EnvironmentSubdomain, b.HttpClient, b.Logger)
Expand Down
30 changes: 20 additions & 10 deletions accounts/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,12 @@ func TestCreateEntity(t *testing.T) {
filesClient := new(mocks.ApiClientMock)
credentials := new(mocks.CredentialsMock)
environment := new(mocks.EnvironmentMock)
enableTelemertry := true

tc.getAuthorization(&credentials.Mock)
tc.apiPost(&apiClient.Mock)

configuration := configuration.NewConfiguration(credentials, environment, &http.Client{}, nil)
configuration := configuration.NewConfiguration(credentials, &enableTelemertry, environment, &http.Client{}, nil)
client := NewClient(configuration, apiClient, filesClient)

tc.checker(client.CreateEntity(tc.request))
Expand Down Expand Up @@ -249,11 +250,12 @@ func TestGetEntity(t *testing.T) {
filesClient := new(mocks.ApiClientMock)
credentials := new(mocks.CredentialsMock)
environment := new(mocks.EnvironmentMock)
enableTelemertry := true

tc.getAuthorization(&credentials.Mock)
tc.apiGet(&apiClient.Mock)

configuration := configuration.NewConfiguration(credentials, environment, &http.Client{}, nil)
configuration := configuration.NewConfiguration(credentials, &enableTelemertry, environment, &http.Client{}, nil)
client := NewClient(configuration, apiClient, filesClient)

tc.checker(client.GetEntity(tc.entityId))
Expand Down Expand Up @@ -369,11 +371,12 @@ func TestUpdateEntity(t *testing.T) {
filesClient := new(mocks.ApiClientMock)
credentials := new(mocks.CredentialsMock)
environment := new(mocks.EnvironmentMock)
enableTelemertry := true

tc.getAuthorization(&credentials.Mock)
tc.apiPut(&apiClient.Mock)

configuration := configuration.NewConfiguration(credentials, environment, &http.Client{}, nil)
configuration := configuration.NewConfiguration(credentials, &enableTelemertry, environment, &http.Client{}, nil)
client := NewClient(configuration, apiClient, filesClient)

tc.checker(client.UpdateEntity(tc.entityId, tc.request))
Expand Down Expand Up @@ -527,11 +530,12 @@ func TestCreatePaymentInstruments(t *testing.T) {
filesClient := new(mocks.ApiClientMock)
credentials := new(mocks.CredentialsMock)
environment := new(mocks.EnvironmentMock)
enableTelemertry := true

tc.getAuthorization(&credentials.Mock)
tc.apiPost(&apiClient.Mock)

configuration := configuration.NewConfiguration(credentials, environment, &http.Client{}, nil)
configuration := configuration.NewConfiguration(credentials, &enableTelemertry, environment, &http.Client{}, nil)
client := NewClient(configuration, apiClient, filesClient)

tc.checker(client.CreatePaymentInstruments(tc.entityId, tc.paymentInstrument))
Expand Down Expand Up @@ -675,11 +679,12 @@ func TestCreatePaymentInstrument(t *testing.T) {
filesClient := new(mocks.ApiClientMock)
credentials := new(mocks.CredentialsMock)
environment := new(mocks.EnvironmentMock)
enableTelemertry := true

tc.getAuthorization(&credentials.Mock)
tc.apiPost(&apiClient.Mock)

configuration := configuration.NewConfiguration(credentials, environment, &http.Client{}, nil)
configuration := configuration.NewConfiguration(credentials, &enableTelemertry, environment, &http.Client{}, nil)
client := NewClient(configuration, apiClient, filesClient)

tc.checker(client.CreatePaymentInstrument(tc.entityId, tc.paymentInstrumentRequest))
Expand Down Expand Up @@ -824,11 +829,12 @@ func TestQueryPaymentInstruments(t *testing.T) {
filesClient := new(mocks.ApiClientMock)
credentials := new(mocks.CredentialsMock)
environment := new(mocks.EnvironmentMock)
enableTelemertry := true

tc.getAuthorization(&credentials.Mock)
tc.apiGet(&apiClient.Mock)

configuration := configuration.NewConfiguration(credentials, environment, &http.Client{}, nil)
configuration := configuration.NewConfiguration(credentials, &enableTelemertry, environment, &http.Client{}, nil)
client := NewClient(configuration, apiClient, filesClient)

tc.checker(client.QueryPaymentInstruments(tc.entityId, tc.query))
Expand Down Expand Up @@ -973,11 +979,12 @@ func TestRetrievePaymentInstrumentDetails(t *testing.T) {
filesClient := new(mocks.ApiClientMock)
credentials := new(mocks.CredentialsMock)
environment := new(mocks.EnvironmentMock)
enableTelemertry := true

tc.getAuthorization(&credentials.Mock)
tc.apiGet(&apiClient.Mock)

configuration := configuration.NewConfiguration(credentials, environment, &http.Client{}, nil)
configuration := configuration.NewConfiguration(credentials, &enableTelemertry, environment, &http.Client{}, nil)
client := NewClient(configuration, apiClient, filesClient)

tc.checker(client.RetrievePaymentInstrumentDetails(tc.entityId, tc.paymentInstrumentId))
Expand Down Expand Up @@ -1096,11 +1103,12 @@ func TestUpdatePaymentInstrumentDetails(t *testing.T) {
filesClient := new(mocks.ApiClientMock)
credentials := new(mocks.CredentialsMock)
environment := new(mocks.EnvironmentMock)
enableTelemertry := true

tc.getAuthorization(&credentials.Mock)
tc.apiPut(&apiClient.Mock)

configuration := configuration.NewConfiguration(credentials, environment, &http.Client{}, nil)
configuration := configuration.NewConfiguration(credentials, &enableTelemertry, environment, &http.Client{}, nil)
client := NewClient(configuration, apiClient, filesClient)

tc.checker(client.UpdatePaymentInstrumentDetails(tc.entityId, tc.instrumentId, tc.request))
Expand Down Expand Up @@ -1189,11 +1197,12 @@ func TestGetPayoutSchedule(t *testing.T) {
filesClient := new(mocks.ApiClientMock)
credentials := new(mocks.CredentialsMock)
environment := new(mocks.EnvironmentMock)
enableTelemertry := true

tc.getAuthorization(&credentials.Mock)
tc.apiGet(&apiClient.Mock)

configuration := configuration.NewConfiguration(credentials, environment, &http.Client{}, nil)
configuration := configuration.NewConfiguration(credentials, &enableTelemertry, environment, &http.Client{}, nil)
client := NewClient(configuration, apiClient, filesClient)

tc.checker(client.RetrievePayoutSchedule(tc.entityId))
Expand Down Expand Up @@ -1283,11 +1292,12 @@ func TestUpdatePayoutSchedule(t *testing.T) {
filesClient := new(mocks.ApiClientMock)
credentials := new(mocks.CredentialsMock)
environment := new(mocks.EnvironmentMock)
enableTelemertry := true

tc.getAuthorization(&credentials.Mock)
tc.apiPut(&apiClient.Mock)

configuration := configuration.NewConfiguration(credentials, environment, &http.Client{}, nil)
configuration := configuration.NewConfiguration(credentials, &enableTelemertry, environment, &http.Client{}, nil)
client := NewClient(configuration, apiClient, filesClient)

tc.checker(client.UpdatePayoutSchedule(tc.entityId, tc.currency, tc.request))
Expand Down
3 changes: 2 additions & 1 deletion apm/ideal/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ func TestGetInfo(t *testing.T) {
apiClient := new(mocks.ApiClientMock)
credentials := new(mocks.CredentialsMock)
environment := new(mocks.EnvironmentMock)
enableTelemertry := true

tc.getAuthorization(&credentials.Mock)
tc.apiGet(&apiClient.Mock)

configuration := configuration.NewConfiguration(credentials, environment, &http.Client{}, nil)
configuration := configuration.NewConfiguration(credentials, &enableTelemertry, environment, &http.Client{}, nil)
client := NewClient(configuration, apiClient)

tc.checker(client.GetInfo())
Expand Down
12 changes: 8 additions & 4 deletions apm/klarna/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@ func TestCreateSession(t *testing.T) {
apiClient := new(mocks.ApiClientMock)
credentials := new(mocks.CredentialsMock)
environment := new(mocks.EnvironmentMock)
enableTelemertry := true

tc.getAuthorization(&credentials.Mock)
tc.apiPost(&apiClient.Mock)

configuration := configuration.NewConfiguration(credentials, environment, &http.Client{}, nil)
configuration := configuration.NewConfiguration(credentials, &enableTelemertry, environment, &http.Client{}, nil)
client := NewClient(configuration, apiClient)

tc.checker(client.CreateCreditSession(tc.request))
Expand Down Expand Up @@ -197,11 +198,12 @@ func TestGetCreditSession(t *testing.T) {
apiClient := new(mocks.ApiClientMock)
credentials := new(mocks.CredentialsMock)
environment := new(mocks.EnvironmentMock)
enableTelemertry := true

tc.getAuthorization(&credentials.Mock)
tc.apiGet(&apiClient.Mock)

configuration := configuration.NewConfiguration(credentials, environment, &http.Client{}, nil)
configuration := configuration.NewConfiguration(credentials, &enableTelemertry, environment, &http.Client{}, nil)
client := NewClient(configuration, apiClient)

tc.checker(client.GetCreditSession(tc.sessionId))
Expand Down Expand Up @@ -291,11 +293,12 @@ func TestCapturePayment(t *testing.T) {
apiClient := new(mocks.ApiClientMock)
credentials := new(mocks.CredentialsMock)
environment := new(mocks.EnvironmentMock)
enableTelemertry := true

tc.getAuthorization(&credentials.Mock)
tc.apiPost(&apiClient.Mock)

configuration := configuration.NewConfiguration(credentials, environment, &http.Client{}, nil)
configuration := configuration.NewConfiguration(credentials, &enableTelemertry, environment, &http.Client{}, nil)
client := NewClient(configuration, apiClient)

tc.checker(client.CapturePayment(tc.paymentId, tc.request))
Expand Down Expand Up @@ -382,11 +385,12 @@ func TestVoidPayment(t *testing.T) {
apiClient := new(mocks.ApiClientMock)
credentials := new(mocks.CredentialsMock)
environment := new(mocks.EnvironmentMock)
enableTelemertry := true

tc.getAuthorization(&credentials.Mock)
tc.apiPost(&apiClient.Mock)

configuration := configuration.NewConfiguration(credentials, environment, &http.Client{}, nil)
configuration := configuration.NewConfiguration(credentials, &enableTelemertry, environment, &http.Client{}, nil)
client := NewClient(configuration, apiClient)

tc.checker(client.VoidPayment(tc.paymentId, tc.request))
Expand Down
6 changes: 4 additions & 2 deletions apm/sepa/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ func TestGetMandate(t *testing.T) {
apiClient := new(mocks.ApiClientMock)
credentials := new(mocks.CredentialsMock)
environment := new(mocks.EnvironmentMock)
enableTelemertry := true

tc.getAuthorization(&credentials.Mock)
tc.apiGet(&apiClient.Mock)

configuration := configuration.NewConfiguration(credentials, environment, &http.Client{}, nil)
configuration := configuration.NewConfiguration(credentials, &enableTelemertry, environment, &http.Client{}, nil)
client := NewClient(configuration, apiClient)

tc.checker(client.GetMandate(tc.mandateId))
Expand Down Expand Up @@ -147,11 +148,12 @@ func TestCancelMandate(t *testing.T) {
apiClient := new(mocks.ApiClientMock)
credentials := new(mocks.CredentialsMock)
environment := new(mocks.EnvironmentMock)
enableTelemertry := true

tc.getAuthorization(&credentials.Mock)
tc.apiPost(&apiClient.Mock)

configuration := configuration.NewConfiguration(credentials, environment, &http.Client{}, nil)
configuration := configuration.NewConfiguration(credentials, &enableTelemertry, environment, &http.Client{}, nil)
client := NewClient(configuration, apiClient)

tc.checker(client.CancelMandate(tc.mandateId))
Expand Down
3 changes: 2 additions & 1 deletion balances/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ func TestRetrieveEntityBalances(t *testing.T) {
apiClient := new(mocks.ApiClientMock)
credentials := new(mocks.CredentialsMock)
environment := new(mocks.EnvironmentMock)
enableTelemertry := true

tc.getAuthorization(&credentials.Mock)
tc.apiGet(&apiClient.Mock)

configuration := configuration.NewConfiguration(credentials, environment, &http.Client{}, nil)
configuration := configuration.NewConfiguration(credentials, &enableTelemertry, environment, &http.Client{}, nil)
client := NewClient(configuration, apiClient)

tc.checker(client.RetrieveEntityBalances(tc.entityId, tc.query))
Expand Down
Loading

0 comments on commit f669aba

Please sign in to comment.