You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Sources/Tracing/Docs.docc/Guides/ImplementATracer.md
+11-47
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
## Overview
4
4
5
-
This guide is aimed at ``Tracer`` and ``Instrument`` protocol implementation authors.
5
+
This guide is aimed at ``Tracer`` and `Instrument` protocol implementation authors.
6
6
7
7
This guide is for you if you find yourself in need of implementing your own tracing client such as Zipkin, Jaeger, X-Trace, OpenTelemetry or something similar that is custom to your company or distributed system. This guide will also complete your understanding of how distributed tracing systems actually work, so even the casual developer may find this guide useful to read through, even if not implementing your own tracers.
8
8
@@ -14,13 +14,13 @@ A tracer is-an instrument as well, and further refines it with the ability to st
14
14
15
15
## Creating an instrument
16
16
17
-
In order to implement an instrument you need to implement the ``Instrument`` protocol.
18
-
``Instrument`` is part of the `Instrumentation` library that `Tracing` depends on and offers the minimal core APIs that allow implementing instrumentation types.
17
+
In order to implement an instrument you need to implement the `Instrument` protocol.
18
+
`Instrument` is part of the `Instrumentation` library that `Tracing` depends on and offers the minimal core APIs that allow implementing instrumentation types.
19
19
20
20
`Instrument` has two requirements:
21
21
22
-
1. A ``Instrument/extract(_:into:using:)`` method, which extracts values from a generic carrier (e.g. HTTP headers) and store them into a `Baggage` instance
23
-
2. A ``Istrument/inject(_:into:using:)`` method, which takes values from the `Baggage` to inject them into a generic carrier (e.g. HTTP headers)
22
+
1. A `Instrument/extract(_:into:using:)` method, which extracts values from a generic carrier (e.g. HTTP headers) and store them into a `Baggage` instance
23
+
2. A `Instrument/inject(_:into:using:)` method, which takes values from the `Baggage` to inject them into a generic carrier (e.g. HTTP headers)
24
24
25
25
The two methods will be called by instrumented libraries/frameworks at asynchronous boundaries, giving you a chance to
26
26
act on the provided information or to add additional information to be carried across these boundaries.
> In case your library makes use of the `NIOHTTP1.HTTPHeaders` type we already have an `HTTPHeadersInjector` and
117
117
`HTTPHeadersExtractor` available as part of the `NIOInstrumentation` library.
118
118
119
-
For your library/framework to be able to carry `FIXME!!!` across asynchronous boundaries, it's crucial that you carry the context throughout your entire call chain in order to avoid dropping metadata.
119
+
For your library/framework to be able to carry `Baggage` across asynchronous boundaries, it's crucial that you carry the context throughout your entire call chain in order to avoid dropping metadata.
120
120
121
-
### Tracing your library
122
-
123
-
When your library/framework can benefit from tracing, you should make use of it by integrating the `Tracing` library.
124
-
125
-
In order to work with the tracer [configured by the end-user](#Bootstrapping-the-Instrumentation-System), it adds a property to `InstrumentationSystem` that gives you back a `Tracer`. You can then use that tracer to start `Span`s. In an HTTP client you e.g.
126
-
should start a `Span` when sending the outgoing HTTP request:
127
-
128
-
```swift
129
-
funcget(url: String, context: FIXME!!!) {
130
-
var request =HTTPRequest(url: url)
131
-
132
-
// inject the request headers into the baggage as explained above
133
-
134
-
// start a span for the outgoing request
135
-
let tracer = InstrumentationSystem.tracer
136
-
var span = tracer.startSpan(named: "HTTP GET", context: context, ofKind: .client)
137
-
138
-
// set attributes on the span
139
-
span.attributes.http.method="GET"
140
-
// ...
141
-
142
-
self.execute(request).always { _in
143
-
// set some more attributes & potentially record an error
144
-
145
-
// end the span
146
-
span.end()
147
-
}
148
-
}
149
-
```
150
-
151
-
> ⚠️ Make sure to ALWAYS end spans to ensure that all paths taken by the code will result in ending the span.
152
-
> Make sure that error cases also set the error attribute and end the span.
153
-
154
-
> In the above example we used the semantic `http.method` attribute that gets exposed via the
155
-
`TracingOpenTelemetrySupport` library.
156
-
157
-
## Instrument developers: Creating an instrument
121
+
## Creating an instrument
158
122
159
123
Creating an instrument means adopting the `Instrument` protocol (or `Tracer` in case you develop a tracer).
160
124
`Instrument` is part of the `Instrumentation` library & `Tracing` contains the `Tracer` protocol.
161
125
162
126
`Instrument` has two requirements:
163
127
164
-
1. A method to inject values inside a `FIXME!!!` into a generic carrier (e.g. HTTP headers)
165
-
2. A method to extract values from a generic carrier (e.g. HTTP headers) and store them in a `FIXME!!!`
128
+
1. A method to inject values inside a `Baggage` into a generic carrier (e.g. HTTP headers)
129
+
2. A method to extract values from a generic carrier (e.g. HTTP headers) and store them in a `Baggage`
166
130
167
131
The two methods will be called by instrumented libraries/frameworks at asynchronous boundaries, giving you a chance to
168
132
act on the provided information or to add additional information to be carried across these boundaries.
169
133
170
134
> Check out the [`Baggage` documentation](https://github.com/apple/swift-distributed-tracing-baggage) for more information on
171
-
how to retrieve values from the `FIXME!!!` and how to set values on it.
135
+
how to retrieve values from the `Baggage` and how to set values on it.
172
136
173
137
### Creating a `Tracer`
174
138
@@ -199,7 +163,7 @@ extension Baggage {
199
163
}
200
164
}
201
165
202
-
var context =DefaultFIXME!!!.topLevel(logger: ...)
0 commit comments