22
33<details ><summary >Table of Contents</summary >
44
5+ * [ Sampling] ( #sampling )
56* [ Tracer Creation] ( #tracer-creation )
67* [ Span Processor] ( #span-processor )
78* [ Span Exporter] ( #span-exporter )
89
910</details >
1011
11- # Tracer Creation
12+ ## Sampling
13+
14+ Sampling is a mechanism to control the noise and overhead introduced by
15+ OpenTelemetry by reducing the number of samples of traces collected and sent to
16+ the backend.
17+
18+ Sampling may be implemented on different stages of a trace collection.
19+ OpenTelemetry API defines a ` Sampler ` interface that can be used at
20+ instrumentation points by libraries to check the sampling ` Decision ` early and
21+ optimize the amount of telemetry that needs to be collected.
22+
23+ All other sampling algorithms may be implemented on SDK layer in exporters, or
24+ even out of process in Agent or Collector.
25+
26+ API defines two interfaces - [ ` Sampler ` ] ( #sampler ) and [ ` Decision ` ] ( #decision )
27+ as well as a set of [ built-in samplers] ( #built-in-samplers ) .
28+
29+ ### Sampler
30+
31+ ` Sampler ` interface allows to create custom samplers which will return a
32+ sampling ` Decision ` based on information that is typically available just before
33+ the ` Span ` was created.
34+
35+ #### ShouldSample
36+
37+ Returns the sampling Decision for a ` Span ` to be created.
38+
39+ ** Required arguments:**
40+
41+ - ` SpanContext ` of a parent ` Span ` . Typically extracted from the wire. Can be
42+ ` null ` .
43+ - ` TraceId ` of the ` Span ` to be created. It can be different from the ` TraceId `
44+ in the ` SpanContext ` . Typically in situations when the ` Span ` to be created
45+ starts a new Trace.
46+ - ` SpanId ` of the ` Span ` to be created.
47+ - Name of the ` Span ` to be created.
48+ - Collection of links that will be associated with the ` Span ` to be created.
49+ Typically useful for batch operations.
50+
51+ ** Return value:**
52+
53+ Sampling ` Decision ` whether span should be sampled or not.
54+
55+ #### GetDescription
56+
57+ Returns the sampler name or short description with the configuration. This may
58+ be displayed on debug pages or in the logs. Example:
59+ ` "ProbabilitySampler{0.000100}" ` .
60+
61+ Description MUST NOT change over time and caller can cache the returned value.
62+
63+ ### Decision
64+
65+ ` Decision ` is an interface with two getters describing the sampling decision.
66+
67+ #### IsSampled
68+
69+ Return sampling decision whether span should be sampled or not. ` True ` value of
70+ ` IsSampled ` flag means that Span information needs to be recorded.
71+
72+ #### GetAttributes
73+
74+ Return attributes to be attached to the ` Span ` . These attributes should be added
75+ to the ` Span ` only for root span or when sampling decision ` IsSampled ` changes
76+ from false to true.
77+
78+ Examples of attribute may be algorithm used to make a decision and sampling
79+ priority. Another example may be recording the reason trace was marked as
80+ "important" to sample in. For instance, when traces from specific user session
81+ should be collected, session identifier can be added to attributes.
82+
83+ The list of attributes returned by ` Decision ` MUST be immutable. Caller may call
84+ this method any number of times and can safely cache the returned value.
85+
86+ ### Built-in samplers
87+
88+ API MUST provide a way to create the following built-in samplers:
89+
90+ - Always sample. ` Sampler ` returns ` Decision ` with ` IsSampled=true ` and empty
91+ arguments collection. Description MUST be ` AlwaysSampleSampler ` .
92+ - Never sample. ` Sampler ` returns ` Decision ` with ` IsSampled=false ` and empty
93+ arguments collection. Description MUST be ` NeverSampleSampler ` .
94+
95+ ## Tracer Creation
1296
1397New ` Tracer ` instances are always created through a ` TracerFactory ` (see [ API] ( api-tracing.md#obtaining-a-tracer ) ).
1498The ` name ` and ` version ` arguments supplied to the ` TracerFactory ` must be used
@@ -17,7 +101,7 @@ The readable representations of all `Span` instances created by a `Tracer` must
17101provide a ` getLibraryResource ` method that returns this ` Resource ` information
18102held by the ` Tracer ` .
19103
20- ## Span processor
104+ ### Span processor
21105
22106Span processor is an interface which allows hooks for span start and end method
23107invocations. The span processors are invoked only when
@@ -46,9 +130,9 @@ in the SDK:
46130 +-----+---------------+ +---------------------+
47131```
48132
49- ### Interface definition
133+ #### Interface definition
50134
51- #### OnStart(Span)
135+ ##### OnStart(Span)
52136
53137` OnStart ` is called when a span is started. This method is called synchronously
54138on the thread that started the span, therefore it should not block or throw
@@ -60,7 +144,7 @@ exceptions.
60144
61145** Returns:** ` Void `
62146
63- #### OnEnd(Span)
147+ ##### OnEnd(Span)
64148
65149` OnEnd ` is called when a span is ended. This method is called synchronously on
66150the execution thread, therefore it should not block or throw an exception.
@@ -71,7 +155,7 @@ the execution thread, therefore it should not block or throw an exception.
71155
72156** Returns:** ` Void `
73157
74- #### Shutdown()
158+ ##### Shutdown()
75159
76160Shuts down the processor. Called when SDK is shut down. This is an opportunity
77161for processor to do any cleanup required.
@@ -82,9 +166,9 @@ call to shutdown subsequent calls to `onStart` or `onEnd` are not allowed.
82166Shutdown should not block indefinitely. Language library authors can decide if
83167they want to make the shutdown timeout to be configurable.
84168
85- ### Built-in span processors
169+ #### Built-in span processors
86170
87- #### Simple processor
171+ ##### Simple processor
88172
89173The implementation of ` SpanProcessor ` that passes ended span directly to the
90174configured ` SpanExporter ` .
@@ -93,7 +177,7 @@ configured `SpanExporter`.
93177
94178* ` exporter ` - the exporter where the spans are pushed.
95179
96- #### Batching processor
180+ ##### Batching processor
97181
98182The implementation of the ` SpanProcessor ` that batches ended spans and pushes
99183them to the configured ` SpanExporter ` .
@@ -113,7 +197,7 @@ high contention in a very high traffic service.
113197* ` maxExportBatchSize ` - the maximum batch size of every export. It must be
114198 smaller or equal to ` maxQueueSize ` . The default value is ` 512 ` .
115199
116- ## Span Exporter
200+ ### Span Exporter
117201
118202` Span Exporter ` defines the interface that protocol-specific exporters must
119203implement so that they can be plugged into OpenTelemetry SDK and support sending
@@ -129,14 +213,14 @@ The goals of the interface are:
129213 functionality such as queuing, batching, tagging, etc. as helpers. This
130214 functionality will be applicable regardless of what protocol exporter is used.
131215
132- ### Interface Definition
216+ #### Interface Definition
133217
134218The exporter must support two functions: ** Export** and ** Shutdown** . In
135219strongly typed languages typically there will be 2 separate ` Exporter `
136220interfaces, one that accepts spans (SpanExporter) and one that accepts metrics
137221(MetricsExporter).
138222
139- #### ` Export(batch) `
223+ ##### ` Export(batch) `
140224
141225Exports a batch of telemetry data. Protocol exporters that will implement this
142226function are typically expected to serialize and transmit the data to the
@@ -176,7 +260,7 @@ ExportResult is one of:
176260 example can happen when the destination is unavailable, there is a network
177261 error or endpoint does not exist.
178262
179- #### ` Shutdown() `
263+ ##### ` Shutdown() `
180264
181265Shuts down the exporter. Called when SDK is shut down. This is an opportunity
182266for exporter to do any cleanup required.
@@ -189,7 +273,7 @@ return FailedNotRetryable error.
189273and the destination is unavailable). Language library authors can decide if they
190274want to make the shutdown timeout to be configurable.
191275
192- ### Further Language Specialization
276+ #### Further Language Specialization
193277
194278Based on the generic interface definition laid out above library authors must
195279define the exact interface for the particular language.
@@ -204,14 +288,14 @@ allocations and use allocation arenas where possible, thus avoiding explosion of
204288allocation/deallocation/collection operations in the presence of high rate of
205289telemetry data generation.
206290
207- #### Examples
291+ ##### Examples
208292
209293These are examples on what the ` Exporter ` interface can look like in specific
210294languages. Examples are for illustration purposes only. Language library authors
211295are free to deviate from these provided that their design remain true to the
212296spirit of ` Exporter ` concept.
213297
214- ##### Go SpanExporter Interface
298+ ###### Go SpanExporter Interface
215299
216300``` go
217301type SpanExporter interface {
0 commit comments