Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ec8d1fc

Browse files
authored
Remove writeKey from header, add writeKey on payload(batch) (#408)
* Remove writeKey from header, add writeKey on payload(batch), add instanceId * Update CHANGELOG with last changes
1 parent e2887fb commit ec8d1fc

File tree

8 files changed

+49
-33
lines changed

8 files changed

+49
-33
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Version 3.4.0 (Feb 04, 2023)
2+
- [Chore] Dependency upgrades
3+
- [New](https://github.com/segmentio/analytics-java/pull/408) Remove writeKey from header, add writeKey on payload(batch)
4+
- [New](https://github.com/segmentio/analytics-java/pull/406) Change exception to identify the limited exceed for the message
5+
- [New](https://github.com/segmentio/analytics-java/pull/400) Change method static to public to create a GSON instance
6+
17
# Version 3.3.1 (Aug 11, 2022)
28
- [Chore] Dependency upgrades
39
- [New](https://github.com/segmentio/analytics-java/pull/345) Add option to sentAt field to be null

analytics-core/src/main/java/com/segment/analytics/messages/Batch.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
public abstract class Batch {
1313
private static final AtomicInteger SEQUENCE_GENERATOR = new AtomicInteger();
1414

15-
public static Batch create(Map<String, ?> context, List<Message> batch) {
15+
public static Batch create(Map<String, ?> context, List<Message> batch, String writeKey) {
1616
Message sentAtNull =
1717
batch.stream().filter(message -> message.sentAt() != null).findAny().orElse(null);
1818

1919
return new AutoValue_Batch(
2020
batch,
2121
sentAtNull == null ? new Date() : sentAtNull.sentAt(),
2222
context,
23-
SEQUENCE_GENERATOR.incrementAndGet());
23+
SEQUENCE_GENERATOR.incrementAndGet(),
24+
writeKey);
2425
}
2526

2627
public abstract List<Message> batch();
@@ -30,4 +31,6 @@ public static Batch create(Map<String, ?> context, List<Message> batch) {
3031
public abstract Map<String, ?> context();
3132

3233
public abstract int sequence();
34+
35+
public abstract String writeKey();
3336
}

analytics-core/src/test/java/com/segment/analytics/messages/BatchTest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
@RunWith(BurstJUnit4.class)
1818
public class BatchTest {
1919

20+
private static final String writeKey = "writeKey";
21+
2022
@Test
2123
public void create(TestUtils.MessageBuilderFactory factory) {
2224
Message message = factory.get().userId("userId").build();
2325
Map<String, String> context = ImmutableMap.of("foo", "bar");
2426
List<Message> messages = ImmutableList.of(message);
2527

26-
Batch batch = Batch.create(context, messages);
28+
Batch batch = Batch.create(context, messages, writeKey);
2729

2830
assertThat(batch.batch()).isEqualTo(messages);
2931
assertThat(batch.context()).isEqualTo(context);
@@ -40,7 +42,7 @@ public void createWithSentAt(TestUtils.MessageBuilderFactory factory) throws Par
4042
Map<String, String> context = ImmutableMap.of("foo", "bar");
4143
List<Message> messages = ImmutableList.of(message);
4244

43-
Batch batch = Batch.create(context, messages);
45+
Batch batch = Batch.create(context, messages, writeKey);
4446

4547
assertThat(batch.sentAt()).isEqualTo(messages.get(0).sentAt());
4648
}
@@ -51,7 +53,7 @@ public void createWithSentAtNull(TestUtils.MessageBuilderFactory factory) throws
5153
Map<String, String> context = ImmutableMap.of("foo", "bar");
5254
List<Message> messages = ImmutableList.of(message);
5355

54-
Batch batch = Batch.create(context, messages);
56+
Batch batch = Batch.create(context, messages, writeKey);
5557

5658
assertThat(batch.sentAt()).isEqualToIgnoringHours(new Date());
5759
}
@@ -62,8 +64,8 @@ public void sequence(TestUtils.MessageBuilderFactory factory) {
6264
Map<String, String> context = ImmutableMap.of("foo", "bar");
6365
List<Message> messages = ImmutableList.of(message);
6466

65-
Batch first = Batch.create(context, messages);
66-
Batch second = Batch.create(context, messages);
67+
Batch first = Batch.create(context, messages, writeKey);
68+
Batch second = Batch.create(context, messages, writeKey);
6769

6870
assertThat(first.sequence() + 1).isEqualTo(second.sequence());
6971
}

analytics/src/main/java/com/segment/analytics/Analytics.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ public void log(String message) {
413413
OkHttpClient.Builder builder =
414414
client
415415
.newBuilder()
416-
.addInterceptor(new AnalyticsRequestInterceptor(writeKey, userAgent))
416+
.addInterceptor(new AnalyticsRequestInterceptor(userAgent))
417417
.addInterceptor(interceptor);
418418

419419
if (forceTlsV1) {
@@ -449,7 +449,8 @@ public void log(String message) {
449449
log,
450450
threadFactory,
451451
networkExecutor,
452-
callbacks);
452+
callbacks,
453+
writeKey);
453454

454455
return new Analytics(analyticsClient, messageTransformers, messageInterceptors, log);
455456
}

analytics/src/main/java/com/segment/analytics/AnalyticsRequestInterceptor.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,22 @@
22

33
import java.io.IOException;
44
import javax.annotation.Nonnull;
5-
import okhttp3.Credentials;
65
import okhttp3.Interceptor;
76
import okhttp3.Request;
87

98
class AnalyticsRequestInterceptor implements Interceptor {
10-
private static final String AUTHORIZATION_HEADER = "Authorization";
119
private static final String USER_AGENT_HEADER = "User-Agent";
1210

13-
private final @Nonnull String writeKey;
1411
private final @Nonnull String userAgent;
1512

16-
AnalyticsRequestInterceptor(@Nonnull String writeKey, @Nonnull String userAgent) {
17-
this.writeKey = writeKey;
13+
AnalyticsRequestInterceptor(@Nonnull String userAgent) {
1814
this.userAgent = userAgent;
1915
}
2016

2117
@Override
2218
public okhttp3.Response intercept(Chain chain) throws IOException {
2319
Request request = chain.request();
24-
Request newRequest =
25-
request
26-
.newBuilder()
27-
.addHeader(AUTHORIZATION_HEADER, Credentials.basic(writeKey, ""))
28-
.addHeader(USER_AGENT_HEADER, userAgent)
29-
.build();
20+
Request newRequest = request.newBuilder().addHeader(USER_AGENT_HEADER, userAgent).build();
3021

3122
return chain.proceed(newRequest);
3223
}

analytics/src/main/java/com/segment/analytics/internal/AnalyticsClient.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.LinkedList;
2222
import java.util.List;
2323
import java.util.Map;
24+
import java.util.UUID;
2425
import java.util.concurrent.BlockingQueue;
2526
import java.util.concurrent.ExecutorService;
2627
import java.util.concurrent.Executors;
@@ -40,13 +41,15 @@ public class AnalyticsClient {
4041
private static final int MSG_MAX_SIZE = 1024 * 32;
4142
private static final Charset ENCODING = StandardCharsets.UTF_8;
4243
private static Gson gsonInstance;
44+
private static final String instanceId = UUID.randomUUID().toString();
4345

4446
static {
4547
Map<String, String> library = new LinkedHashMap<>();
4648
library.put("name", "analytics-java");
4749
library.put("version", AnalyticsVersion.get());
4850
Map<String, Object> context = new LinkedHashMap<>();
4951
context.put("library", Collections.unmodifiableMap(library));
52+
context.put("instanceId", instanceId);
5053
CONTEXT = Collections.unmodifiableMap(context);
5154
}
5255

@@ -63,6 +66,7 @@ public class AnalyticsClient {
6366
private final ExecutorService looperExecutor;
6467
private final ScheduledExecutorService flushScheduler;
6568
private final AtomicBoolean isShutDown;
69+
private final String writeKey;
6670

6771
public static AnalyticsClient create(
6872
HttpUrl uploadUrl,
@@ -75,7 +79,8 @@ public static AnalyticsClient create(
7579
Log log,
7680
ThreadFactory threadFactory,
7781
ExecutorService networkExecutor,
78-
List<Callback> callbacks) {
82+
List<Callback> callbacks,
83+
String writeKey) {
7984
return new AnalyticsClient(
8085
new LinkedBlockingQueue<Message>(queueCapacity),
8186
uploadUrl,
@@ -88,7 +93,8 @@ public static AnalyticsClient create(
8893
threadFactory,
8994
networkExecutor,
9095
callbacks,
91-
new AtomicBoolean(false));
96+
new AtomicBoolean(false),
97+
writeKey);
9298
}
9399

94100
public AnalyticsClient(
@@ -103,7 +109,8 @@ public AnalyticsClient(
103109
ThreadFactory threadFactory,
104110
ExecutorService networkExecutor,
105111
List<Callback> callbacks,
106-
AtomicBoolean isShutDown) {
112+
AtomicBoolean isShutDown,
113+
String writeKey) {
107114
this.messageQueue = messageQueue;
108115
this.uploadUrl = uploadUrl;
109116
this.service = service;
@@ -115,6 +122,7 @@ public AnalyticsClient(
115122
this.looperExecutor = Executors.newSingleThreadExecutor(threadFactory);
116123
this.networkExecutor = networkExecutor;
117124
this.isShutDown = isShutDown;
125+
this.writeKey = writeKey;
118126

119127
this.currentQueueSizeInBytes = 0;
120128

@@ -296,7 +304,7 @@ public void run() {
296304
Boolean isOverflow = messages.size() >= size;
297305

298306
if (!messages.isEmpty() && (isOverflow || isBlockingSignal || batchSizeLimitReached)) {
299-
Batch batch = Batch.create(CONTEXT, new ArrayList<>(messages));
307+
Batch batch = Batch.create(CONTEXT, new ArrayList<>(messages), writeKey);
300308
log.print(
301309
VERBOSE,
302310
"Batching %s message(s) into batch %s.",

analytics/src/test/java/com/segment/analytics/AnalyticsRequestInterceptorTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@ public class AnalyticsRequestInterceptorTest {
1616

1717
@Test
1818
public void testInterceptor() throws IOException {
19-
AnalyticsRequestInterceptor interceptor =
20-
new AnalyticsRequestInterceptor("writeKey", "userAgent");
19+
AnalyticsRequestInterceptor interceptor = new AnalyticsRequestInterceptor("userAgent");
2120

2221
final Request request = new Request.Builder().url("https://api.segment.io").get().build();
2322

2423
Chain chain =
2524
new ChainAdapter(request, mockConnection) {
2625
@Override
2726
public Response proceed(Request request) throws IOException {
28-
assertThat(request.header("Authorization"), Is.is("Basic d3JpdGVLZXk6"));
2927
assertThat(request.header("User-Agent"), Is.is("userAgent"));
3028
return null;
3129
}

analytics/src/test/java/com/segment/analytics/internal/AnalyticsClientTest.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public class AnalyticsClientTest {
6868
MAX_MSG_SIZE
6969
- 200; // Once we create msg object with this size it barely below 32 threshold so good
7070
// for tests
71+
private static String writeKey = "writeKey";
7172

7273
Log log = Log.NONE;
7374

@@ -103,7 +104,8 @@ AnalyticsClient newClient() {
103104
threadFactory,
104105
networkExecutor,
105106
Collections.singletonList(callback),
106-
isShutDown);
107+
isShutDown,
108+
writeKey);
107109
}
108110

109111
@Test
@@ -290,7 +292,8 @@ public void flushHowManyTimesNecessaryToStayWithinLimit() throws InterruptedExce
290292
threadFactory,
291293
networkExecutor,
292294
Collections.singletonList(callback),
293-
isShutDown);
295+
isShutDown,
296+
writeKey);
294297

295298
Map<String, String> properties = new HashMap<String, String>();
296299

@@ -357,7 +360,8 @@ public void enqueueBeforeMaxDoesNotTriggerFlush() {
357360
}
358361

359362
static Batch batchFor(Message message) {
360-
return Batch.create(Collections.<String, Object>emptyMap(), Collections.singletonList(message));
363+
return Batch.create(
364+
Collections.<String, Object>emptyMap(), Collections.singletonList(message), writeKey);
361365
}
362366

363367
@Test
@@ -863,7 +867,8 @@ public void submitBatchBelowThreshold() throws InterruptedException, IllegalArgu
863867
threadFactory,
864868
networkExecutor,
865869
Collections.singletonList(callback),
866-
isShutDown);
870+
isShutDown,
871+
writeKey);
867872

868873
Map<String, String> properties = new HashMap<String, String>();
869874
properties.put("property3", generateDataOfSizeSpecialChars(MAX_MSG_SIZE, true));
@@ -903,7 +908,8 @@ public void submitBatchAboveThreshold() throws InterruptedException, IllegalArgu
903908
threadFactory,
904909
networkExecutor,
905910
Collections.singletonList(callback),
906-
isShutDown);
911+
isShutDown,
912+
writeKey);
907913

908914
Map<String, String> properties = new HashMap<String, String>();
909915
properties.put("property3", generateDataOfSizeSpecialChars(MAX_MSG_SIZE, true));
@@ -942,7 +948,8 @@ public void submitManySmallMessagesBatchAboveThreshold() throws InterruptedExcep
942948
threadFactory,
943949
networkExecutor,
944950
Collections.singletonList(callback),
945-
isShutDown);
951+
isShutDown,
952+
writeKey);
946953

947954
Map<String, String> properties = new HashMap<String, String>();
948955
properties.put("property3", generateDataOfSizeSpecialChars(1024 * 8, true));

0 commit comments

Comments
 (0)