通過(guò)zipkin的表結(jié)構(gòu),理解dapper者冤;trace把多個(gè)span進(jìn)行串接肤视;
形成依賴鏈路。
結(jié)構(gòu)
zipkin主要包括:collector涉枫、storage邢滑、search、webui愿汰;
zipkin collector會(huì)對(duì)一個(gè)到來(lái)的被trace的數(shù)據(jù)(span)進(jìn)行驗(yàn)證困后、存儲(chǔ)并設(shè)置索引。
其中storage包括:內(nèi)存衬廷、mysql摇予、es、cassandra吗跋。
數(shù)據(jù)結(jié)構(gòu)
Annotation:用于定位一個(gè)request的開(kāi)始和結(jié)束侧戴,cs/sr/ss/cr含有額外的信息,比如說(shuō)時(shí)間點(diǎn),當(dāng)這個(gè)annotation被記錄了救鲤,這個(gè)RPC也被認(rèn)為完成了。
cs:Client Start,表示客戶端發(fā)起請(qǐng)求 秩冈;一個(gè)span的開(kāi)始本缠;
cr:Client Received,表示客戶端獲取到服務(wù)端返回信息;一個(gè)span的結(jié)束
sr:Server Receive,表示服務(wù)端收到請(qǐng)求
ss:Server Send,表示服務(wù)端完成處理入问,并將結(jié)果發(fā)送給客戶端
sr-cs:網(wǎng)絡(luò)延遲
ss-sr:邏輯處理時(shí)間
cr-cs:整個(gè)流程時(shí)間
Span:一個(gè)請(qǐng)求(包含一組Annotation和BinaryAnnotation)丹锹;它是基本工作單元,一次鏈路調(diào)用(可以是RPC芬失,DB等沒(méi)有特定的限制)創(chuàng)建一個(gè)span楣黍,通過(guò)一個(gè)64位ID標(biāo)識(shí)它。span通過(guò)還有其他的數(shù)據(jù)棱烂,例如描述信息租漂,時(shí)間戳,key-value對(duì)的(Annotation)tag信息颊糜,parent-id等,其中parent-id 可以表示span調(diào)用鏈路來(lái)源哩治,通俗的理解span就是一次請(qǐng)求信息。
Trace:類似于樹(shù)結(jié)構(gòu)的Span集合衬鱼,表示一條調(diào)用鏈路业筏,存在唯一標(biāo)識(shí)
Traces are built by collecting all Spans that share a traceId。通過(guò)traceId鸟赫、spanId和parentId蒜胖,被收集到的span會(huì)匯聚成一個(gè)tree,從而提供出一個(gè)request的整體流程抛蚤。
流程圖
表設(shè)計(jì)
1.span表
CREATE TABLE IF NOT EXISTS zipkin_spans (
`trace_id` BIGINT NOT NULL,
`id` BIGINT NOT NULL,
`name` VARCHAR(255) NOT NULL,
`parent_id` BIGINT,
`debug` BIT(1),
`start_ts` BIGINT
COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement
TTL',
`duration` BIGINT
COMMENT 'Span.duration(): micros used for minDuration and maxDuration query'
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED;
一些約束
ALTER TABLE zipkin_spans ADD UNIQUE KEY(`trace_id`, `id`)
COMMENT 'ignore insert on duplicate';
ALTER TABLE zipkin_spans ADD INDEX(`trace_id`, `id`)
COMMENT 'for joining with zipkin_annotations';
ALTER TABLE zipkin_spans ADD INDEX(`trace_id`)
COMMENT 'for getTracesByIds';
ALTER TABLE zipkin_spans ADD INDEX(`name`)
COMMENT 'for getTraces and getSpanNames';
ALTER TABLE zipkin_spans ADD INDEX(`start_ts`)
COMMENT 'for getTraces ordering and range';
CREATE TABLE IF NOT EXISTS zipkin_annotations (
`trace_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.trace_id',
`span_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.id',
`a_key` VARCHAR(255) NOT NULL
COMMENT 'BinaryAnnotation.key or Annotation.value if type == -1',
`a_value` BLOB
COMMENT 'BinaryAnnotation.value(), which must be smaller than 64KB',
`a_type` INT NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation',
`a_timestamp` BIGINT
COMMENT 'Used to implement TTL; Annotation.timestamp or
zipkin_spans.timestamp',
`endpoint_ipv4` INT COMMENT 'Null when Binary/Annotation.endpoint is null',
`endpoint_ipv6` BINARY(16)
COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address',
`endpoint_port` SMALLINT COMMENT 'Null when Binary/Annotation.endpoint is null',
`endpoint_service_name` VARCHAR(255) COMMENT 'Null when
Binary/Annotation.endpoint is null'
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED;
ALTER TABLE zipkin_annotations ADD UNIQUE KEY(`trace_id`, `span_id`, `a_key`, `a_timestamp`)
COMMENT 'Ignore insert on duplicate';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id`, `span_id`)
COMMENT 'for joining with zipkin_spans';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id`) COMMENT 'for getTraces/ByIds';
ALTER TABLE zipkin_annotations ADD INDEX(`endpoint_service_name`)
COMMENT 'for getTraces and getServiceNames';
ALTER TABLE zipkin_annotations ADD INDEX(`a_type`) COMMENT 'for getTraces';
ALTER TABLE zipkin_annotations ADD INDEX(`a_key`) COMMENT 'for getTraces';
3.依賴關(guān)系
CREATE TABLE IF NOT EXISTS zipkin_dependencies (
`day` DATE NOT NULL,
`parent` VARCHAR(255) NOT NULL,
`child` VARCHAR(255) NOT NULL,
`call_count` BIGINT
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED;
ALTER TABLE zipkin_dependencies ADD UNIQUE KEY(`day`, `parent`, `child`);