-
Notifications
You must be signed in to change notification settings - Fork 3
/
README.md
646 lines (509 loc) · 17.4 KB
/
README.md
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
638
639
640
641
642
643
644
645
646
# Tracing for Tarantool
![tracing-img](https://user-images.githubusercontent.com/8830475/68295738-17abe800-00a4-11ea-855f-d46589b89bed.png)
`Tracing` module for Tarantool includes the following parts:
* OpenTracing API
* Zipkin tracer
## Table of contents
* [OpenTracing](#opentracing)
* [Required Reading](#required-reading)
* [Conventions](#conventions)
* [Span](#span)
* [SpanContext](#spancontext)
* [Tracer](#tracer)
* [Basic usage](#basic-usage)
* [Zipkin](#zipkin)
* [Basic usage](#basic-usage)
* [Examples](#examples)
* [HTTP](#http)
* [Cartridge](#tarantool-cartridge)
## OpenTracing
This library is a Tarantool platform API for OpenTracing.
### Required Reading
To fully understand this platform API,
it's helpful to be familiar with the [OpenTracing project](https://opentracing.io) and
[terminology](https://opentracing.io/specification/) more specifically.
### Conventions
- All timestamps are in microseconds
### Span
> The “span” is the primary building block of a distributed trace,
representing an individual unit of work done in a distributed system.
Traces in OpenTracing are defined implicitly by their Spans.
In particular, a Trace can be thought of as a directed acyclic graph
(DAG) of Spans, where the edges between Spans are called References.
```lua
local opentracing_span = require('opentracing.span')
-- tracer - External tracer
-- context - Span context
-- name - Name of span
-- start_timestamp (optional) - Time of span's start in microseconds (by default current time)
local span = opentracing_span.new(tracer, context, name, start_timestamp)
```
### SpanContext
> The SpanContext carries data across process boundaries.
```lua
local opentracing_span_context = require('opentracing.span_context')
-- trace_id (optional) - Trace ID (by default generates automatically)
-- span_id (optional) - Span ID (by default generates automatically)
-- parent_id (optional) - Span ID of parent span (by default is empty)
-- should_sample (optional) - Flag is enable collecting data of this span (by default false)
-- baggage (optional) - Table with trace baggage (by default is empty table)
local context = opentracing_span_context.new({
tracer_id = trace_id,
span_id = span_id,
parent_id = parent_id,
should_sample = should_sample,
baggage = baggage,
})
```
### Tracer
> The Tracer interface creates Spans and understands
how to Inject (serialize) and Extract (deserialize)
their metadata across process boundaries.
An interface for custom tracers
```lua
local opentracing_tracer = require('opentracing.tracer')
-- reporter (optional) - Table with `report` method to process finished spans (by default no-op table)
-- sampler (optional) - Table with `sample` method to select traces to send to distributing tracing system (by default random selection)
-- But you can implement your own sampler with appropriate sampling strategy
-- For more information see: https://www.jaegertracing.io/docs/1.11/sampling/
local tracer = opentracing_tracer.new(reporter, sampler)
```
### Basic usage
```lua
local zipkin = require('zipkin.tracer')
local opentracing = require('opentracing')
-- Create client to Zipkin and set it global for easy access from any part of app
local tracer = zipkin.new(config)
opentracing.set_global_tracer(tracer)
-- Create and manage spans manually
local span = opentracing.start_span('root span')
-- ... your code ...
span:finish()
-- Simple wrappers via user's function
-- Creates span before function call and finishes it after
local result = opentracing.trace('one span', func, ...)
-- Wrappers with context passing
local span = opentracing.start_span('root span')
-- Pass your function as third argument and then its arguments
opentracing.trace_with_context('child span 1', span:context(), func1, ...)
opentracing.trace_with_context('child span 2', span:context(), func2, ...)
span:finish()
```
## Zipkin
[Zipkin](https://zipkin.io/) is a distributed tracing system.
It helps gather timing data needed to troubleshoot latency problems in microservice architectures.
It manages both the collection and lookup of this data.
This module allows you to instance Zipkin Tracer that can start spans and
will report collected spans to Zipkin Server.
### Basic usage
```lua
local zipkin = require('zipkin.tracer')
-- First argument is config that contains url of Zipkin API,
-- method to send collected traces and interval of reports in seconds
-- Second optional argument is Sampler (see OpenTracing API description), by default random sampler
local tracer = zipkin.new({
base_url = 'localhost:9411/api/v2/spans',
api_method = 'POST',
report_interval = 0,
}, Sampler)
local span = tracer:start_span('example')
-- ...
span:finish()
```
## Examples
### HTTP
![http-example-img](https://user-images.githubusercontent.com/8830475/68296113-de27ac80-00a4-11ea-844a-20f798d3f5d8.png)
This example is a Lua port of
[Go OpenTracing tutorial](https://github.com/yurishkuro/opentracing-tutorial/tree/master/go).
*Complete source code see [here](/examples/http)*
#### Description
The example demonstrates trace propagation through two services:
`formatter` that formats the source string to "Hello, world"
and `publisher` that prints it in the console.
Add data to these services via HTTP; initially it sends `client`.
*Note: example requires http rock (version >= 2.0.1)*
*Install it using `tarantoolctl rocks install http 2.0.1`*
#### How to run
* Create `docker-compose.zipkin.yml`
```yaml
---
version: '3.5'
# Initially got from https://github.com/openzipkin/docker-zipkin/blob/master/docker-compose.yml
services:
storage:
image: openzipkin/zipkin-mysql:1
container_name: mysql
networks:
- zipkin
ports:
- 3306:3306
# The zipkin process services the UI, and also exposes a POST endpoint that
# instrumentation can send trace data to. Scribe is disabled by default.
zipkin:
image: openzipkin/zipkin:1
container_name: zipkin
networks:
- zipkin
# Environment settings are defined here https://github.com/openzipkin/zipkin/tree/1.19.0/zipkin-server#environment-variables
environment:
- STORAGE_TYPE=mysql
# Point the zipkin at the storage backend
- MYSQL_HOST=mysql
# Enable debug logging
- JAVA_OPTS=-Dlogging.level.zipkin=DEBUG -Dlogging.level.zipkin2=DEBUG
ports:
# Port used for the Zipkin UI and HTTP Api
- 9411:9411
depends_on:
- storage
# Adds a cron to process spans since midnight every hour, and all spans each day
# This data is served by http://192.168.99.100:8080/dependency
#
# For more details, see https://github.com/openzipkin/docker-zipkin-dependencies
dependencies:
image: openzipkin/zipkin-dependencies:1
container_name: dependencies
entrypoint: crond -f
networks:
- zipkin
environment:
- STORAGE_TYPE=mysql
- MYSQL_HOST=mysql
# Add the baked-in username and password for the zipkin-mysql image
- MYSQL_USER=zipkin
- MYSQL_PASS=zipkin
# Dependency processing logs
- ZIPKIN_LOG_LEVEL=DEBUG
depends_on:
- storage
networks:
zipkin:
```
* Start Zipkin `docker-compose -f docker-compose.zipkin.yml up`
* Run mock applications from separate consoles: consumer, formatter and client
Formatter HTTP server
```lua
#!/usr/bin/env tarantool
local http_server = require('http.server')
local http_router = require('http.router')
local fiber = require('fiber')
local log = require('log')
local zipkin = require('zipkin.tracer')
local opentracing = require('opentracing')
local app = {}
local Sampler = {
sample = function() return true end,
}
local HOST = '0.0.0.0'
local PORT = '33302'
local function handler(req)
-- Extract content from request's http headers
local ctx, err = opentracing.http_extract(req:headers())
if ctx == nil then
local resp = req:render({ text = err })
resp.status = 400
return resp
end
local hello_to = req:query_param('helloto')
-- Start new child span
local span = opentracing.start_span_from_context(ctx, 'format_string')
-- Set service type
span:set_component('formatter')
span:set_server_kind()
span:set_http_method(req:method())
span:set_http_path(req:path())
local greeting = span:get_baggage_item('greeting')
local result = ('%s, %s!'):format(greeting, hello_to)
local resp = req:render({ text = result })
-- Simulate long request processing
fiber.sleep(2)
span:log_kv({
event = 'String format',
value = result,
})
resp.status = 200
span:set_http_status_code(resp.status)
span:finish()
return resp
end
function app.init()
-- Initialize zipkin client that will be send spans every 5 seconds
local tracer = zipkin.new({
base_url = 'localhost:9411/api/v2/spans',
api_method = 'POST',
report_interval = 5,
on_error = function(err) log.error(err) end,
}, Sampler)
opentracing.set_global_tracer(tracer)
local httpd = http_server.new(HOST, PORT)
local router = http_router.new()
:route({ path = '/format', method = 'GET' }, handler)
httpd:set_router(router)
httpd:start()
end
app.init()
return app
```
Publisher HTTP server
```lua
#!/usr/bin/env tarantool
local http_server = require('http.server')
local http_router = require('http.router')
local fiber = require('fiber')
local log = require('log')
local zipkin = require('zipkin.tracer')
local opentracing = require('opentracing')
local app = {}
local Sampler = {
sample = function() return true end,
}
local HOST = '0.0.0.0'
local PORT = '33303'
local function handler(req)
local ctx, err = opentracing.http_extract(req:headers())
if ctx == nil then
local resp = req:render({ text = err })
resp.status = 400
return resp
end
local hello = req:query_param('hello')
local span = opentracing.start_span_from_context(ctx, 'print_string')
span:set_component('publisher')
span:set_server_kind()
span:set_http_method(req:method())
span:set_http_path(req:path())
-- Simulate long request processing
fiber.sleep(3)
io.write(hello, '\n')
local resp = req:render({text = '' })
resp.status = 200
span:set_http_status_code(resp.status)
span:finish()
return resp
end
function app.init()
local tracer = zipkin.new({
base_url = 'localhost:9411/api/v2/spans',
api_method = 'POST',
report_interval = 5,
on_error = function(err) log.error(err) end,
}, Sampler)
opentracing.set_global_tracer(tracer)
local httpd = http_server.new(HOST, PORT)
local router = http_router.new()
:route({ path = '/print', method = 'GET' }, handler)
httpd:set_router(router)
httpd:start()
end
app.init()
return app
```
Client
```lua
#!/usr/bin/env tarantool
local http_client = require('http.client')
local json = require('json')
local log = require('log')
local fiber = require('fiber')
local zipkin = require('zipkin.tracer')
local opentracing = require('opentracing')
local app = {}
-- Process all requests
local Sampler = {
sample = function() return true end,
}
local function url_encode(str)
local res = string.gsub(str, '[^a-zA-Z0-9_]',
function(c)
return string.format('%%%02X', string.byte(c))
end
)
return res
end
-- Client part to formatter
local formatter_url = 'http://localhost:33302/format'
local function format_string(ctx, str)
local span = opentracing.start_span_from_context(ctx, 'format_string')
local httpc = http_client.new()
span:set_component('client')
span:set_client_kind()
span:set_http_method('GET')
span:set_http_url(formatter_url)
-- Use http headers as carrier
local headers = {
['content-type'] = 'application/json'
}
opentracing.http_inject(span:context(), headers)
-- Simulate problems with network
fiber.sleep(1)
local resp = httpc:get(formatter_url .. '?helloto=' .. url_encode(str),
{ headers = headers })
fiber.sleep(1)
span:set_http_status_code(resp.status)
if resp.status ~= 200 then
error('Format string error: ' .. json.encode(resp))
end
local result = resp.body
-- Log result
span:log_kv({
event = 'String format',
value = result
})
span:finish()
return result
end
-- Client part to publisher
local printer_url = 'http://localhost:33303/print'
local function print_string(ctx, str)
local span = opentracing.start_span_from_context(ctx, 'print_string')
local httpc = http_client.new()
span:set_component('client')
span:set_client_kind()
span:set_http_method('GET')
span:set_http_url(printer_url)
local headers = {
['content-type'] = 'application/json'
}
opentracing.http_inject(span:context(), headers)
-- Simulate problems with network
fiber.sleep(1)
local resp = httpc:get(printer_url .. '?hello=' .. url_encode(str),
{ headers = headers })
fiber.sleep(1)
span:set_http_status_code(resp.status)
if resp.status ~= 200 then
error('Print string error: ' .. json.encode(resp))
end
span:finish()
end
function app.init()
-- Initialize Zipkin tracer
local tracer = zipkin.new({
base_url = 'localhost:9411/api/v2/spans',
api_method = 'POST',
report_interval = 0,
on_error = function(err) log.error(err) end,
}, Sampler)
opentracing.set_global_tracer(tracer)
-- Initialize root span
local span = opentracing.start_span('Hello-world')
local hello_to = 'world'
local greeting = 'my greeting'
span:set_component('client')
-- Set service type
span:set_client_kind()
-- Set tag with metadata
span:set_tag('hello-to', hello_to)
-- Add data to baggage
span:set_baggage_item('greeting', greeting)
local ctx = span:context()
local formatted_string = format_string(ctx, hello_to)
print_string(ctx, formatted_string)
span:finish()
end
app.init()
os.exit(0)
```
* Check results on [http://localhost:9411/zipkin](http://localhost:9411/zipkin)
### Tarantool Cartridge
![cartridge-example-img](https://user-images.githubusercontent.com/8830475/68297520-2e543e00-00a8-11ea-9517-f9567dc3c808.png)
*Complete source code see [here](/examples/cartridge)*
Opentracing could be used with [Tarantool Cartridge](https://github.com/tarantool/cartridge).
This example is pretty similar to previous. We will have several roles
that communicate via rpc_call.
#### Basics
Before describing let's define some restrictions of "tracing in Tarantool".
Remote communications between tarantools are made using `net.box` module.
It allows to send only primitive types (except functions) and doesn't have
containers for request context (as headers in HTTP).
Then you should transfer span context explicitly as raw table as additional argument
in your function.
```lua
-- Create span
local span = opentracing.start_span('span')
-- Create context carrier
local rpc_context = {}
opentracing.map_inject(span:context(), rpc_context)
-- Pass context explicitly as additional argument
local res, err = cartridge.rpc_call('role', 'fun', {rpc_context, ...})
```
#### Using inside roles
The logic of tracing fits into a separate permanent role.
Let's define it:
```lua
local opentracing = require('opentracing')
local zipkin = require('zipkin.tracer')
local log = require('log')
-- config = {
-- base_url = 'localhost:9411/api/v2/spans',
-- api_method = 'POST',
-- report_interval = 5, -- in seconds
-- spans_limit = 1e4, -- amount of spans that could be stored locally
-- }
local function apply_config(config)
-- sample all requests
local sampler = { sample = function() return true end }
local tracer = zipkin.new({
base_url = config.base_url,
api_method = config.api_method,
report_interval = config.report_interval,
spans_limit = config.spans_limit,
on_error = function(err) log.error('zipkin error: %s', err) end,
}, sampler)
-- Setup global tracer for easy access from another modules
opentracing.set_global_tracer(tracer)
return true
end
return {
role_name = 'tracing',
apply_config = apply_config,
dependencies = {},
-- Role will be hidden from WebUI
-- but constantly enabled on all instances,
-- no need to specify it as dependency for other roles
permanent = true,
}
```
Then you can use this role as dependency:
```lua
local opentracing = require('opentracing')
local membership = require('membership')
local role_name = 'formatter'
local template = 'Hello, %s'
local service_uri = ('%s@%s'):format(role_name, membership.myself().uri)
local function format(ctx, input)
-- Extract tracing context from request context
local context = opentracing.map_extract(ctx)
local span = opentracing.start_span_from_context(context, 'format')
span:set_component(service_uri)
local result, err
if input == '' then
err = 'Empty string'
span:set_error(err)
else
result = template:format(input)
end
span:finish()
return result, err
end
local function init(_)
return true
end
local function stop()
end
local function validate_config(_, _)
return true
end
local function apply_config(_, _)
return true
end
return {
format = format,
role_name = role_name,
init = init,
stop = stop,
validate_config = validate_config,
apply_config = apply_config,
dependencies = {},
}
```