forked from getsentry/sentry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionAdapter.java
More file actions
238 lines (206 loc) · 6.37 KB
/
SessionAdapter.java
File metadata and controls
238 lines (206 loc) · 6.37 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package io.sentry;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import io.sentry.util.Objects;
import io.sentry.util.StringUtils;
import java.io.IOException;
import java.util.Date;
import java.util.Locale;
import java.util.UUID;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ApiStatus.Internal
public final class SessionAdapter extends TypeAdapter<Session> {
private final @NotNull SentryOptions options;
public SessionAdapter(final @NotNull SentryOptions options) {
this.options = Objects.requireNonNull(options, "The SentryOptions is required.");
}
@Override
public void write(JsonWriter writer, Session value) throws IOException {
if (value == null) {
writer.nullValue();
return;
}
writer.beginObject();
if (value.getSessionId() != null) {
writer.name("sid").value(value.getSessionId().toString());
}
if (value.getDistinctId() != null) {
writer.name("did").value(value.getDistinctId());
}
if (value.getInit() != null) {
writer.name("init").value(value.getInit());
}
final Date started = value.getStarted();
if (started != null) {
writer.name("started").value(DateUtils.getTimestamp(started));
}
final Session.State status = value.getStatus();
if (status != null) {
writer.name("status").value(status.name().toLowerCase(Locale.ROOT));
}
if (value.getSequence() != null) {
writer.name("seq").value(value.getSequence());
}
int errorCount = value.errorCount();
if (errorCount > 0) {
writer.name("errors").value(errorCount);
}
if (value.getDuration() != null) {
writer.name("duration").value(value.getDuration());
}
if (value.getTimestamp() != null) {
writer.name("timestamp").value(DateUtils.getTimestamp(value.getTimestamp()));
}
boolean hasInitAttrs = false;
hasInitAttrs = initAttrs(writer, hasInitAttrs);
writer.name("release").value(value.getRelease());
if (value.getEnvironment() != null) {
hasInitAttrs = initAttrs(writer, hasInitAttrs);
writer.name("environment").value(value.getEnvironment());
}
if (value.getIpAddress() != null) {
hasInitAttrs = initAttrs(writer, hasInitAttrs);
writer.name("ip_address").value(value.getIpAddress());
}
if (value.getUserAgent() != null) {
hasInitAttrs = initAttrs(writer, hasInitAttrs);
writer.name("user_agent").value(value.getUserAgent());
}
if (hasInitAttrs) {
writer.endObject();
}
writer.endObject();
}
private boolean initAttrs(JsonWriter writer, boolean hasInitAtts) throws IOException {
if (!hasInitAtts) {
writer.name("attrs").beginObject();
}
return true;
}
@Override
public Session read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
UUID sid = null;
String did = null;
Boolean init = null;
Date started = null;
Session.State status = null;
int errors = 0;
Long seq = null;
Double duration = null;
Date timestamp = null;
String release = null;
String environment = null;
String ipAddress = null;
String userAgent = null;
reader.beginObject();
while (reader.hasNext()) {
switch (reader.nextName()) {
case "sid":
String sidStr = null;
try {
sidStr = reader.nextString();
sid = UUID.fromString(sidStr);
} catch (IllegalArgumentException e) {
options.getLogger().log(SentryLevel.ERROR, "%s sid is not valid.", sidStr);
}
break;
case "did":
did = reader.nextString();
break;
case "init":
init = reader.nextBoolean();
break;
case "started":
started = converTimeStamp(reader.nextString(), "started");
break;
case "status":
String statusStr = null;
try {
statusStr = StringUtils.capitalize(reader.nextString());
status = Session.State.valueOf(statusStr);
} catch (IllegalArgumentException e) {
options.getLogger().log(SentryLevel.ERROR, "%s status is not valid.", statusStr);
}
break;
case "errors":
errors = reader.nextInt();
break;
case "seq":
seq = reader.nextLong();
break;
case "duration":
duration = reader.nextDouble();
break;
case "timestamp":
timestamp = converTimeStamp(reader.nextString(), "timestamp");
break;
case "attrs":
{
reader.beginObject();
while (reader.hasNext()) {
switch (reader.nextName()) {
case "release":
release = reader.nextString();
break;
case "environment":
environment = reader.nextString();
break;
case "ip_address":
ipAddress = reader.nextString();
break;
case "user_agent":
userAgent = reader.nextString();
break;
default:
reader.skipValue();
break;
}
}
reader.endObject();
break;
}
default:
reader.skipValue();
break;
}
}
reader.endObject();
if (status == null || started == null || release == null || release.isEmpty()) {
options
.getLogger()
.log(SentryLevel.ERROR, "Session is gonna be dropped due to invalid fields.");
return null;
}
return new Session(
status,
started,
timestamp,
errors,
did,
sid,
init,
seq,
duration,
ipAddress,
userAgent,
environment,
release);
}
private @Nullable Date converTimeStamp(
final @NotNull String timestamp, final @NotNull String field) {
try {
return DateUtils.getDateTime(timestamp);
} catch (IllegalArgumentException e) {
options.getLogger().log(SentryLevel.ERROR, e, "Error converting session (%s) field.", field);
}
return null;
}
}