Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report usage of stripe_client #1721

Merged
merged 19 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ tasks.withType(JavaCompile) {
// com.stripe.param.AccountUpdateParams.Individual.Address] within this file.)
// We should fix this by having autogen use the fully-qualified class to eliminate ambiguity.
check("SameNameButDifferent", net.ltgt.gradle.errorprone.CheckSeverity.OFF)

// InlineMe (https://errorprone.info/docs/inlineme) seems neat, but in order to add these annotations
// we would be imposing another dependency on `errorprone` to our users, not worth it.
check("InlineMeSuggester", net.ltgt.gradle.errorprone.CheckSeverity.OFF)
}
}

Expand Down
33 changes: 31 additions & 2 deletions src/main/java/com/stripe/net/ApiRequest.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,49 @@
package com.stripe.net;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import lombok.Getter;

@Getter
public class ApiRequest extends BaseApiRequest {
private Map<String, Object> params;

private ApiRequest(
BaseAddress baseAddress,
ApiResource.RequestMethod method,
String path,
RequestOptions options,
ApiMode apiMode,
List<String> usage,
Map<String, Object> params) {
super(baseAddress, method, path, options, apiMode, usage);
this.params = params;
}

public ApiRequest(
BaseAddress baseAddress,
ApiResource.RequestMethod method,
String path,
Map<String, Object> params,
RequestOptions options,
ApiMode apiMode) {
super(baseAddress, method, path, options, apiMode);
this.params = params;
this(baseAddress, method, path, options, apiMode, null, params);
}

public ApiRequest withAddedUsage(String usage) {
List<String> newUsage = new ArrayList<>();
if (this.getUsage() != null) {
newUsage.addAll(this.getUsage());
}
newUsage.add(usage);
return new ApiRequest(
this.getBaseAddress(),
this.getMethod(),
this.getPath(),
this.getOptions(),
this.getApiMode(),
newUsage,
this.getParams());
}
}
32 changes: 14 additions & 18 deletions src/main/java/com/stripe/net/BaseApiRequest.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
package com.stripe.net;

