traceview 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (137) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.rubocop.yml +5 -0
  4. data/.travis.yml +58 -0
  5. data/Appraisals +10 -0
  6. data/CHANGELOG.md +490 -0
  7. data/CONFIG.md +16 -0
  8. data/Gemfile +95 -0
  9. data/LICENSE +199 -0
  10. data/README.md +380 -0
  11. data/Rakefile +109 -0
  12. data/examples/DNT.md +35 -0
  13. data/examples/carrying_context.rb +225 -0
  14. data/examples/instrumenting_metal_controller.rb +8 -0
  15. data/examples/puma_on_heroku_config.rb +17 -0
  16. data/examples/tracing_async_threads.rb +125 -0
  17. data/examples/tracing_background_jobs.rb +52 -0
  18. data/examples/tracing_forked_processes.rb +100 -0
  19. data/examples/unicorn_on_heroku_config.rb +28 -0
  20. data/ext/oboe_metal/extconf.rb +61 -0
  21. data/ext/oboe_metal/noop/noop.c +7 -0
  22. data/ext/oboe_metal/src/bson/bson.h +221 -0
  23. data/ext/oboe_metal/src/bson/platform_hacks.h +91 -0
  24. data/ext/oboe_metal/src/oboe.h +275 -0
  25. data/ext/oboe_metal/src/oboe.hpp +352 -0
  26. data/ext/oboe_metal/src/oboe_wrap.cxx +3886 -0
  27. data/ext/oboe_metal/tests/test.rb +11 -0
  28. data/gemfiles/mongo.gemfile +33 -0
  29. data/gemfiles/moped.gemfile +33 -0
  30. data/get_version.rb +5 -0
  31. data/init.rb +4 -0
  32. data/lib/joboe_metal.rb +206 -0
  33. data/lib/oboe.rb +7 -0
  34. data/lib/oboe/README +2 -0
  35. data/lib/oboe/backward_compatibility.rb +59 -0
  36. data/lib/oboe/inst/rack.rb +11 -0
  37. data/lib/oboe_metal.rb +151 -0
  38. data/lib/rails/generators/traceview/install_generator.rb +76 -0
  39. data/lib/rails/generators/traceview/templates/traceview_initializer.rb +159 -0
  40. data/lib/traceview.rb +62 -0
  41. data/lib/traceview/api.rb +18 -0
  42. data/lib/traceview/api/layerinit.rb +51 -0
  43. data/lib/traceview/api/logging.rb +209 -0
  44. data/lib/traceview/api/memcache.rb +31 -0
  45. data/lib/traceview/api/profiling.rb +50 -0
  46. data/lib/traceview/api/tracing.rb +135 -0
  47. data/lib/traceview/api/util.rb +121 -0
  48. data/lib/traceview/base.rb +225 -0
  49. data/lib/traceview/config.rb +238 -0
  50. data/lib/traceview/frameworks/grape.rb +97 -0
  51. data/lib/traceview/frameworks/padrino.rb +64 -0
  52. data/lib/traceview/frameworks/padrino/templates.rb +58 -0
  53. data/lib/traceview/frameworks/rails.rb +145 -0
  54. data/lib/traceview/frameworks/rails/helpers/rum/rum_ajax_header.js.erb +5 -0
  55. data/lib/traceview/frameworks/rails/helpers/rum/rum_footer.js.erb +1 -0
  56. data/lib/traceview/frameworks/rails/helpers/rum/rum_header.js.erb +3 -0
  57. data/lib/traceview/frameworks/rails/inst/action_controller.rb +216 -0
  58. data/lib/traceview/frameworks/rails/inst/action_view.rb +56 -0
  59. data/lib/traceview/frameworks/rails/inst/action_view_2x.rb +54 -0
  60. data/lib/traceview/frameworks/rails/inst/action_view_30.rb +48 -0
  61. data/lib/traceview/frameworks/rails/inst/active_record.rb +24 -0
  62. data/lib/traceview/frameworks/rails/inst/connection_adapters/mysql.rb +43 -0
  63. data/lib/traceview/frameworks/rails/inst/connection_adapters/mysql2.rb +28 -0
  64. data/lib/traceview/frameworks/rails/inst/connection_adapters/oracle.rb +18 -0
  65. data/lib/traceview/frameworks/rails/inst/connection_adapters/postgresql.rb +30 -0
  66. data/lib/traceview/frameworks/rails/inst/connection_adapters/utils.rb +117 -0
  67. data/lib/traceview/frameworks/sinatra.rb +95 -0
  68. data/lib/traceview/frameworks/sinatra/templates.rb +56 -0
  69. data/lib/traceview/inst/cassandra.rb +279 -0
  70. data/lib/traceview/inst/dalli.rb +86 -0
  71. data/lib/traceview/inst/em-http-request.rb +99 -0
  72. data/lib/traceview/inst/excon.rb +111 -0
  73. data/lib/traceview/inst/faraday.rb +73 -0
  74. data/lib/traceview/inst/http.rb +87 -0
  75. data/lib/traceview/inst/httpclient.rb +173 -0
  76. data/lib/traceview/inst/memcache.rb +102 -0
  77. data/lib/traceview/inst/memcached.rb +94 -0
  78. data/lib/traceview/inst/mongo.rb +238 -0
  79. data/lib/traceview/inst/moped.rb +474 -0
  80. data/lib/traceview/inst/rack.rb +122 -0
  81. data/lib/traceview/inst/redis.rb +271 -0
  82. data/lib/traceview/inst/resque.rb +192 -0
  83. data/lib/traceview/inst/rest-client.rb +38 -0
  84. data/lib/traceview/inst/sequel.rb +162 -0
  85. data/lib/traceview/inst/typhoeus.rb +102 -0
  86. data/lib/traceview/instrumentation.rb +21 -0
  87. data/lib/traceview/loading.rb +94 -0
  88. data/lib/traceview/logger.rb +41 -0
  89. data/lib/traceview/method_profiling.rb +84 -0
  90. data/lib/traceview/ruby.rb +36 -0
  91. data/lib/traceview/support.rb +113 -0
  92. data/lib/traceview/thread_local.rb +26 -0
  93. data/lib/traceview/util.rb +250 -0
  94. data/lib/traceview/version.rb +16 -0
  95. data/lib/traceview/xtrace.rb +90 -0
  96. data/test/frameworks/apps/grape_nested.rb +30 -0
  97. data/test/frameworks/apps/grape_simple.rb +24 -0
  98. data/test/frameworks/apps/padrino_simple.rb +45 -0
  99. data/test/frameworks/apps/sinatra_simple.rb +24 -0
  100. data/test/frameworks/grape_test.rb +142 -0
  101. data/test/frameworks/padrino_test.rb +30 -0
  102. data/test/frameworks/sinatra_test.rb +30 -0
  103. data/test/instrumentation/cassandra_test.rb +380 -0
  104. data/test/instrumentation/dalli_test.rb +171 -0
  105. data/test/instrumentation/em_http_request_test.rb +86 -0
  106. data/test/instrumentation/excon_test.rb +207 -0
  107. data/test/instrumentation/faraday_test.rb +235 -0
  108. data/test/instrumentation/http_test.rb +140 -0
  109. data/test/instrumentation/httpclient_test.rb +296 -0
  110. data/test/instrumentation/memcache_test.rb +251 -0
  111. data/test/instrumentation/memcached_test.rb +226 -0
  112. data/test/instrumentation/mongo_test.rb +462 -0
  113. data/test/instrumentation/moped_test.rb +496 -0
  114. data/test/instrumentation/rack_test.rb +116 -0
  115. data/test/instrumentation/redis_hashes_test.rb +265 -0
  116. data/test/instrumentation/redis_keys_test.rb +318 -0
  117. data/test/instrumentation/redis_lists_test.rb +310 -0
  118. data/test/instrumentation/redis_misc_test.rb +160 -0
  119. data/test/instrumentation/redis_sets_test.rb +293 -0
  120. data/test/instrumentation/redis_sortedsets_test.rb +325 -0
  121. data/test/instrumentation/redis_strings_test.rb +333 -0
  122. data/test/instrumentation/resque_test.rb +62 -0
  123. data/test/instrumentation/rest-client_test.rb +294 -0
  124. data/test/instrumentation/sequel_mysql2_test.rb +326 -0
  125. data/test/instrumentation/sequel_mysql_test.rb +326 -0
  126. data/test/instrumentation/sequel_pg_test.rb +330 -0
  127. data/test/instrumentation/typhoeus_test.rb +285 -0
  128. data/test/minitest_helper.rb +187 -0
  129. data/test/profiling/method_test.rb +198 -0
  130. data/test/servers/rackapp_8101.rb +22 -0
  131. data/test/support/backcompat_test.rb +269 -0
  132. data/test/support/config_test.rb +128 -0
  133. data/test/support/dnt_test.rb +73 -0
  134. data/test/support/liboboe_settings_test.rb +104 -0
  135. data/test/support/xtrace_test.rb +35 -0
  136. data/traceview.gemspec +29 -0
  137. metadata +250 -0
