appoptics_apm 4.10.1 → 4.12.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,68 +0,0 @@
1
-
2
- ###############################################################
3
- # BASIC TRACING EXAMPLES
4
- ###############################################################
5
-
6
- # set APPOPTICS_SERVICE_KEY and run with
7
- # `bundle exec ruby 01_basic_tracing.rb`
8
-
9
- require 'appoptics_apm'
10
- unless AppopticsAPM::SDK.appoptics_ready?(10_000)
11
- puts "aborting!!! Agent not ready after 10 seconds"
12
- exit false
13
- end
14
-
15
-
16
- ###############################################################
17
- # Starting a trace and adding a span
18
- ###############################################################
19
-
20
- # USE CASE:
21
- # You may want to either trace a piece of your own code or a
22
- # call to a method from a gem that isn't auto-instrumented by
23
- # appoptics_apm
24
-
25
- # The first example will not create a span, because no trace has
26
- # been started, but the second and third ones will.
27
-
28
- # The string argument is the name for the span
29
-
30
- ##
31
- # AppOpticsAPM::SDK.trace()
32
- # most of the time this is the method you need. It adds a span
33
- # to a trace that has probably been started by rack.
34
-
35
- # Example 1
36
- def do_work
37
- 42
38
- end
39
-
40
- AppOpticsAPM::SDK.trace('simple_span') do
41
- do_work
42
- end
43
-
44
- ##
45
- # AppOpticsAPM::SDK.start_trace()
46
- # This method starts a trace. It is handy for background jobs,
47
- # workers, or scripts, that are not part of a rack application
48
-
49
- # Example 2
50
- AppOpticsAPM::SDK.start_trace('outer_span') do
51
-
52
- AppOpticsAPM::SDK.trace('simple_span') do
53
- do_work
54
- AppOpticsAPM::API.log_info(AppOpticsAPM.layer, { some: :fancy, hash: :to, send: 1 })
55
- end
56
-
57
- end
58
-
59
- # Example 3
60
- def do_traced_work
61
- AppOpticsAPM::SDK.trace('simple_span_2') do
62
- do_work
63
- end
64
- end
65
-
66
- AppOpticsAPM::SDK.start_trace('outer_span_2') do
67
- do_traced_work
68
- end
@@ -1,220 +0,0 @@
1
- ###############################################################
2
- # A brief overview of AppOpticsAPM tracing context
3
- ###############################################################
4
- #
5
- # Tracing context is the state held when AppOpticsAPM is instrumenting a
6
- # transaction, block, request etc.. This context is advanced as
7
- # new blocks are instrumented and this chain of context is used
8
- # by AppOpticsAPM to later reassemble performance data to be displayed
9
- # in the AppOptics dashboard.
10
- #
11
- # Tracing context is non-existent until established by calling
12
- # `AppOpticsAPM::API.start_trace` or `AppOpticsAPM::API.log_start`. Those methods
13
- # are part of the high-level and low-level API respectively.
14
- #
15
- # After a tracing context is established, that context can be
16
- # continued by calling `AppOpticsAPM::API.trace` or `AppOpticsAPM::API.log_entry`.
17
- # These methods will advance an existing context but not start
18
- # new one.
19
- #
20
- # For example, when a web request comes into a stack, a tracing
21
- # context is established using `AppOpticsAPM::API.log_start` as the request
22
- # enters through the rack middleware via `AppOpticsAPM::Rack`.
23
- #
24
- # That tracing context is then continued using `AppOpticsAPM::API.trace` or
25
- # `AppOpticsAPM::API.log_entry` for each subsequent layer such as Rails,
26
- # ActiveRecord, Redis, Memcache, ActionView, Mongo (etc...) until
27
- # finally request processing is complete and the tracing context
28
- # is cleared (AppOpticsAPM::Context.clear)
29
- #
30
-
31
- ###############################################################
32
- # Carrying Context
33
- ###############################################################
34
- #
35
- # The tracing context exists in the form of an X-Trace string and
36
- # can be retrieved using 'AppOpticsAPM::Context.toString'
37
- #
38
- # xtrace = AppOpticsAPM::Context.toString
39
- #
40
- # => "1B4EDAB9E028CA3C81BCD57CC4644B4C4AE239C7B713F0BCB9FAD6D562"
41
- #
42
- # Tracing context can also be picked up from a pre-existing
43
- # X-Trace string:
44
- #
45
- # xtrace = "1B4EDAB9E028CA3C81BCD57CC4644B4C4AE239C7B713F0BCB9FAD6D562"
46
- #
47
- # AppOpticsAPM::Context.fromString(xtrace)
48
- #
49
- # With these two methods, context can be passed across threads,
50
- # processes (via fork) and in requests (such as external HTTP
51
- # requests where the X-Trace is inserted in request headers).
52
- #
53
- #
54
-
55
- ###############################################################
56
- # Two Options for Spawned Tracing
57
- ###############################################################
58
- #
59
- # When your application needs to instrument code that forks,
60
- # spawns a thread or does something in-parallel, you have the
61
- # option to either link those child traces to the parent or
62
- # trace them as individuals (but with identifying information).
63
- #
64
- # Linking parent and child has it's benefits as in the
65
- # AppOptics dashboard, you will see how a process may spawn
66
- # a task in parallel and in a single view see the performance
67
- # of both.
68
- #
69
- # The limitation of this is that this is only useful if your
70
- # parent process spawns only a limited number of child traces.
71
- #
72
- # If your parent process is spawning many child tasks (e.g.
73
- # twenty, hundreds, thousands or more) it's best to trace those
74
- # child tasks as individuals and pass in identifier Key-Values
75
- # (such as task ID, job ID etc..)
76
- #
77
- # In the examples below, I show implementations of both linked
78
- # asynchronous traces and separated independent traces.
79
-
80
- ###############################################################
81
- # Thread - with separated traces
82
- ###############################################################
83
-
84
- AppOpticsAPM::API.log_start('parent')
85
-
86
- # Get the work to be done
87
- job = get_work
88
-
89
- Thread.new do
90
- # This is a new thread so there is no pre-existing context so
91
- # we'll call `AppOpticsAPM::API.log_start` to start a new trace context.
92
- AppOpticsAPM::API.log_start('worker_thread', :job_id => job.id)
93
-
94
- # Do the work
95
- do_the_work(job)
96
-
97
- AppOpticsAPM::API.log_end('worker_thread')
98
- end
99
-
100
- AppOpticsAPM::API.log_end('parent')
101
-
102
- ###############################################################
103
- #
104
- # This will generate two independent traces with the following
105
- # topology.
106
- #
107
- # 'parent'
108
- # ------------------------------------------------------------
109
- #
110
- # 'worker_thread'
111
- # ------------------------------------------------------------
112
- #
113
-
114
- ###############################################################
115
- # Thread - with linked asynchronous traces
116
- ###############################################################
117
-
118
- # Since the following example spawns a thread without waiting
119
- # for it to return, we carry over the context and we mark the
120
- # trace generated in that thread to be asynchronous using
121
- # the `Async` flag.
122
-
123
- AppOpticsAPM::API.log_start('parent')
124
-
125
- # Save the context to be imported in spawned thread
126
- tracing_context = AppOpticsAPM::Context.toString
127
-
128
- # Get the work to be done
129
- job = get_work
130
-
131
- Thread.new do
132
- # Restore context
133
- AppOpticsAPM::Context.fromString(tracing_context)
134
-
135
- AppOpticsAPM::API.log_entry('worker_thread')
136
-
137
- # Do the work
138
- do_the_work(job)
139
-
140
- AppOpticsAPM::API.log_exit('worker_thread', :Async => 1)
141
- end
142
-
143
- AppOpticsAPM::API.log_end('parent')
144
-
145
- ###############################################################
146
- #
147
- # This will generate a single trace with an asynchronous
148
- # branch like the following
149
- #
150
- # 'parent'
151
- # ------------------------------------------------------------
152
- # \
153
- # \
154
- # ------------------------------------------------------
155
- # 'worker_thread'
156
- #
157
-
158
- ###############################################################
159
- # Process via fork - with separated traces
160
- ###############################################################
161
-
162
- AppOpticsAPM::API.start_trace('parent_process') do
163
- # Get some work to process
164
- job = get_job
165
-
166
- # fork process to handle work
167
- fork do
168
- # Since fork does a complete process copy, the tracing_context still exists
169
- # so we have to clear it and start again.
170
- AppOpticsAPM::Context.clear
171
-
172
- AppOpticsAPM::API.start_trace('worker_process', nil, :job_id => job.id) do
173
- do_work(job)
174
- end
175
- end
176
-
177
- end
178
-
179
- ###############################################################
180
- #
181
- # This will generate two independent traces:
182
- #
183
- # 'parent_process'
184
- # ------------------------------------------------------------
185
- #
186
- # 'worker_process'
187
- # ------------------------------------------------------------
188
- #
189
- ###############################################################
190
- # Process via fork - with linked asynchronous traces
191
- ###############################################################
192
-
193
- AppOpticsAPM::API.start_trace('parent_process') do
194
- # Get some work to process
195
- job = get_job
196
-
197
- # fork process to handle work
198
- fork do
199
- # Since fork does a complete process copy, the tracing_context still exists
200
- # although we'll have to mark these traces as asynchronous to denote
201
- # that it has split off from the main program flow
202
-
203
- AppOpticsAPM::API.trace('worker_process', :Async => 1) do
204
- do_work(job)
205
- end
206
- end
207
- end
208
-
209
- ###############################################################
210
- #
211
- # This will generate a single trace with an asynchronous
212
- # branch like the following
213
- #
214
- # 'parent_process'
215
- # ------------------------------------------------------------
216
- # \
217
- # \
218
- # ------------------------------------------------------
219
- # 'worker_process'
220
- #