forked from stripe/stripe-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestTelemetry.java
More file actions
102 lines (83 loc) · 2.98 KB
/
Copy pathRequestTelemetry.java
File metadata and controls
102 lines (83 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.stripe.net;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.stripe.Stripe;
import java.time.Duration;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ConcurrentLinkedQueue;
import lombok.Data;
/** Helper class used by {@link LiveStripeResponseGetter} to manage request telemetry. */
class RequestTelemetry {
/** The name of the header used to send request telemetry in requests. */
public static final String HEADER_NAME = "X-Stripe-Client-Telemetry";
private static final int MAX_REQUEST_METRICS_QUEUE_SIZE = 100;
private static final Gson gson = new Gson();
private static ConcurrentLinkedQueue<RequestMetrics> prevRequestMetrics =
new ConcurrentLinkedQueue<RequestMetrics>();
/**
* Returns an {@link Optional} containing the value of the {@code X-Stripe-Telemetry} header to
* add to the request. If the header is already present in the request, or if there is available
* metrics, or if telemetry is disabled, then the returned {@code Optional} is empty.
*
* @param headers the request headers
* @deprecated Use {$ling {@link #pollPayload()} instead.
*/
@Deprecated
public Optional<String> getHeaderValue(HttpHeaders headers) {
if (headers.firstValue(HEADER_NAME).isPresent()) {
return Optional.empty();
}
return this.pollPayload();
}
public Optional<String> pollPayload() {
RequestMetrics requestMetrics = prevRequestMetrics.poll();
if (requestMetrics == null) {
return Optional.empty();
}
if (!Stripe.enableTelemetry) {
return Optional.empty();
}
ClientTelemetryPayload payload = new ClientTelemetryPayload(requestMetrics);
return Optional.of(gson.toJson(payload));
}
/**
* 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, List<String> usage) {
if (!Stripe.enableTelemetry) {
return;
}
if (response.requestId() == null) {
return;
}
if (prevRequestMetrics.size() >= MAX_REQUEST_METRICS_QUEUE_SIZE) {
return;
}
if (usage != null && usage.isEmpty()) {
usage = null;
}
RequestMetrics metrics = new RequestMetrics(response.requestId(), duration.toMillis(), usage);
prevRequestMetrics.add(metrics);
}
@Data
private static class ClientTelemetryPayload {
@SerializedName("last_request_metrics")
private final RequestMetrics lastRequestMetrics;
}
@Data
private static class RequestMetrics {
@SerializedName("request_id")
private final String requestId;
@SerializedName("request_duration_ms")
private final long requestDurationMs;
@SerializedName("usage")
private final List<String> usage;
}
}