22
22
package com .microsoft .applicationinsights .channel .concrete .inprocess ;
23
23
24
24
import java .io .IOException ;
25
+ import java .io .PrintWriter ;
25
26
import java .io .StringWriter ;
26
27
import java .net .URI ;
27
28
import java .util .Map ;
28
29
import java .util .concurrent .TimeUnit ;
30
+ import java .util .concurrent .atomic .AtomicLong ;
29
31
30
32
import com .microsoft .applicationinsights .internal .channel .TelemetriesTransmitter ;
31
33
import com .microsoft .applicationinsights .channel .TelemetrySampler ;
42
44
43
45
import com .google .common .base .Strings ;
44
46
import com .google .common .base .Preconditions ;
47
+ import org .apache .commons .lang3 .exception .ExceptionUtils ;
45
48
46
49
/**
47
50
* An implementation of {@link com.microsoft.applicationinsights.channel.TelemetryChannel}
48
- *
51
+ * <p>
49
52
* The channel holds two main entities:
50
- *
53
+ * <p>
51
54
* A buffer for incoming {@link com.microsoft.applicationinsights.telemetry.Telemetry} instances
52
55
* A transmitter
53
- *
56
+ * <p>
54
57
* The buffer is stores incoming telemetry instances. Every new buffer starts a timer.
55
58
* When the timer expires, or when the buffer is 'full' (whichever happens first), the
56
59
* transmitter will pick up that buffer and will handle its sending to the server. For example,
57
60
* a transmitter will be responsible for compressing, sending and activate a policy in case of failures.
58
- *
61
+ * <p>
59
62
* The model here is:
60
- *
63
+ * <p>
61
64
* Use application threads to populate the buffer
62
65
* Use channel's threads to send buffers to the server
63
- *
66
+ * <p>
64
67
* Created by gupele on 12/17/2014.
65
68
*/
66
69
public final class InProcessTelemetryChannel implements TelemetryChannel {
@@ -90,6 +93,8 @@ public final class InProcessTelemetryChannel implements TelemetryChannel {
90
93
private TelemetryBuffer telemetryBuffer ;
91
94
private TelemetrySampler telemetrySampler ;
92
95
96
+ private static AtomicLong itemsSent = new AtomicLong (0 );
97
+
93
98
public InProcessTelemetryChannel () {
94
99
boolean developerMode = false ;
95
100
try {
@@ -99,6 +104,8 @@ public InProcessTelemetryChannel() {
99
104
}
100
105
} catch (Throwable t ) {
101
106
developerMode = false ;
107
+ InternalLogger .INSTANCE .trace ("%s generated exception in parsing," +
108
+ "stack trace is %s" , DEVELOPER_MODE_SYSTEM_PROPRETY_NAME , ExceptionUtils .getStackTrace (t ));
102
109
}
103
110
initialize (null ,
104
111
null ,
@@ -110,25 +117,27 @@ public InProcessTelemetryChannel() {
110
117
111
118
/**
112
119
* Ctor
113
- * @param endpointAddress Must be empty string or a valid uri, else an exception will be thrown
114
- * @param developerMode True will behave in a 'non-production' mode to ease the debugging
120
+ *
121
+ * @param endpointAddress Must be empty string or a valid uri, else an exception will be thrown
122
+ * @param developerMode True will behave in a 'non-production' mode to ease the debugging
115
123
* @param maxTelemetryBufferCapacity Max number of Telemetries we keep in the buffer, when reached we will send the buffer
116
- * Note, value should be between TRANSMIT_BUFFER_MIN_TIMEOUT_IN_MILLIS and TRANSMIT_BUFFER_MAX_TIMEOUT_IN_MILLIS inclusive
117
- * @param sendIntervalInMillis The maximum number of milliseconds to wait before we send the buffer
118
- * Note, value should be between MIN_MAX_TELEMETRY_BUFFER_CAPACITY and MAX_MAX_TELEMETRY_BUFFER_CAPACITY inclusive
124
+ * Note, value should be between TRANSMIT_BUFFER_MIN_TIMEOUT_IN_MILLIS and TRANSMIT_BUFFER_MAX_TIMEOUT_IN_MILLIS inclusive
125
+ * @param sendIntervalInMillis The maximum number of milliseconds to wait before we send the buffer
126
+ * Note, value should be between MIN_MAX_TELEMETRY_BUFFER_CAPACITY and MAX_MAX_TELEMETRY_BUFFER_CAPACITY inclusive
119
127
*/
120
128
public InProcessTelemetryChannel (String endpointAddress , boolean developerMode , int maxTelemetryBufferCapacity , int sendIntervalInMillis ) {
121
129
initialize (endpointAddress ,
122
- null ,
123
- developerMode ,
124
- createDefaultMaxTelemetryBufferCapacityEnforcer (maxTelemetryBufferCapacity ),
125
- createDefaultSendIntervalInSecondsEnforcer (sendIntervalInMillis ),
126
- true );
130
+ null ,
131
+ developerMode ,
132
+ createDefaultMaxTelemetryBufferCapacityEnforcer (maxTelemetryBufferCapacity ),
133
+ createDefaultSendIntervalInSecondsEnforcer (sendIntervalInMillis ),
134
+ true );
127
135
}
128
136
129
137
/**
130
138
* This Ctor will query the 'namesAndValues' map for data to initialize itself
131
139
* It will ignore data that is not of its interest, this Ctor is useful for building an instance from configuration
140
+ *
132
141
* @param namesAndValues - The data passed as name and value pairs
133
142
*/
134
143
public InProcessTelemetryChannel (Map <String , String > namesAndValues ) {
@@ -157,7 +166,7 @@ public InProcessTelemetryChannel(Map<String, String> namesAndValues) {
157
166
}
158
167
159
168
/**
160
- * Gets value indicating whether this channel is in developer mode.
169
+ * Gets value indicating whether this channel is in developer mode.
161
170
*/
162
171
@ Override
163
172
public boolean isDeveloperMode () {
@@ -166,6 +175,7 @@ public boolean isDeveloperMode() {
166
175
167
176
/**
168
177
* Sets value indicating whether this channel is in developer mode.
178
+ *
169
179
* @param developerMode True or false
170
180
*/
171
181
@ Override
@@ -179,7 +189,7 @@ public void setDeveloperMode(boolean developerMode) {
179
189
}
180
190
181
191
/**
182
- * Sends a Telemetry instance through the channel.
192
+ * Sends a Telemetry instance through the channel.
183
193
*/
184
194
@ Override
185
195
public void send (Telemetry telemetry ) {
@@ -204,8 +214,13 @@ public void send(Telemetry telemetry) {
204
214
String asJson = writer .toString ();
205
215
telemetryBuffer .add (asJson );
206
216
telemetry .reset ();
217
+ if (itemsSent .incrementAndGet () % 10000 == 0 ) {
218
+ InternalLogger .INSTANCE .info ("items sent till now %d" , itemsSent .get ());
219
+ }
220
+
207
221
} catch (IOException e ) {
208
222
InternalLogger .INSTANCE .error ("Failed to serialize Telemetry" );
223
+ InternalLogger .INSTANCE .trace ("Stack trace is %s" , ExceptionUtils .getStackTrace (e ));
209
224
return ;
210
225
}
211
226
@@ -227,6 +242,8 @@ public synchronized void stop(long timeout, TimeUnit timeUnit) {
227
242
telemetriesTransmitter .stop (timeout , timeUnit );
228
243
stopped = true ;
229
244
} catch (Throwable t ) {
245
+ InternalLogger .INSTANCE .error ("Exception generated while stopping telemetry transmitter" );
246
+ InternalLogger .INSTANCE .trace ("Stack trace generated is %s" , ExceptionUtils .getStackTrace (t ));
230
247
}
231
248
}
232
249
@@ -241,6 +258,7 @@ public void flush() {
241
258
/**
242
259
* Sets an optional Sampler that can sample out telemetries
243
260
* Currently, we don't allow to replace a valid telemtry sampler.
261
+ *
244
262
* @param telemetrySampler - The sampler
245
263
*/
246
264
@ Override
@@ -252,6 +270,7 @@ public void setSampler(TelemetrySampler telemetrySampler) {
252
270
253
271
/**
254
272
* Sets the buffer size
273
+ *
255
274
* @param maxTelemetriesInBatch should be between MIN_MAX_TELEMETRY_BUFFER_CAPACITY
256
275
* and MAX_MAX_TELEMETRY_BUFFER_CAPACITY inclusive
257
276
* if the number is lower than the minimum then the minimum will be used
@@ -263,6 +282,7 @@ public void setMaxTelemetriesInBatch(int maxTelemetriesInBatch) {
263
282
264
283
/**
265
284
* Sets the time tow wait before flushing the internal buffer
285
+ *
266
286
* @param transmitBufferTimeoutInSeconds should be between MIN_FLUSH_BUFFER_TIMEOUT_IN_SECONDS
267
287
* and MAX_FLUSH_BUFFER_TIMEOUT_IN_SECONDS inclusive
268
288
* if the number is lower than the minimum then the minimum will be used
@@ -297,6 +317,7 @@ private synchronized void initialize(String endpointAddress,
297
317
/**
298
318
* The method will throw IllegalArgumentException if the endpointAddress is not a valid uri
299
319
* Please note that a null or empty string is valid as far as the class is concerned and thus considered valid
320
+ *
300
321
* @param endpointAddress
301
322
*/
302
323
private void makeSureEndpointAddressIsValid (String endpointAddress ) {
0 commit comments