@@ -0,0 +1,91 @@
1
+ /* platform_hacks.h */
2
+ /* Copyright 2009, 2010 10gen Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+
18
+ /* all platform-specific ifdefs should go here */
19
+
20
+ #ifndef _PLATFORM_HACKS_H_
21
+ #define _PLATFORM_HACKS_H_
22
+
23
+ #ifdef __GNUC__
24
+ #define MONGO_INLINE static __inline__
25
+ #else
26
+ #define MONGO_INLINE static
27
+ #endif
28
+
29
+ #ifdef __cplusplus
30
+ #define MONGO_EXTERN_C_START extern "C" {
31
+ #define MONGO_EXTERN_C_END }
32
+ #else
33
+ #define MONGO_EXTERN_C_START
34
+ #define MONGO_EXTERN_C_END
35
+ #endif
36
+
37
+
38
+ #if defined(MONGO_HAVE_STDINT) || __STDC_VERSION__ >= 199901L
39
+ #include <stdint.h>
40
+ #elif defined(MONGO_HAVE_UNISTD)
41
+ #include <unistd.h>
42
+ #elif defined(MONGO_USE__INT64)
43
+ typedef __int64 int64_t;
44
+ #elif defined(MONGO_USE_LONG_LONG_INT)
45
+ typedef long long int int64_t;
46
+ #else
47
+ #error must have a 64bit int type
48
+ #endif
49
+
50
+ /* big endian is only used for OID generation. little is used everywhere else */
51
+ #ifdef MONGO_BIG_ENDIAN
52
+ #define bson_little_endian64(out, in) ( bson_swap_endian64(out, in) )
53
+ #define bson_little_endian32(out, in) ( bson_swap_endian32(out, in) )
54
+ #define bson_big_endian64(out, in) ( memcpy(out, in, 8) )
55
+ #define bson_big_endian32(out, in) ( memcpy(out, in, 4) )
56
+ #else
57
+ #define bson_little_endian64(out, in) ( memcpy(out, in, 8) )
58
+ #define bson_little_endian32(out, in) ( memcpy(out, in, 4) )
59
+ #define bson_big_endian64(out, in) ( bson_swap_endian64(out, in) )
60
+ #define bson_big_endian32(out, in) ( bson_swap_endian32(out, in) )
61
+ #endif
62
+
63
+ MONGO_EXTERN_C_START
64
+
65
+ MONGO_INLINE void bson_swap_endian64(void* outp, const void* inp){
66
+ const char *in = (const char*)inp;
67
+ char *out = (char*)outp;
68
+
69
+ out[0] = in[7];
70
+ out[1] = in[6];
71
+ out[2] = in[5];
72
+ out[3] = in[4];
73
+ out[4] = in[3];
74
+ out[5] = in[2];
75
+ out[6] = in[1];
76
+ out[7] = in[0];
77
+
78
+ }
79
+ MONGO_INLINE void bson_swap_endian32(void* outp, const void* inp){
80
+ const char *in = (const char*)inp;
81
+ char *out = (char*)outp;
82
+
83
+ out[0] = in[3];
84
+ out[1] = in[2];
85
+ out[2] = in[1];
86
+ out[3] = in[0];
87
+ }
88
+
89
+ MONGO_EXTERN_C_END
90
+
91
+ #endif
@@ -0,0 +1,275 @@
1
+ #ifndef LIBOBOE_H
2
+ #define LIBOBOE_H
3
+
4
+ #ifdef __cplusplus
5
+ extern "C" {
6
+ #endif
7
+
8
+ #include <sys/types.h>
9
+ #include <inttypes.h>
10
+ #include "bson/bson.h"
11
+
12
+ #define OBOE_SAMPLE_RATE_DEFAULT 300000 // 30%
13
+ #define OBOE_SAMPLE_RESOLUTION 1000000
14
+
15
+ #define OBOE_MAX_TASK_ID_LEN 20
16
+ #define OBOE_MAX_OP_ID_LEN 8
17
+ #define OBOE_MAX_METADATA_PACK_LEN 512
18
+
19
+ #define XTR_CURRENT_VERSION 1
20
+ #define XTR_UDP_PORT 7831
21
+
22
+
23
+ // structs
24
+
25
+ typedef struct oboe_ids {
26
+ uint8_t task_id[OBOE_MAX_TASK_ID_LEN];
27
+ uint8_t op_id[OBOE_MAX_OP_ID_LEN];
28
+ } oboe_ids_t;
29
+
30
+ typedef struct oboe_metadata {
31
+ oboe_ids_t ids;
32
+ size_t task_len;
33
+ size_t op_len;
34
+ } oboe_metadata_t;
35
+
36
+ typedef struct oboe_event {
37
+ oboe_metadata_t metadata;
38
+ bson_buffer bbuf;
39
+ char * bb_str;
40
+ } oboe_event_t;
41
+
42
+
43
+ // oboe_metadata
44
+
45
+ int oboe_metadata_init (oboe_metadata_t *);
46
+ int oboe_metadata_destroy (oboe_metadata_t *);
47
+
48
+ int oboe_metadata_is_valid (const oboe_metadata_t *);
49
+
50
+ void oboe_metadata_copy (oboe_metadata_t *, const oboe_metadata_t *);
51
+
52
+ void oboe_metadata_random (oboe_metadata_t *);
53
+
54
+ int oboe_metadata_set_lengths (oboe_metadata_t *, size_t, size_t);
55
+ int oboe_metadata_create_event (const oboe_metadata_t *, oboe_event_t *);
56
+
57
+ int oboe_metadata_tostr (const oboe_metadata_t *, char *, size_t);
58
+ int oboe_metadata_fromstr (oboe_metadata_t *, const char *, size_t);
59
+
60
+
61
+ // oboe_event
62
+
63
+ int oboe_event_init (oboe_event_t *, const oboe_metadata_t *);
64
+ int oboe_event_destroy (oboe_event_t *);
65
+
66
+ int oboe_event_add_info (oboe_event_t *, const char *, const char *);
67
+ int oboe_event_add_info_binary (oboe_event_t *, const char *, const char *, size_t);
68
+ int oboe_event_add_info_int64 (oboe_event_t *, const char *, const int64_t);
69
+ int oboe_event_add_info_double (oboe_event_t *, const char *, const double);
70
+ int oboe_event_add_info_bool (oboe_event_t *, const char *, const int);
71
+ int oboe_event_add_info_fmt (oboe_event_t *, const char *key, const char *fmt, ...);
72
+ int oboe_event_add_info_bson (oboe_event_t *, const char *key, const bson *val);
73
+ int oboe_event_add_edge (oboe_event_t *, const oboe_metadata_t *);
74
+ int oboe_event_add_edge_fromstr(oboe_event_t *, const char *, size_t);
75
+
76
+
77
+ // oboe_context
78
+
79
+ oboe_metadata_t *oboe_context_get();
80
+ void oboe_context_set(oboe_metadata_t *);
81
+ int oboe_context_set_fromstr(const char *, size_t);
82
+
83
+ void oboe_context_clear();
84
+
85
+ int oboe_context_is_valid();
86
+
87
+
88
+ // oboe_reporter
89
+
90
+ typedef ssize_t (*reporter_send)(void *, const char *, size_t);
91
+ typedef int (*reporter_destroy)(void *);
92
+
93
+ typedef struct oboe_reporter {
94
+ void * descriptor;
95
+ reporter_send send;
96
+ reporter_destroy destroy;
97
+ } oboe_reporter_t;
98
+
99
+ int oboe_reporter_udp_init (oboe_reporter_t *, const char *, const char *);
100
+ int oboe_reporter_file_init (oboe_reporter_t *, const char *);
101
+
102
+ int oboe_reporter_send(oboe_reporter_t *, oboe_metadata_t *, oboe_event_t *);
103
+ int oboe_reporter_destroy(oboe_reporter_t *);
104
+ ssize_t oboe_reporter_udp_send(void *desc, const char *data, size_t len);
105
+
106
+
107
+ // initialization
108
+
109
+ void oboe_init();
110
+
111
+
112
+ // Settings interface
113
+
114
+ #define OBOE_SETTINGS_VERSION 1
115
+ #define OBOE_SETTINGS_MAGIC_NUMBER 0x6f626f65
116
+ #define OBOE_SETTINGS_TYPE_SKIP 0
117
+ #define OBOE_SETTINGS_TYPE_STOP 1
118
+ #define OBOE_SETTINGS_TYPE_DEFAULT_SAMPLE_RATE 2
119
+ #define OBOE_SETTINGS_TYPE_LAYER_SAMPLE_RATE 3
120
+ #define OBOE_SETTINGS_TYPE_LAYER_APP_SAMPLE_RATE 4
121
+ #define OBOE_SETTINGS_TYPE_LAYER_HTTPHOST_SAMPLE_RATE 5
122
+ #define OBOE_SETTINGS_TYPE_CONFIG_STRING 6
123
+ #define OBOE_SETTINGS_TYPE_CONFIG_INT 7
124
+ #define OBOE_SETTINGS_FLAG_OK 0x0
125
+ #define OBOE_SETTINGS_FLAG_INVALID 0x1
126
+ #define OBOE_SETTINGS_FLAG_OVERRIDE 0x2
127
+ #define OBOE_SETTINGS_FLAG_SAMPLE_START 0x4
128
+ #define OBOE_SETTINGS_FLAG_SAMPLE_THROUGH 0x8
129
+ #define OBOE_SETTINGS_FLAG_SAMPLE_THROUGH_ALWAYS 0x10
130
+ #define OBOE_SETTINGS_FLAG_SAMPLE_AVW_ALWAYS 0x20
131
+ #define OBOE_SETTINGS_MAX_STRLEN 256
132
+
133
+ #define OBOE_SETTINGS_UNSET -1
134
+ #define OBOE_SETTINGS_MIN_REFRESH_INTERVAL 30
135
+
136
+ // Value for "SampleSource" info key
137
+ // where was the sample rate specified? (oboe settings, config file, hard-coded default, etc)
138
+ #define OBOE_SAMPLE_RATE_SOURCE_FILE 1
139
+ #define OBOE_SAMPLE_RATE_SOURCE_DEFAULT 2
140
+ #define OBOE_SAMPLE_RATE_SOURCE_OBOE 3
141
+ #define OBOE_SAMPLE_RATE_SOURCE_LAST_OBOE 4
142
+ #define OBOE_SAMPLE_RATE_SOURCE_DEFAULT_MISCONFIGURED 5
143
+ #define OBOE_SAMPLE_RATE_SOURCE_OBOE_DEFAULT 6
144
+
145
+ #define OBOE_SAMPLE_RESOLUTION 1000000
146
+
147
+ // Used to convert to settings flags:
148
+ #define OBOE_TRACE_NEVER 0
149
+ #define OBOE_TRACE_ALWAYS 1
150
+ #define OBOE_TRACE_THROUGH 2
151
+
152
+ typedef struct {
153
+ volatile uint32_t magic;
154
+ volatile uint32_t timestamp;
155
+ volatile uint16_t type;
156
+ volatile uint16_t flags;
157
+ volatile uint32_t value;
158
+ uint32_t _pad;
159
+ char layer[OBOE_SETTINGS_MAX_STRLEN];
160
+ char arg[OBOE_SETTINGS_MAX_STRLEN];
161
+ } __attribute__((packed)) oboe_settings_t;
162
+
163
+ // Current settings configuration:
164
+ typedef struct {
165
+ int tracing_mode; // loaded from config file
166
+ int sample_rate; // loaded from config file
167
+ int default_sample_rate; // default sample rate (fallback)
168
+ oboe_settings_t *settings; // cached settings, updated by tracelyzer (init to NULL)
169
+ int last_auto_sample_rate; // stores last known automatic sampling rate
170
+ uint16_t last_auto_flags; // stores last known flags associated with above
171
+ uint32_t last_auto_timestamp; // timestamp from last *settings lookup
172
+ uint32_t last_refresh; // last refresh time
173
+ } oboe_settings_cfg_t;
174
+
175
+ oboe_settings_t* oboe_settings_get(uint16_t type, const char* layer, const char* arg);
176
+ oboe_settings_t* oboe_settings_get_layer_tracing_mode(const char* layer);
177
+ oboe_settings_t* oboe_settings_get_layer_sample_rate(const char* layer);
178
+ oboe_settings_t* oboe_settings_get_layer_app_sample_rate(const char* layer, const char* app);
179
+ uint32_t oboe_settings_get_latest_timestamp(const char* layer);
180
+ int oboe_settings_get_value(oboe_settings_t *s, int *outval, unsigned short *outflags, uint32_t *outtimestamp);
181
+
182
+ oboe_settings_cfg_t* oboe_settings_cfg_get();
183
+ void oboe_settings_cfg_init(oboe_settings_cfg_t *cfg);
184
+ void oboe_settings_cfg_tracing_mode_set(int new_mode);
185
+ void oboe_settings_cfg_sample_rate_set(int new_rate);
186
+
187
+ /**
188
+ * Check if this request should be sampled (deprecated - use oboe_sample_layer() instead).
189
+ *
190
+ * @param layer Layer name as used in oboe_settings_t.layer (may be NULL to use default settings)
191
+ * @param xtrace X-Trace ID string from an HTTP request or higher layer (NULL or empty string if not present).
192
+ * @param cfg The settings configuration to use for this evaluation.
193
+ * @param sample_rate_out The sample rate used to check if this request should be sampled
194
+ * (output - may be zero if not used).
195
+ * @param sample_source_out The OBOE_SAMPLE_RATE_SOURCE used to check if this request
196
+ * should be sampled (output - may be zero if not used).
197
+ * @return Non-zero if the given layer should be sampled.
198
+ */
199
+ int oboe_sample_request(const char *layer, const char *in_xtrace, oboe_settings_cfg_t *cfg,
200
+ int *sample_rate_out, int *sample_source_out);
201
+ int oboe_rand_get_value();
202
+
203
+ /**
204
+ * Check if this request should be sampled.
205
+ *
206
+ * Checks for sample rate flags and settings for the specified layer, considers any
207
+ * special features in the X-Trace and X-TV-Meta HTTP headers, and, if appropriate,
208
+ * rolls the virtual dice to decide if this request should be sampled.
209
+ *
210
+ * This replaces oboe_sample_request with a version that uses the settings
211
+ * configuration kept in thread-local storage and takes the X-TV-Meta HTTP
212
+ * header value in order to support AppView Web integration.
213
+ *
214
+ * @param layer Layer name as used in oboe_settings_t.layer (may be NULL to use default settings)
215
+ * @param xtrace X-Trace ID string from an HTTP request or higher layer (NULL or empty string if not present).
216
+ * @param tv_meta AppView Web ID from X-TV-Meta HTTP header or higher layer (NULL or empty string if not present).
217
+ * @param sample_rate_out The sample rate used to check if this request should be sampled
218
+ * (output - may be zero if not used).
219
+ * @param sample_source_out The OBOE_SAMPLE_RATE_SOURCE used to check if this request
220
+ * should be sampled (output - may be zero if not used).
221
+ * @return Non-zero if the given layer should be sampled.
222
+ */
223
+ int oboe_sample_layer(
224
+ const char *layer,
225
+ const char *xtrace,
226
+ const char *tv_meta,
227
+ int *sample_rate_out,
228
+ int *sample_source_out
229
+ );
230
+
231
+ /* Oboe configuration interface. */
232
+
233
+ /**
234
+ * Check if the Oboe library is compatible with a given version.revision.
235
+ *
236
+ * This will succeed if the library is at least as recent as specified and if no
237
+ * definitions have been removed since that revision.
238
+ *
239
+ * @param version The library's version number which increments every time the API changes.
240
+ * @param revision The revision of the current version of the library.
241
+ * @return Non-zero if the Oboe library is considered compatible with the specified revision.
242
+ */
243
+ extern int oboe_config_check_version(int version, int revision);
244
+
245
+ /**
246
+ * Get the Oboe library version number.
247
+ *
248
+ * This number increments whenever the API is changed.
249
+ *
250
+ * @return The library's version number or -1 if the version is not known.
251
+ */
252
+ extern int oboe_config_get_version();
253
+
254
+ /**
255
+ * Get the Oboe library revision number.
256
+ *
257
+ * This is the revision of the current version which is updated whenever
258
+ * compatible changes are made to the API/ABI (ie. additions).
259
+ *
260
+ * @return The library's revision number or -1 if not known.
261
+ */
262
+ extern int oboe_config_get_revision();
263
+
264
+ /*
265
+ * Get the Oboe library version as a string.
266
+ *
267
+ * Returns the complete VERSION string or null
268
+ */
269
+ const char* oboe_config_get_version_string();
270
+
271
+ #ifdef __cplusplus
272
+ } // extern "C"
273
+ #endif
274
+
275
+ #endif // LIBOBOE_H
@@ -0,0 +1,352 @@
1
+ #ifndef OBOE_HPP
2
+ #define OBOE_HPP
3
+
4
+ #include <string>
5
+ #include <oboe.h>
6
+
7
+
8
+ class Event;
9
+
10
+ class Metadata : private oboe_metadata_t {
11
+ friend class UdpReporter;
12
+ friend class FileReporter;
13
+ friend class Context;
14
+
15
+ public:
16
+ Metadata(oboe_metadata_t *md) {
17
+ oboe_metadata_copy(this, md);
18
+ }
19
+
20
+ ~Metadata() {
21
+ oboe_metadata_destroy(this);
22
+ }
23
+
24
+ static Metadata* fromString(std::string s) {
25
+ oboe_metadata_t md;
26
+ oboe_metadata_fromstr(&md, s.data(), s.size());
27
+ return new Metadata(&md); // copies md
28
+ }
29
+
30
+ // these new objects are managed by SWIG %newobject
31
+ Event *createEvent();
32
+
33
+ static Metadata *makeRandom() {
34
+ oboe_metadata_t md;
35
+ oboe_metadata_init(&md);
36
+ oboe_metadata_random(&md);
37
+ return new Metadata(&md); // copies md
38
+ }
39
+
40
+ Metadata *copy() {
41
+ return new Metadata(this);
42
+ }
43
+
44
+ bool isValid() {
45
+ return oboe_metadata_is_valid(this);
46
+ }
47
+
48
+ #ifdef SWIGJAVA
49
+ std::string toStr() {
50
+ #else
51
+ std::string toString() {
52
+ #endif
53
+ char buf[OBOE_MAX_METADATA_PACK_LEN];
54
+
55
+ int rc = oboe_metadata_tostr(this, buf, sizeof(buf) - 1);
56
+ if (rc == 0) {
57
+ return std::string(buf);
58
+ } else {
59
+ return std::string(); // throw exception?
60
+ }
61
+ }
62
+
63
+ };
64
+
65
+ class Context {
66
+ public:
67
+ /**
68
+ * Set the tracing mode.
69
+ *
70
+ * @param newMode One of
71
+ * - OBOE_TRACE_NEVER(0) to disable tracing,
72
+ * - OBOE_TRACE_ALWAYS(1) to start a new trace if needed, or
73
+ * - OBOE_TRACE_THROUGH(2) to only add to an existing trace.
74
+ */
75
+ static void setTracingMode(int newMode) {
76
+ oboe_settings_cfg_tracing_mode_set(newMode);
77
+ }
78
+
79
+ /**
80
+ * Set the default sample rate.
81
+ *
82
+ * This rate is used until overridden by the TraceView servers. If not set then the
83
+ * value 300,000 will be used (ie. 30%).
84
+ *
85
+ * The rate is interpreted as a ratio out of OBOE_SAMPLE_RESOLUTION (currently 1,000,000).
86
+ *
87
+ * @param newRate A number between 0 (none) and OBOE_SAMPLE_RESOLUTION (a million)
88
+ */
89
+ static void setDefaultSampleRate(int newRate) {
90
+ oboe_settings_cfg_sample_rate_set(newRate);
91
+ }
92
+
93
+ /**
94
+ * Check if the current request should be traced based on the current settings.
95
+ *
96
+ * If xtrace is empty, or if it is identified as a foreign (ie. cross customer)
97
+ * trace, then sampling will be considered as a new trace.
98
+ * Otherwise sampling will be considered as adding to the current trace.
99
+ * Different layers may have special rules. Also special rules for AppView
100
+ * Web synthetic traces apply if in_tv_meta is given a non-empty string.
101
+ *
102
+ * This is designed to be called once per layer per request.
103
+ *
104
+ * @param layer Name of the layer being considered for tracing
105
+ * @param in_xtrace Incoming X-Trace ID (NULL or empty string if not present)
106
+ * @param in_tv_meta AppView Web ID from X-TV-Meta HTTP header or higher layer (NULL or empty string if not present).
107
+ * @return Zero to not trace; otherwise return the sample rate used in the low order
108
+ * bytes 0 to 2 and the sample source in the higher-order byte 3.
109
+ */
110
+ static int sampleRequest(
111
+ std::string layer,
112
+ std::string in_xtrace,
113
+ std::string in_tv_meta)
114
+ {
115
+ int sample_rate = 0;
116
+ int sample_source = 0;
117
+ int rc = (oboe_sample_layer(layer.c_str(), in_xtrace.c_str(), in_tv_meta.c_str(), &sample_rate, &sample_source));
118
+
119
+ return (rc == 0 ? 0 : (((sample_source & 0xFF) << 24) | (sample_rate & 0xFFFFFF)));
120
+ }
121
+
122
+ // returns pointer to current context (from thread-local storage)
123
+ static oboe_metadata_t *get() {
124
+ return oboe_context_get();
125
+ }
126
+
127
+ #ifdef SWIGJAVA
128
+ static std::string toStr() {
129
+ #else
130
+ static std::string toString() {
131
+ #endif
132
+ char buf[OBOE_MAX_METADATA_PACK_LEN];
133
+
134
+ oboe_metadata_t *md = Context::get();
135
+ int rc = oboe_metadata_tostr(md, buf, sizeof(buf) - 1);
136
+ if (rc == 0) {
137
+ return std::string(buf);
138
+ } else {
139
+ return std::string(); // throw exception?
140
+ }
141
+ }
142
+
143
+ static void set(oboe_metadata_t *md) {
144
+ oboe_context_set(md);
145
+ }
146
+
147
+ static void fromString(std::string s) {
148
+ oboe_context_set_fromstr(s.data(), s.size());
149
+ }
150
+
151
+ // this new object is managed by SWIG %newobject
152
+ static Metadata *copy() {
153
+ return new Metadata(Context::get());
154
+ }
155
+
156
+ static void clear() {
157
+ oboe_context_clear();
158
+ }
159
+
160
+ static bool isValid() {
161
+ return oboe_context_is_valid();
162
+ }
163
+
164
+ static void init() {
165
+ oboe_init();
166
+ }
167
+
168
+ // these new objects are managed by SWIG %newobject
169
+ static Event *createEvent();
170
+ static Event *startTrace();
171
+ };
172
+
173
+ class Event : private oboe_event_t {
174
+ friend class UdpReporter;
175
+ friend class FileReporter;
176
+ friend class Context;
177
+ friend class Metadata;
178
+
179
+ private:
180
+ Event() {
181
+ oboe_event_init(this, Context::get());
182
+ }
183
+
184
+ Event(const oboe_metadata_t *md, bool addEdge=true) {
185
+ // both methods copy metadata from md -> this
186
+ if (addEdge) {
187
+ // create_event automatically adds edge in event to md
188
+ oboe_metadata_create_event(md, this);
189
+ } else {
190
+ // initializes new Event with this md's task_id & new random op_id; no edges set
191
+ oboe_event_init(this, md);
192
+ }
193
+ }
194
+
195
+ public:
196
+ ~Event() {
197
+ oboe_event_destroy(this);
198
+ }
199
+
200
+ // called e.g. from Python e.addInfo("Key", None) & Ruby e.addInfo("Key", nil)
201
+ bool addInfo(char *key, void* val) {
202
+ // oboe_event_add_info(evt, key, NULL) does nothing
203
+ (void) key;
204
+ (void) val;
205
+ return true;
206
+ }
207
+
208
+ bool addInfo(char *key, const std::string& val) {
209
+ if (memchr(val.data(), '\0', val.size())) {
210
+ return oboe_event_add_info_binary(this, key, val.data(), val.size()) == 0;
211
+ } else {
212
+ return oboe_event_add_info(this, key, val.data()) == 0;
213
+ }
214
+ }
215
+
216
+ bool addInfo(char *key, long val) {
217
+ int64_t val_ = val;
218
+ return oboe_event_add_info_int64(this, key, val_) == 0;
219
+ }
220
+
221
+ bool addInfo(char *key, double val) {
222
+ return oboe_event_add_info_double(this, key, val) == 0;
223
+ }
224
+
225
+ bool addEdge(oboe_metadata_t *md) {
226
+ return oboe_event_add_edge(this, md) == 0;
227
+ }
228
+
229
+ bool addEdgeStr(const std::string& val) {
230
+ return oboe_event_add_edge_fromstr(this, val.c_str(), val.size()) == 0;
231
+ }
232
+
233
+ Metadata* getMetadata() {
234
+ return new Metadata(&this->metadata);
235
+ }
236
+
237
+ std::string metadataString() {
238
+ char buf[OBOE_MAX_METADATA_PACK_LEN];
239
+
240
+ int rc = oboe_metadata_tostr(&this->metadata, buf, sizeof(buf) - 1);
241
+ if (rc == 0) {
242
+ return std::string(buf);
243
+ } else {
244
+ return std::string(); // throw exception?
245
+ }
246
+ }
247
+
248
+ static Event* startTrace(const oboe_metadata_t *md);
249
+
250
+ };
251
+
252
+ Event *Context::createEvent() {
253
+ return new Event(Context::get());
254
+ }
255
+
256
+ Event *Metadata::createEvent() {
257
+ return new Event(this);
258
+ }
259
+
260
+ Event *Context::startTrace() {
261
+ oboe_metadata_t *md = Context::get();
262
+ oboe_metadata_random(md);
263
+ return new Event();
264
+ }
265
+
266
+ Event *Event::startTrace(const oboe_metadata_t *md) {
267
+ return new Event(md, false);
268
+ }
269
+
270
+ class UdpReporter : private oboe_reporter_t {
271
+ public:
272
+ UdpReporter(const char *addr, const char *port=NULL) {
273
+ if (port == NULL)
274
+ port = "7831";
275
+
276
+ oboe_reporter_udp_init(this, addr, port);
277
+ }
278
+
279
+ ~UdpReporter() {
280
+ oboe_reporter_destroy(this);
281
+ }
282
+
283
+ bool sendReport(Event *evt) {
284
+ return oboe_reporter_send(this, Context::get(), evt) >= 0;
285
+ }
286
+
287
+ bool sendReport(Event *evt, oboe_metadata_t *md) {
288
+ return oboe_reporter_send(this, md, evt) >= 0;
289
+ }
290
+ };
291
+
292
+ class FileReporter : private oboe_reporter_t {
293
+ public:
294
+ FileReporter(const char *file) {
295
+ oboe_reporter_file_init(this, file);
296
+ }
297
+
298
+ ~FileReporter() {
299
+ oboe_reporter_destroy(this);
300
+ }
301
+
302
+ bool sendReport(Event *evt) {
303
+ return oboe_reporter_send(this, Context::get(), evt) >= 0;
304
+ }
305
+
306
+ bool sendReport(Event *evt, oboe_metadata_t *md) {
307
+ return oboe_reporter_send(this, md, evt) >= 0;
308
+ }
309
+ };
310
+
311
+
312
+ class Config {
313
+ public:
314
+ /**
315
+ * Check if the Oboe library is compatible with a given version.revision.
316
+ *
317
+ * This will succeed if the library is at least as recent as specified and if no
318
+ * definitions have been removed since that revision.
319
+ *
320
+ * @param version The library's version number which increments every time the API changes.
321
+ * @param revision The revision of the current version of the library.
322
+ * @return Non-zero if the Oboe library is considered compatible with the specified revision.
323
+ */
324
+ static bool checkVersion(int version, int revision) {
325
+ return (oboe_config_check_version(version, revision) != 0);
326
+ }
327
+
328
+ /**
329
+ * Get the Oboe library version number.
330
+ *
331
+ * This number increments whenever an incompatible change to the API/ABI is made.
332
+ *
333
+ * @return The library's version number or -1 if the version is not known.
334
+ */
335
+ static int getVersion() {
336
+ return oboe_config_get_version();
337
+ }
338
+
339
+ /**
340
+ * Get the Oboe library revision number.
341
+ *
342
+ * This number increments whenever a compatible change is made to the
343
+ * API/ABI (ie. an addition).
344
+ *
345
+ * @return The library's revision number or -1 if not known.
346
+ */
347
+ static int getRevision() {
348
+ return oboe_config_get_revision();
349
+ }
350
+ };
351
+
352
+ #endif