opencensus 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rubocop.yml +48 -0
- data/.travis.yml +16 -0
- data/AUTHORS +1 -0
- data/CODE_OF_CONDUCT.md +43 -0
- data/CONTRIBUTING.md +34 -0
- data/Gemfile +4 -0
- data/LICENSE +201 -0
- data/README.md +180 -0
- data/Rakefile +20 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/docs/.gitignore +3 -0
- data/docs/404.html +24 -0
- data/docs/Gemfile +31 -0
- data/docs/_config.yml +39 -0
- data/docs/_layouts/default.html +65 -0
- data/docs/index.md +151 -0
- data/lib/opencensus.rb +21 -0
- data/lib/opencensus/common.rb +24 -0
- data/lib/opencensus/common/config.rb +521 -0
- data/lib/opencensus/config.rb +54 -0
- data/lib/opencensus/context.rb +72 -0
- data/lib/opencensus/stats.rb +26 -0
- data/lib/opencensus/tags.rb +25 -0
- data/lib/opencensus/trace.rb +181 -0
- data/lib/opencensus/trace/annotation.rb +60 -0
- data/lib/opencensus/trace/config.rb +119 -0
- data/lib/opencensus/trace/exporters.rb +26 -0
- data/lib/opencensus/trace/exporters/logger.rb +149 -0
- data/lib/opencensus/trace/formatters.rb +29 -0
- data/lib/opencensus/trace/formatters/binary.rb +66 -0
- data/lib/opencensus/trace/formatters/cloud_trace.rb +102 -0
- data/lib/opencensus/trace/formatters/trace_context.rb +124 -0
- data/lib/opencensus/trace/integrations.rb +24 -0
- data/lib/opencensus/trace/integrations/faraday_middleware.rb +176 -0
- data/lib/opencensus/trace/integrations/rack_middleware.rb +127 -0
- data/lib/opencensus/trace/integrations/rails.rb +121 -0
- data/lib/opencensus/trace/link.rb +90 -0
- data/lib/opencensus/trace/message_event.rb +80 -0
- data/lib/opencensus/trace/samplers.rb +50 -0
- data/lib/opencensus/trace/samplers/always_sample.rb +34 -0
- data/lib/opencensus/trace/samplers/max_qps.rb +55 -0
- data/lib/opencensus/trace/samplers/never_sample.rb +34 -0
- data/lib/opencensus/trace/samplers/probability.rb +69 -0
- data/lib/opencensus/trace/span.rb +196 -0
- data/lib/opencensus/trace/span_builder.rb +560 -0
- data/lib/opencensus/trace/span_context.rb +308 -0
- data/lib/opencensus/trace/status.rb +49 -0
- data/lib/opencensus/trace/time_event.rb +38 -0
- data/lib/opencensus/trace/trace_context_data.rb +22 -0
- data/lib/opencensus/trace/truncatable_string.rb +61 -0
- data/lib/opencensus/version.rb +18 -0
- data/opencensus.gemspec +32 -0
- metadata +210 -0
@@ -0,0 +1,26 @@
|
|
1
|
+
# Copyright 2017 OpenCensus Authors
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
require "opencensus/trace/exporters/logger"
|
16
|
+
|
17
|
+
module OpenCensus
|
18
|
+
module Trace
|
19
|
+
##
|
20
|
+
# The Exporters module provides integrations for exporting collected trace
|
21
|
+
# spans to an external or local service.
|
22
|
+
#
|
23
|
+
module Exporters
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,149 @@
|
|
1
|
+
# Copyright 2017 OpenCensus Authors
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
require "logger"
|
16
|
+
require "json"
|
17
|
+
|
18
|
+
module OpenCensus
|
19
|
+
module Trace
|
20
|
+
module Exporters
|
21
|
+
##
|
22
|
+
# The Logger exporter exports captured spans to a standard Ruby Logger
|
23
|
+
# interface.
|
24
|
+
#
|
25
|
+
class Logger
|
26
|
+
##
|
27
|
+
# Create a new Logger exporter
|
28
|
+
#
|
29
|
+
# @param [#log] logger The logger to write to.
|
30
|
+
# @param [Symbol] level The log level. This should be a log level
|
31
|
+
# defined by {https://ruby-doc.org/stdlib-2.5.0/libdoc/logger/rdoc/Logger.html
|
32
|
+
# Logger Standard Library}.
|
33
|
+
#
|
34
|
+
def initialize logger, level: ::Logger::INFO
|
35
|
+
@logger = logger
|
36
|
+
@level = level
|
37
|
+
end
|
38
|
+
|
39
|
+
##
|
40
|
+
# Export the captured spans to the configured logger.
|
41
|
+
#
|
42
|
+
# @param [Array<Span>] spans The captured spans.
|
43
|
+
# @return [Boolean]
|
44
|
+
#
|
45
|
+
def export spans
|
46
|
+
@logger.log @level, spans.map { |span| format_span(span) }.to_json
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def format_span span
|
52
|
+
{
|
53
|
+
name: format_value(span.name),
|
54
|
+
trace_id: span.trace_id,
|
55
|
+
span_id: span.span_id,
|
56
|
+
parent_span_id: span.parent_span_id,
|
57
|
+
start_time: span.start_time,
|
58
|
+
end_time: span.end_time,
|
59
|
+
attributes: format_attributes(span.attributes),
|
60
|
+
dropped_attributes_count: span.dropped_attributes_count,
|
61
|
+
stack_trace: span.stack_trace,
|
62
|
+
dropped_frames_count: span.dropped_frames_count,
|
63
|
+
time_events: span.time_events.map { |te| format_time_event(te) },
|
64
|
+
dropped_annotations_count: span.dropped_annotations_count,
|
65
|
+
dropped_message_events_count: span.dropped_message_events_count,
|
66
|
+
links: span.links.map { |link| format_link(link) },
|
67
|
+
dropped_links_count: span.dropped_links_count,
|
68
|
+
status: format_status(span.status),
|
69
|
+
same_process_as_parent_span: span.same_process_as_parent_span,
|
70
|
+
child_span_count: span.child_span_count
|
71
|
+
}
|
72
|
+
end
|
73
|
+
|
74
|
+
def format_time_event time_event
|
75
|
+
case time_event
|
76
|
+
when Annotation
|
77
|
+
format_annotation time_event
|
78
|
+
when MessageEvent
|
79
|
+
format_message_event time_event
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def format_annotation annotation
|
84
|
+
{
|
85
|
+
description: format_value(annotation.description),
|
86
|
+
attributes: format_attributes(annotation.attributes),
|
87
|
+
dropped_attributes_count: annotation.dropped_attributes_count,
|
88
|
+
time: annotation.time
|
89
|
+
}
|
90
|
+
end
|
91
|
+
|
92
|
+
def format_message_event message_event
|
93
|
+
{
|
94
|
+
type: message_event.type,
|
95
|
+
id: message_event.id,
|
96
|
+
uncompressed_size: message_event.uncompressed_size,
|
97
|
+
compressed_size: message_event.compressed_size,
|
98
|
+
time: message_event.time
|
99
|
+
}
|
100
|
+
end
|
101
|
+
|
102
|
+
def format_link link
|
103
|
+
{
|
104
|
+
trace_id: link.trace_id,
|
105
|
+
span_id: link.span_id,
|
106
|
+
type: link.type,
|
107
|
+
attributes: format_attributes(link.attributes),
|
108
|
+
dropped_attributes_count: link.dropped_attributes_count
|
109
|
+
}
|
110
|
+
end
|
111
|
+
|
112
|
+
def format_status status
|
113
|
+
return nil if status.nil?
|
114
|
+
|
115
|
+
{
|
116
|
+
code: status.code,
|
117
|
+
message: status.message
|
118
|
+
}
|
119
|
+
end
|
120
|
+
|
121
|
+
def format_attributes attrs
|
122
|
+
result = {}
|
123
|
+
attrs.each do |k, v|
|
124
|
+
result[k] = format_value v
|
125
|
+
end
|
126
|
+
result
|
127
|
+
end
|
128
|
+
|
129
|
+
def format_value value
|
130
|
+
case value
|
131
|
+
when String, Integer, true, false
|
132
|
+
value
|
133
|
+
when TruncatableString
|
134
|
+
if value.truncated_byte_count.zero?
|
135
|
+
value.value
|
136
|
+
else
|
137
|
+
{
|
138
|
+
value: value.value,
|
139
|
+
truncated_byte_count: value.truncated_byte_count
|
140
|
+
}
|
141
|
+
end
|
142
|
+
else
|
143
|
+
nil
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# Copyright 2017 OpenCensus Authors
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
require "opencensus/trace/formatters/binary"
|
16
|
+
require "opencensus/trace/formatters/cloud_trace"
|
17
|
+
require "opencensus/trace/formatters/trace_context"
|
18
|
+
|
19
|
+
module OpenCensus
|
20
|
+
module Trace
|
21
|
+
##
|
22
|
+
# The Formatters module contains several implementations of cross-service
|
23
|
+
# SpanContext propagation. Each formatter can serialize and deserialize a
|
24
|
+
# TraceContextData instance.
|
25
|
+
#
|
26
|
+
module Formatters
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# Copyright 2017 OpenCensus Authors
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
module OpenCensus
|
16
|
+
module Trace
|
17
|
+
module Formatters
|
18
|
+
##
|
19
|
+
# This formatter serializes and deserializes span context according to
|
20
|
+
# the OpenCensus' BinaryEncoding specification. See
|
21
|
+
# [documentation](https://github.com/census-instrumentation/opencensus-specs/blob/master/encodings/BinaryEncoding.md).
|
22
|
+
#
|
23
|
+
class Binary
|
24
|
+
##
|
25
|
+
# Internal format used to (un)pack binary data
|
26
|
+
#
|
27
|
+
# @private
|
28
|
+
#
|
29
|
+
BINARY_FORMAT = "CCH32CH16CC".freeze
|
30
|
+
|
31
|
+
##
|
32
|
+
# Deserialize a trace context header into a TraceContext object.
|
33
|
+
#
|
34
|
+
# @param [String] binary
|
35
|
+
# @return [TraceContextData, nil]
|
36
|
+
#
|
37
|
+
def deserialize binary
|
38
|
+
data = binary.unpack(BINARY_FORMAT)
|
39
|
+
if data[0].zero? && data[1].zero? && data[3] == 1 && data[5] == 2
|
40
|
+
TraceContextData.new data[2], data[4], data[6]
|
41
|
+
else
|
42
|
+
nil
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
##
|
47
|
+
# Serialize a TraceContextData object.
|
48
|
+
#
|
49
|
+
# @param [TraceContextData] trace_context
|
50
|
+
# @return [String]
|
51
|
+
#
|
52
|
+
def serialize trace_context
|
53
|
+
[
|
54
|
+
0, # version
|
55
|
+
0, # field 0
|
56
|
+
trace_context.trace_id,
|
57
|
+
1, # field 1
|
58
|
+
trace_context.span_id,
|
59
|
+
2, # field 2
|
60
|
+
trace_context.trace_options
|
61
|
+
].pack(BINARY_FORMAT)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
# Copyright 2017 OpenCensus Authors
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
module OpenCensus
|
16
|
+
module Trace
|
17
|
+
module Formatters
|
18
|
+
##
|
19
|
+
# This formatter serializes and deserializes span context according to
|
20
|
+
# the Google X-Cloud-Trace header specification.
|
21
|
+
#
|
22
|
+
class CloudTrace
|
23
|
+
##
|
24
|
+
# Internal regex used to parse fields
|
25
|
+
#
|
26
|
+
# @private
|
27
|
+
#
|
28
|
+
HEADER_FORMAT = %r{([0-9a-fA-F]{32})(?:\/(\d+))?(?:;o=(\d+))?}
|
29
|
+
|
30
|
+
##
|
31
|
+
# The outgoing header used for the Google Cloud Trace header
|
32
|
+
# specification.
|
33
|
+
#
|
34
|
+
# @private
|
35
|
+
#
|
36
|
+
HEADER_NAME = "X-Cloud-Trace".freeze
|
37
|
+
|
38
|
+
##
|
39
|
+
# The rack environment header used the the Google Cloud Trace header
|
40
|
+
# specification.
|
41
|
+
#
|
42
|
+
# @private
|
43
|
+
#
|
44
|
+
RACK_HEADER_NAME = "HTTP_X_CLOUD_TRACE".freeze
|
45
|
+
|
46
|
+
##
|
47
|
+
# Returns the name of the header used for context propagation.
|
48
|
+
#
|
49
|
+
# @return [String]
|
50
|
+
#
|
51
|
+
def header_name
|
52
|
+
HEADER_NAME
|
53
|
+
end
|
54
|
+
|
55
|
+
##
|
56
|
+
# Returns the name of the rack_environment header to use when parsing
|
57
|
+
# context from an incoming request.
|
58
|
+
#
|
59
|
+
# @return [String]
|
60
|
+
#
|
61
|
+
def rack_header_name
|
62
|
+
RACK_HEADER_NAME
|
63
|
+
end
|
64
|
+
|
65
|
+
##
|
66
|
+
# Deserialize a trace context header into a TraceContext object.
|
67
|
+
#
|
68
|
+
# @param [String] header
|
69
|
+
# @return [TraceContextData, nil]
|
70
|
+
#
|
71
|
+
def deserialize header
|
72
|
+
match = HEADER_FORMAT.match(header)
|
73
|
+
if match
|
74
|
+
trace_id = match[1].downcase
|
75
|
+
span_id = format("%016x", match[2].to_i)
|
76
|
+
trace_options = match[3].to_i
|
77
|
+
TraceContextData.new trace_id, span_id, trace_options
|
78
|
+
else
|
79
|
+
nil
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
##
|
84
|
+
# Serialize a TraceContextData object.
|
85
|
+
#
|
86
|
+
# @param [TraceContextData] trace_context
|
87
|
+
# @return [String]
|
88
|
+
#
|
89
|
+
def serialize trace_context
|
90
|
+
trace_context.trace_id.dup.tap do |ret|
|
91
|
+
if trace_context.span_id
|
92
|
+
ret << "/" << trace_context.span_id.to_i(16).to_s
|
93
|
+
end
|
94
|
+
if trace_context.trace_options
|
95
|
+
ret << ";o=" << trace_context.trace_options.to_s
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
# Copyright 2017 OpenCensus Authors
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
module OpenCensus
|
16
|
+
module Trace
|
17
|
+
module Formatters
|
18
|
+
##
|
19
|
+
# This formatter serializes and deserializes span context according to
|
20
|
+
# the TraceContext specification. See
|
21
|
+
# [documentation](https://github.com/TraceContext/tracecontext-spec/blob/master/trace_context/HTTP_HEADER_FORMAT.md).
|
22
|
+
#
|
23
|
+
class TraceContext
|
24
|
+
##
|
25
|
+
# Internal regex used to identify the TraceContext version
|
26
|
+
#
|
27
|
+
# @private
|
28
|
+
#
|
29
|
+
VERSION_PATTERN = /^([0-9a-fA-F]{2})-(.+)$/
|
30
|
+
|
31
|
+
##
|
32
|
+
# Internal regex used to parse fields in version 0
|
33
|
+
#
|
34
|
+
# @private
|
35
|
+
#
|
36
|
+
HEADER_V0_PATTERN =
|
37
|
+
/^([0-9a-fA-F]{32})-([0-9a-fA-F]{16})(-([0-9a-fA-F]{2}))?$/
|
38
|
+
|
39
|
+
##
|
40
|
+
# The outgoing header used for the TraceContext header specification.
|
41
|
+
#
|
42
|
+
# @private
|
43
|
+
#
|
44
|
+
HEADER_NAME = "Trace-Context".freeze
|
45
|
+
|
46
|
+
##
|
47
|
+
# The rack environment header used for the TraceContext header
|
48
|
+
# specification
|
49
|
+
#
|
50
|
+
# @private
|
51
|
+
#
|
52
|
+
RACK_HEADER_NAME = "HTTP_TRACE_CONTEXT".freeze
|
53
|
+
|
54
|
+
##
|
55
|
+
# Returns the name of the header used for context propagation.
|
56
|
+
#
|
57
|
+
# @return [String]
|
58
|
+
#
|
59
|
+
def header_name
|
60
|
+
HEADER_NAME
|
61
|
+
end
|
62
|
+
|
63
|
+
##
|
64
|
+
# Returns the name of the rack_environment header to use when parsing
|
65
|
+
# context from an incoming request.
|
66
|
+
#
|
67
|
+
# @return [String]
|
68
|
+
#
|
69
|
+
def rack_header_name
|
70
|
+
RACK_HEADER_NAME
|
71
|
+
end
|
72
|
+
|
73
|
+
##
|
74
|
+
# Deserialize a trace context header into a TraceContext object.
|
75
|
+
#
|
76
|
+
# @param [String] header
|
77
|
+
# @return [TraceContextData, nil]
|
78
|
+
#
|
79
|
+
def deserialize header
|
80
|
+
match = VERSION_PATTERN.match(header)
|
81
|
+
if match
|
82
|
+
version = match[1].to_i(16)
|
83
|
+
version_format = match[2]
|
84
|
+
case version
|
85
|
+
when 0
|
86
|
+
parse_trace_context_header_version_0 version_format
|
87
|
+
else
|
88
|
+
nil
|
89
|
+
end
|
90
|
+
else
|
91
|
+
nil
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
##
|
96
|
+
# Serialize a TraceContextData object.
|
97
|
+
#
|
98
|
+
# @param [TraceContextData] trace_context
|
99
|
+
# @return [String]
|
100
|
+
#
|
101
|
+
def serialize trace_context
|
102
|
+
format(
|
103
|
+
"%02<version>d-%<trace_id>s-%<span_id>s-%02<trace_options>d",
|
104
|
+
version: 0, # version 0,
|
105
|
+
trace_id: trace_context.trace_id,
|
106
|
+
span_id: trace_context.span_id,
|
107
|
+
trace_options: trace_context.trace_options
|
108
|
+
)
|
109
|
+
end
|
110
|
+
|
111
|
+
private
|
112
|
+
|
113
|
+
def parse_trace_context_header_version_0 str
|
114
|
+
match = HEADER_V0_PATTERN.match(str)
|
115
|
+
if match
|
116
|
+
TraceContextData.new match[1].downcase,
|
117
|
+
match[2].downcase,
|
118
|
+
match[4].to_i(16)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|