import java.util.List;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@AllArgsConstructor(access = AccessLevel.PROTECTED)
class BaseApiRequest {
private BaseAddress baseAddress;
private ApiResource.RequestMethod method;
private String path;
private RequestOptions options;
private ApiMode apiMode;
private final BaseAddress baseAddress;
private final ApiResource.RequestMethod method;
private final String path;
private final RequestOptions options;
private final ApiMode apiMode;

@Setter private List<String> usage;
// TODO (major): Remove setter and make final
private List<String> usage;

public BaseApiRequest(
BaseAddress baseAddress,
ApiResource.RequestMethod method,
String path,
RequestOptions options,
ApiMode apiMode) {
this.baseAddress = baseAddress;
this.method = method;
this.path = path;
this.options = options;
this.apiMode = apiMode;
/** @deprecated Use {@link com.stripe.net.ApiRequest#withAddedUsage(String)} instead. */
@Deprecated
public void setUsage(List<String> usage) {
this.usage = usage;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/stripe/net/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private <T extends AbstractStripeResponse<?>> T sendWithTelemetry(

stopwatch.stop();

requestTelemetry.maybeEnqueueMetrics(response, stopwatch.getElapsed());
requestTelemetry.maybeEnqueueMetrics(response, stopwatch.getElapsed(), request.usage());

return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ private StripeRequest toStripeRequest(ApiRequest apiRequest) throws StripeExcept
apiRequest.getMethod(),
fullUrl,
apiRequest.getParams(),
RequestOptions.merge(this.options, apiRequest.getOptions()));
RequestOptions.merge(this.options, apiRequest.getOptions()),
apiRequest.getUsage());
}

@Override
Expand Down
24 changes: 22 additions & 2 deletions src/main/java/com/stripe/net/RequestTelemetry.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.google.gson.annotations.SerializedName;
import com.stripe.Stripe;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ConcurrentLinkedQueue;
import lombok.Data;
Expand Down Expand Up @@ -45,14 +47,25 @@ public Optional<String> getHeaderValue(HttpHeaders headers) {
return Optional.of(gson.toJson(payload));
}

// TODO (major) remove this overload
/**
* @deprecated use {@link #maybeEnqueueMetrics(AbstractStripeResponse, Duration, List)} instead.
*/
@Deprecated
public void maybeEnqueueMetrics(AbstractStripeResponse<?> response, Duration duration) {
maybeEnqueueMetrics(response, duration, new ArrayList<String>());
}

/**
* If telemetry is enabled and the queue is not full, then enqueue a new metrics item; otherwise,
* do nothing.
*
* @param response the Stripe response
* @param duration the request duration
* @param usage a list of tracked features used by the corresponding request
*/
public void maybeEnqueueMetrics(AbstractStripeResponse<?> response, Duration duration) {
public void maybeEnqueueMetrics(
AbstractStripeResponse<?> response, Duration duration, List<String> usage) {
if (!Stripe.enableTelemetry) {
return;
}
Expand All @@ -65,7 +78,11 @@ public void maybeEnqueueMetrics(AbstractStripeResponse<?> response, Duration dur
return;
}

RequestMetrics metrics = new RequestMetrics(response.requestId(), duration.toMillis());
if (usage != null && usage.size() == 0) {
usage = null;
}

RequestMetrics metrics = new RequestMetrics(response.requestId(), duration.toMillis(), usage);
prevRequestMetrics.add(metrics);
}

Expand All @@ -82,5 +99,8 @@ private static class RequestMetrics {

@SerializedName("request_duration_ms")
private final long requestDurationMs;

@SerializedName("usage")
private final List<String> usage;
}
}
20 changes: 18 additions & 2 deletions src/main/java/com/stripe/net/StripeRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,25 @@ public class StripeRequest {
/** The special modifiers of the request. */
RequestOptions options;

/** List of tracked behaviors associated with this request. */
List<String> usage;

/**
* Initializes a new instance of the {@link StripeRequest} class.
*
* @param method the HTTP method
* @param url the URL of the request
* @param params the parameters of the request
* @param options the special modifiers of the request
* @param usage list of tracked behaviors associated with this request
* @throws StripeException if the request cannot be initialized for any reason
*/
public StripeRequest(
ApiResource.RequestMethod method,
String url,
Map<String, Object> params,
RequestOptions options)
RequestOptions options,
List<String> usage)
throws StripeException {
try {
this.params = (params != null) ? Collections.unmodifiableMap(params) : null;
Expand All @@ -73,6 +78,7 @@ public StripeRequest(
this.url = buildURL(method, url, params);
this.content = buildContent(method, params);
this.headers = buildHeaders(method, this.options);
this.usage = usage;
} catch (IOException e) {
throw new ApiConnectionException(
String.format(
Expand All @@ -85,6 +91,15 @@ public StripeRequest(
}
}

public StripeRequest(
ApiResource.RequestMethod method,
String url,
Map<String, Object> params,
RequestOptions options)
throws StripeException {
this(method, url, params, options, Collections.emptyList());
}

/**
* Returns a new {@link StripeRequest} instance with an additional header.
*
Expand All @@ -99,7 +114,8 @@ public StripeRequest withAdditionalHeader(String name, String value) {
this.content,
this.headers.withAdditionalHeader(name, value),
this.params,
this.options);
this.options,
this.usage);
}

private static URL buildURL(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

/** Controls how the request is sent by {@link StripeResponseGetter} */
public abstract class StripeResponseGetterOptions {

// When adding setting here keep them in sync with settings in RequestOptions and
// When adding settings here keep them in sync with settings in RequestOptions and
// in the RequestOptions.merge method
public abstract String getApiKey();

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/stripe/service/AccountLinkService.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public AccountLink create(AccountLinkCreateParams params, RequestOptions options
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.withAddedUsage("stripe_client");
return getResponseGetter().request(request, AccountLink.class);
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/stripe/service/AccountService.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public Account delete(String account, RequestOptions options) throws StripeExcep
ApiRequest request =
new ApiRequest(
BaseAddress.API, ApiResource.RequestMethod.DELETE, path, null, options, ApiMode.V1);
request = request.withAddedUsage("stripe_client");
return getResponseGetter().request(request, Account.class);
}
/** Retrieves the details of an account. */
Expand All @@ -81,6 +82,7 @@ public Account retrieve(String account, AccountRetrieveParams params, RequestOpt
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.withAddedUsage("stripe_client");
return getResponseGetter().request(request, Account.class);
}
/**
Expand Down Expand Up @@ -163,6 +165,7 @@ public Account update(String account, AccountUpdateParams params, RequestOptions
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.withAddedUsage("stripe_client");
return getResponseGetter().request(request, Account.class);
}
/** Retrieves the details of an account. */
Expand All @@ -189,6 +192,7 @@ public Account retrieveCurrent(AccountRetrieveCurrentParams params, RequestOptio
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.withAddedUsage("stripe_client");
return getResponseGetter().request(request, Account.class);
}
/**
Expand Down Expand Up @@ -231,6 +235,7 @@ public StripeCollection<Account> list(AccountListParams params, RequestOptions o
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.withAddedUsage("stripe_client");
return getResponseGetter()
.request(request, new TypeToken<StripeCollection<Account>>() {}.getType());
}
Expand Down Expand Up @@ -293,6 +298,7 @@ public Account create(AccountCreateParams params, RequestOptions options) throws
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.withAddedUsage("stripe_client");
return getResponseGetter().request(request, Account.class);
}
/**
Expand Down Expand Up @@ -323,6 +329,7 @@ public Account reject(String account, AccountRejectParams params, RequestOptions
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.withAddedUsage("stripe_client");
return getResponseGetter().request(request, Account.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public AccountSession create(AccountSessionCreateParams params, RequestOptions o
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.withAddedUsage("stripe_client");
return getResponseGetter().request(request, AccountSession.class);
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/stripe/service/ApplePayDomainService.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public ApplePayDomain delete(String domain, RequestOptions options) throws Strip
ApiRequest request =
new ApiRequest(
BaseAddress.API, ApiResource.RequestMethod.DELETE, path, null, options, ApiMode.V1);
request = request.withAddedUsage("stripe_client");
return getResponseGetter().request(request, ApplePayDomain.class);
}
/** Retrieve an apple pay domain. */
Expand Down Expand Up @@ -60,6 +61,7 @@ public ApplePayDomain retrieve(
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.withAddedUsage("stripe_client");
return getResponseGetter().request(request, ApplePayDomain.class);
}
/** List apple pay domains. */
Expand Down Expand Up @@ -87,6 +89,7 @@ public StripeCollection<ApplePayDomain> list(
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.withAddedUsage("stripe_client");
return getResponseGetter()
.request(request, new TypeToken<StripeCollection<ApplePayDomain>>() {}.getType());
}
Expand All @@ -106,6 +109,7 @@ public ApplePayDomain create(ApplePayDomainCreateParams params, RequestOptions o
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.withAddedUsage("stripe_client");
return getResponseGetter().request(request, ApplePayDomain.class);
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/stripe/service/ApplicationFeeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public StripeCollection<ApplicationFee> list(
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.withAddedUsage("stripe_client");
return getResponseGetter()
.request(request, new TypeToken<StripeCollection<ApplicationFee>>() {}.getType());
}
Expand Down Expand Up @@ -99,6 +100,7 @@ public ApplicationFee retrieve(
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.withAddedUsage("stripe_client");
return getResponseGetter().request(request, ApplicationFee.class);
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/stripe/service/BalanceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public Balance retrieve(BalanceRetrieveParams params, RequestOptions options)
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.withAddedUsage("stripe_client");
return getResponseGetter().request(request, Balance.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public StripeCollection<BalanceTransaction> list(
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.withAddedUsage("stripe_client");
return getResponseGetter()
.request(request, new TypeToken<StripeCollection<BalanceTransaction>>() {}.getType());
}
Expand Down Expand Up @@ -119,6 +120,7 @@ public BalanceTransaction retrieve(
ApiRequestParams.paramsToMap(params),
options,
ApiMode.V1);
request = request.withAddedUsage("stripe_client");
return getResponseGetter().request(request, BalanceTransaction.class);
}
}
Loading
Loading