forked from dasein-cloud/dasein-cloud-core
/
APITrace.java
637 lines (550 loc) · 25 KB
/
APITrace.java
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/**
* Copyright (C) 2009-2016 Dell, Inc.
* See annotations for authorship information
*
* ====================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.dasein.cloud.util;
import org.apache.log4j.Logger;
import org.dasein.cloud.CloudProvider;
import org.dasein.cloud.ProviderContext;
import org.dasein.cloud.RequestTrackingStrategy;
import org.json.JSONObject;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;
/**
* A tool for tracing the load your Dasein Cloud usage is placing on a cloud provider. This class is used by
* {@link API} to provide JMX integration. In order for any API tracing to be functional, you must set the
* log level for org.dasein.cloud.util.APITrace to TRACE, DEBUG, or INFO depending on the information you are seeking.
* To turn it off, set the level to WARN or higher.
* <p>Created by George Reese: 11/16/12 7:20 PM</p>
* @author George Reese
* @version 2013.01 initial version (Issue #1)
* @since 2013.01
*/
public class APITrace {
static private final Logger logger = Logger.getLogger(APITrace.class);
static public final String DELIMITER = ".";
static public final String DELIMITER_REGEX = "\\.";
static private class CloudOperation {
public String name;
public long startTimestamp = System.currentTimeMillis();
public long endTimestamp = 0L;
public int calls = 0;
public CloudOperation currentChild;
public ArrayList<CloudOperation> priorChildren;
public ArrayList<String> apiCalls;
public CloudOperation(@Nonnull String name) { this.name = name; }
}
static private final HashMap<String,Long> apiCount = new HashMap<String, Long>();
static private final HashMap<String,Long> operationApis = new HashMap<String, Long>();
static private final HashMap<String,Long> operationCount = new HashMap<String, Long>();
static private final HashMap<String,CloudOperation> operationTrace = new HashMap<String, CloudOperation>();
static private HashMap<Long,CloudOperation> operations = new HashMap<Long, CloudOperation>();
static {
try {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName("org.dasein:type=API");
API api = new API();
server.registerMBean(api, name);
}
catch( Throwable t ) {
logger.error("Unable to set up API MBean: " + t.getMessage());
}
}
static public void begin(@Nonnull CloudProvider provider, @Nonnull String operationName) {
if( logger.isDebugEnabled() ) {
try {
ProviderContext ctx = provider.getContext();
String accountNumber = getAccountNumber( ctx );
RequestTrackingStrategy strategy = ctx.getRequestTrackingStrategy();
String requestTracking = "";
if(strategy != null && strategy.getInAPITrace()){
requestTracking = strategy.getRequestId() + DELIMITER;
}
operationName = provider.getProviderName().replaceAll(DELIMITER_REGEX, "_") + DELIMITER + provider.getCloudName().replaceAll(DELIMITER_REGEX, "_") + DELIMITER + accountNumber.replaceAll(DELIMITER_REGEX, "_") + DELIMITER + requestTracking + operationName;
long thread = Thread.currentThread().getId();
CloudOperation operation = new CloudOperation(operationName);
CloudOperation current = operations.get(thread);
if( current == null ) {
operations.put(thread, operation);
}
else {
while( current.currentChild != null ) {
current = current.currentChild;
}
current.currentChild = operation;
}
synchronized( operationCount ) {
if( operationCount.containsKey(operationName) ) {
operationCount.put(operationName, operationCount.get(operationName) + 1);
}
else {
operationCount.put(operationName, 1L);
}
}
}
catch( Throwable t ) {
logger.warn("Error with API trace begin: " + t.getMessage());
}
}
}
static private long count(CloudOperation operation) {
long count = operation.calls;
if( operation.priorChildren != null ) {
for( CloudOperation o : operation.priorChildren ) {
count += count(o);
}
}
return count;
}
static public void end() {
if( logger.isDebugEnabled() ) {
try {
long thread = Thread.currentThread().getId();
CloudOperation current = operations.get(thread);
if( current == null ) {
return;
}
CloudOperation parent = null;
while( current.currentChild != null ) {
parent = current;
current = current.currentChild;
}
current.endTimestamp = System.currentTimeMillis();
if( parent != null ) {
if( parent.priorChildren == null ) {
parent.priorChildren = new ArrayList<CloudOperation>();
}
parent.priorChildren.add(current);
parent.currentChild = null;
}
else {
operations.remove(thread);
}
log(current);
}
catch( Throwable t ) {
logger.warn("Error with API trace end: " + t.getMessage());
}
}
}
static public long getAPICount() {
long count = 0L;
synchronized( apiCount ) {
for( Map.Entry<String,Long> api : apiCount.entrySet() ) {
count += api.getValue();
}
}
return count;
}
static public long getAPICount(@Nonnull String providerName) {
return getAPICountForPrefix(providerName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER);
}
static public long getAPICount(@Nonnull String providerName, @Nonnull String cloudName) {
return getAPICountForPrefix(providerName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER + cloudName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER);
}
static public long getAPICount(@Nonnull String providerName, @Nonnull String cloudName, @Nonnull String accountNumber) {
return getAPICountForPrefix(providerName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER + cloudName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER + accountNumber.replaceAll(DELIMITER_REGEX, "_") + DELIMITER);
}
static public long getAPICount(@Nonnull String providerName, @Nonnull String cloudName, @Nonnull String accountNumber, @Nonnull String apiCall) {
return getAPICountForPrefix(providerName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER + cloudName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER + accountNumber.replaceAll(DELIMITER_REGEX, "_") + DELIMITER + apiCall);
}
static public long getAPICountAcrossAccounts(@Nonnull String providerName, @Nonnull String cloudName, @Nonnull String apiCall) {
String prefix = providerName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER + cloudName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER;
long count = 0L;
synchronized( apiCount ) {
for( Map.Entry<String,Long> api : apiCount.entrySet() ) {
if( api.getKey().startsWith(prefix) && api.getKey().endsWith(apiCall)) {
count += api.getValue();
}
}
}
return count;
}
static private long getAPICountForPrefix(@Nonnull String prefix) {
long count = 0L;
synchronized( apiCount ) {
for( Map.Entry<String,Long> api : apiCount.entrySet() ) {
if( api.getKey().startsWith(prefix) ) {
count += api.getValue();
}
}
}
return count;
}
static public long getAPICountForOperation() {
long count = 0L;
synchronized( operationApis ) {
for( Map.Entry<String,Long> api : operationApis.entrySet() ) {
count += api.getValue();
}
}
return count;
}
static public long getAPICountForOperation(@Nonnull String providerName) {
return getAPICountForPrefixForOperation(providerName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER);
}
static public long getAPICountForOperation(@Nonnull String providerName, @Nonnull String cloudName) {
return getAPICountForPrefixForOperation(providerName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER + cloudName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER);
}
static public long getAPICountForOperation(@Nonnull String providerName, @Nonnull String cloudName, @Nonnull String accountNumber) {
return getAPICountForPrefixForOperation(providerName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER + cloudName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER + accountNumber.replaceAll(DELIMITER_REGEX, "_") + DELIMITER);
}
static public long getAPICountForOperation(@Nonnull String providerName, @Nonnull String cloudName, @Nonnull String accountNumber, @Nonnull String operation) {
return getAPICountForPrefixForOperation(providerName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER + cloudName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER + accountNumber.replaceAll(DELIMITER_REGEX, "_") + DELIMITER + operation);
}
static public long getAPICountForOperationAcrossAccounts(@Nonnull String providerName, @Nonnull String cloudName, @Nonnull String operation) {
String prefix = providerName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER + cloudName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER;
long count = 0L;
synchronized( operationApis ) {
for( Map.Entry<String,Long> api : operationApis.entrySet() ) {
if( api.getKey().startsWith(prefix) && api.getKey().endsWith(operation)) {
count += api.getValue();
}
}
}
return count;
}
static private long getAPICountForPrefixForOperation(@Nonnull String prefix) {
long count = 0L;
synchronized( operationApis ) {
for( Map.Entry<String,Long> api : operationApis.entrySet() ) {
if( api.getKey().startsWith(prefix) ) {
count += api.getValue();
}
}
}
return count;
}
static public long getOperationCount() {
long count = 0L;
synchronized( operationCount ) {
for( Map.Entry<String,Long> api : operationCount.entrySet() ) {
count += api.getValue();
}
}
return count;
}
static public long getOperationCount(@Nonnull String providerName) {
return getOperationCountForPrefix(providerName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER);
}
static public long getOperationCount(@Nonnull String providerName, @Nonnull String cloudName) {
return getOperationCountForPrefix(providerName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER + cloudName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER);
}
static public long getOperationCount(@Nonnull String providerName, @Nonnull String cloudName, @Nonnull String accountNumber) {
return getOperationCountForPrefix(providerName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER + cloudName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER + accountNumber.replaceAll(DELIMITER_REGEX, "_") + DELIMITER);
}
static public long getOperationCount(@Nonnull String providerName, @Nonnull String cloudName, @Nonnull String accountNumber, @Nonnull String operation) {
return getOperationCountForPrefix(providerName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER + cloudName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER + accountNumber.replaceAll(DELIMITER_REGEX, "_") + DELIMITER + operation);
}
static public long getOperationCountAcrossAccounts(@Nonnull String providerName, @Nonnull String cloudName, @Nonnull String operation) {
String prefix = providerName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER + cloudName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER;
long count = 0L;
synchronized( operationCount ) {
for( Map.Entry<String,Long> api : operationCount.entrySet() ) {
if( api.getKey().startsWith(prefix) && api.getKey().endsWith(operation)) {
count += api.getValue();
}
}
}
return count;
}
static private long getOperationCountForPrefix(@Nonnull String prefix) {
long count = 0L;
synchronized( operationCount ) {
for( Map.Entry<String,Long> api : operationCount.entrySet() ) {
if( api.getKey().startsWith(prefix) ) {
count += api.getValue();
}
}
}
return count;
}
static public @Nullable String getStackTrace(@Nonnull String providerName, @Nonnull String cloudName, @Nonnull String operationName) {
CloudOperation operation = null;
synchronized( operationTrace ) {
for( Map.Entry<String,CloudOperation> entry : operationTrace.entrySet() ) {
if( entry.getKey().startsWith(providerName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER + cloudName.replaceAll(DELIMITER_REGEX, "_") + DELIMITER) && entry.getKey().endsWith(DELIMITER + operationName) ) {
operation = entry.getValue();
break;
}
}
}
if( operation == null ) {
return null;
}
return (new JSONObject(toJSON(operation))).toString();
}
static private @Nonnull Map<String,Object> toJSON(@Nonnull CloudOperation operation) {
HashMap<String,Object> map = new HashMap<String, Object>();
String[] parts = operation.name.split(DELIMITER_REGEX);
String provider, cloud;
String name;
if( parts.length == 4 ) {
provider = parts[0];
cloud = parts[1];
name = parts[3];
}
else if( parts.length > 4 ) {
StringBuilder tmp = new StringBuilder();
provider = parts[0];
cloud = parts[1];
for( int i=3; i<parts.length; i++ ) {
tmp.append(parts[i]);
if( i < parts.length-1 ) {
tmp.append(DELIMITER);
}
}
name = tmp.toString();
}
else {
provider = (parts.length > 0 ? parts[0] : "");
cloud = (parts.length > 1 ? parts[1] : "");
name = operation.name;
}
map.put("operation", name);
map.put("provider", provider);
map.put("cloud", cloud);
map.put("apiCalls", operation.apiCalls == null ? new String[0] : operation.apiCalls);
if( operation.endTimestamp > 0L ) {
map.put("duration", operation.endTimestamp - operation.startTimestamp);
}
if( operation.priorChildren != null ) {
ArrayList<Map<String,Object>> children = new ArrayList<Map<String, Object>>();
for( CloudOperation child : operation.priorChildren ) {
children.add(toJSON(child));
}
map.put("operationCalls", children);
}
else {
map.put("operationCalls", new String[0]);
}
return map;
}
static public String[] listAccounts(@Nonnull String provider, @Nonnull String cloud) {
provider = provider.replaceAll(DELIMITER_REGEX, "_");
cloud = cloud.replaceAll(DELIMITER_REGEX, "_");
TreeSet<String> list = new TreeSet<String>();
synchronized( apiCount ) {
for( String call : apiCount.keySet() ) {
String[] parts = call.split(DELIMITER_REGEX);
if( parts.length > 2 && parts[0].equals(provider) && parts[1].equals(cloud) ) {
list.add(parts[2]);
}
}
}
return list.toArray(new String[list.size()]);
}
static public String[] listApis(@Nonnull String provider, @Nonnull String cloud) {
provider = provider.replaceAll(DELIMITER_REGEX, "_");
cloud = cloud.replaceAll(DELIMITER_REGEX, "_");
TreeSet<String> list = new TreeSet<String>();
synchronized( apiCount ) {
for( String call : apiCount.keySet() ) {
String[] parts = call.split(DELIMITER_REGEX);
if( parts.length > 3 && parts[0].equals(provider) && parts[1].equals(cloud) ) {
if( parts.length == 4 ) {
list.add(parts[3]);
}
else {
StringBuilder tmp = new StringBuilder();
for( int i=3; i<parts.length; i++ ) {
tmp.append(parts[i]);
if( i< parts.length-1 ) {
tmp.append(DELIMITER);
}
}
list.add(tmp.toString());
}
}
}
}
return list.toArray(new String[list.size()]);
}
static public String[] listClouds(@Nonnull String provider) {
provider = provider.replaceAll(DELIMITER_REGEX, "_");
TreeSet<String> list = new TreeSet<String>();
synchronized( apiCount ) {
for( String call : apiCount.keySet() ) {
String[] parts = call.split(DELIMITER_REGEX);
if( parts.length > 1 && parts[0].equals(provider) ) {
list.add(parts[1]);
}
}
}
return list.toArray(new String[list.size()]);
}
static public String[] listOperations(@Nonnull String provider, @Nonnull String cloud) {
provider = provider.replaceAll(DELIMITER_REGEX, "_");
cloud = cloud.replaceAll(DELIMITER_REGEX, "_");
TreeSet<String> list = new TreeSet<String>();
synchronized( operationCount ) {
for( String call : operationCount.keySet() ) {
String[] parts = call.split(DELIMITER_REGEX);
if( parts.length > 3 && parts[0].equals(provider) && parts[1].equals(cloud) ) {
if( parts.length == 4 ) {
list.add(parts[3]);
}
else {
StringBuilder tmp = new StringBuilder();
for( int i=3; i<parts.length; i++ ) {
tmp.append(parts[i]);
if( i< parts.length-1 ) {
tmp.append(DELIMITER);
}
}
list.add(tmp.toString());
}
}
}
}
return list.toArray(new String[list.size()]);
}
static public String[] listProviders() {
TreeSet<String> providers = new TreeSet<String>();
synchronized( apiCount ) {
for( String call : apiCount.keySet() ) {
String[] parts = call.split(DELIMITER_REGEX);
if( parts.length > 0 ) {
providers.add(parts[0]);
}
}
}
return providers.toArray(new String[providers.size()]);
}
static private void log(CloudOperation operation) {
long count = count(operation);
synchronized( operationApis ) {
if( operationApis.containsKey(operation.name) ) {
operationApis.put(operation.name, operationApis.get(operation.name) + count);
}
else {
operationApis.put(operation.name, count);
}
}
if( logger.isTraceEnabled() ) {
operationTrace.put(operation.name, operation);
}
}
static public void report(@Nonnull String prefix) {
logger.info("");
if( logger.isInfoEnabled() ) {
synchronized( apiCount ) {
TreeSet<String> keys = new TreeSet<String>();
keys.addAll(apiCount.keySet());
logger.debug(prefix + "-> API calls: ");
for( String key : keys ) {
logger.debug(prefix + "->\t" + key + " = " + apiCount.get(key));
}
}
}
if( logger.isDebugEnabled() ) {
synchronized( operationCount ) {
TreeSet<String> keys = new TreeSet<String>();
keys.addAll(operationCount.keySet());
logger.debug(prefix + "-> Operation calls:");
for( String key : keys ) {
logger.debug(prefix + "->\t" + key + " = " + operationCount.get(key));
}
}
synchronized( operationApis ) {
TreeSet<String> keys = new TreeSet<String>();
keys.addAll(operationApis.keySet());
logger.debug(prefix + "-> API calls by operation:");
for( String key : keys ) {
logger.debug(prefix + "->\t" + key + " = " + operationApis.get(key));
}
}
}
if( logger.isTraceEnabled() ) {
synchronized( operationTrace ) {
TreeSet<String> keys = new TreeSet<String>();
keys.addAll(operationTrace.keySet());
logger.trace(prefix + "-> Stack trace:");
for( String key : keys ) {
Map<String,Object> map = toJSON(operationTrace.get(key));
logger.trace((new JSONObject(map)).toString());
logger.trace("");
}
}
}
logger.info("");
}
static public void reset() {
synchronized( apiCount ) {
apiCount.clear();
}
synchronized( operationApis ) {
operationApis.clear();
}
synchronized( operationCount ) {
operationCount.clear();
}
synchronized( operationTrace ) {
operationTrace.clear();
}
operations.clear();
}
static public void trace(@Nonnull CloudProvider provider, @Nonnull String apiCall) {
if( logger.isInfoEnabled() ) {
ProviderContext ctx = provider.getContext();
String accountNumber = getAccountNumber( ctx );
String callName = provider.getProviderName().replaceAll(DELIMITER_REGEX, "_") + DELIMITER + provider.getCloudName().replaceAll(DELIMITER_REGEX, "_") + DELIMITER + accountNumber.replaceAll(DELIMITER_REGEX, "_") + DELIMITER + apiCall;
try {
CloudOperation current = null;
if( logger.isDebugEnabled() ) {
long thread = Thread.currentThread().getId();
current = operations.get(thread);
if( current != null ) {
while( current.currentChild != null ) {
current = current.currentChild;
}
current.calls++;
}
}
synchronized( apiCount ) {
if( apiCount.containsKey(callName) ) {
apiCount.put(callName, apiCount.get(callName) + 1);
}
else {
apiCount.put(callName, 1L);
}
}
if( logger.isTraceEnabled() ) {
if( current != null ) {
if( current.apiCalls == null ) {
current.apiCalls = new ArrayList<String>();
}
current.apiCalls.add(apiCall);
}
}
}
catch( Throwable t ) {
logger.warn("Error with API trace trace: " + t.getMessage());
}
}
}
static public String getAccountNumber(@Nullable ProviderContext ctx) {
return ((ctx == null || ctx.getAccountNumber() == null) ? "---" : ctx.getAccountNumber());
}
}