@@ -11,6 +11,13 @@ cSpell:ignore: decltype labelkv nostd nullptr
11
11
12
12
{{% docs/languages/instrumentation-intro %}}
13
13
14
+ {{% alert title="Note" color="primary" %}}
15
+
16
+ OpenTelemetry C++ doesn't support automatic instrumentation when the source code
17
+ of the library you want to instrument isn't available.
18
+
19
+ {{% /alert %}}
20
+
14
21
## Setup
15
22
16
23
Follow the instructions in the
@@ -19,7 +26,7 @@ OpenTelemetry C++.
19
26
20
27
## Traces
21
28
22
- ### Initializing tracing
29
+ ### Initialize tracing
23
30
24
31
``` cpp
25
32
auto provider = opentelemetry::trace::Provider::GetTracerProvider();
@@ -57,7 +64,7 @@ The concept of an active span is important, as any span that is created without
57
64
explicitly specifying a parent is parented to the currently active span. A span
58
65
without a parent is called root span.
59
66
60
- ### Create nested Spans
67
+ ### Create nested spans
61
68
62
69
```cpp
63
70
auto outer_span = tracer->StartSpan("Outer operation");
@@ -76,7 +83,7 @@ Spans can be nested, and have a parent-child relationship with other spans. When
76
83
a given span is active, the newly created span inherits the active span’s trace
77
84
ID, and other context attributes.
78
85
79
- ### Context Propagation
86
+ ### Context propagation
80
87
81
88
``` cpp
82
89
// set global propagator
@@ -105,18 +112,18 @@ distributed tracing to transfer this Context across service boundary often
105
112
through HTTP headers. OpenTelemetry provides a text-based approach to propagate
106
113
context to remote services using the W3C Trace Context HTTP headers.
107
114
108
- ### Further Reading
115
+ ### Further reading
109
116
110
117
- [Traces API](https://opentelemetry-cpp.readthedocs.io/en/latest/otel_docs/namespace_opentelemetry__trace.html)
111
118
- [Traces SDK](https://opentelemetry-cpp.readthedocs.io/en/latest/otel_docs/namespace_opentelemetry__sdk__trace.html)
112
119
- [Simple Metrics Example](https://github.com/open-telemetry/opentelemetry-cpp/tree/main/examples/metrics_simple)
113
120
114
121
## Metrics
115
122
116
- ### Initialize Exporter and Reader
123
+ ### Initialize exporter and reader
117
124
118
- Initialize an exporter and a reader. In this case, we initialize an OStream
119
- Exporter which will print to stdout by default. The reader periodically collects
125
+ Initialize an exporter and a reader. In this case, you initialize an OStream
126
+ Exporter which prints to stdout by default. The reader periodically collects
120
127
metrics from the Aggregation Store and exports them.
121
128
122
129
```cpp
@@ -125,23 +132,23 @@ std::unique_ptr<opentelemetry::sdk::metrics::MetricReader> reader{
125
132
new opentelemetry::sdk::metrics::PeriodicExportingMetricReader(std::move(exporter), options)};
126
133
```
127
134
128
- ### Initialize Meter Provider
135
+ ### Initialize a meter provider
129
136
130
- Initialize a MeterProvider and add the reader. We will use this to obtain Meter
131
- objects in the future.
137
+ Initialize a MeterProvider and add the reader. Use this to obtain Meter objects
138
+ in the future.
132
139
133
140
``` cpp
134
141
auto provider = std::shared_ptr<opentelemetry::metrics::MeterProvider>(new opentelemetry::sdk::metrics::MeterProvider());
135
142
auto p = std::static_pointer_cast<opentelemetry::sdk::metrics::MeterProvider>(provider);
136
143
p->AddMetricReader (std::move(reader));
137
144
```
138
145
139
- ### Create a Counter
146
+ ### Create a counter
140
147
141
148
Create a Counter instrument from the Meter, and record the measurement. Every
142
149
Meter pointer returned by the MeterProvider points to the same Meter. This means
143
- that the Meter will be able to combine metrics captured from different functions
144
- without having to constantly pass the Meter around the library.
150
+ that the Meter can combine metrics captured from different functions without
151
+ having to constantly pass the Meter around the library.
145
152
146
153
```cpp
147
154
auto meter = provider->GetMeter(name, "1.2.0");
@@ -152,36 +159,36 @@ auto labelkv = common::KeyValueIterableView<decltype(labels)>{labels};
152
159
double_counter->Add(val, labelkv);
153
160
```
154
161
155
- ### Create a Histogram
162
+ ### Create a histogram
156
163
157
- Create a Histogram instrument from the Meter , and record the measurement.
164
+ Create a histogram instrument from the meter , and record the measurement.
158
165
159
166
``` cpp
160
167
auto meter = provider->GetMeter (name, "1.2.0");
161
168
auto histogram_counter = meter->CreateDoubleHistogram("histogram_name");
162
169
histogram_counter->Record(val, labelkv);
163
170
```
164
171
165
- ### Create Observable Counter
172
+ ### Create an observable counter
166
173
167
- Create a Observable Counter Instrument from the Meter , and add a callback. The
168
- callback would be used to record the measurement during metrics collection.
169
- Ensure to keep the Instrument object active for the lifetime of collection.
174
+ Create an observable counter instrument from the meter , and add a callback. The
175
+ callback is used to record the measurement during metrics collection. Ensure to
176
+ keep the Instrument object active for the lifetime of collection.
170
177
171
178
```cpp
172
179
auto meter = provider->GetMeter(name, "1.2.0");
173
180
auto counter = meter->CreateDoubleObservableCounter(counter_name);
174
181
counter->AddCallback(MeasurementFetcher::Fetcher, nullptr);
175
182
```
176
183
177
- ### Create Views
184
+ ### Create views
178
185
179
- #### Map the Counter Instrument to Sum Aggregation
186
+ #### Map the counter instrument to sum aggregation
180
187
181
188
Create a view to map the Counter Instrument to Sum Aggregation. Add this view to
182
- provider. View creation is optional unless we want to add custom aggregation
183
- config, and attribute processor. Metrics SDK will implicitly create a missing
184
- view with default mapping between Instrument and Aggregation.
189
+ provider. View creation is optional unless you want to add custom aggregation
190
+ config, and attribute processor. Metrics SDK creates a missing view with default
191
+ mapping between Instrument and Aggregation.
185
192
186
193
``` cpp
187
194
std::unique_ptr<opentelemetry::sdk::metrics::InstrumentSelector> instrument_selector{
@@ -193,7 +200,7 @@ std::unique_ptr<opentelemetry::sdk::metrics::View> sum_view{
193
200
p->AddView(std::move(instrument_selector), std::move(meter_selector), std::move(sum_view));
194
201
```
195
202
196
- #### Map the Histogram Instrument to Histogram Aggregation
203
+ #### Map the histogram instrument to histogram aggregation
197
204
198
205
```cpp
199
206
std::unique_ptr<opentelemetry::sdk::metrics::InstrumentSelector> histogram_instrument_selector{
@@ -206,7 +213,7 @@ p->AddView(std::move(histogram_instrument_selector), std::move(histogram_meter_s
206
213
std::move(histogram_view));
207
214
```
208
215
209
- #### Map the Observable Counter Instrument to Sum Aggregation
216
+ #### Map the observable counter instrument to sum aggregation
210
217
211
218
``` cpp
212
219
std::unique_ptr<opentelemetry::sdk::metrics::InstrumentSelector> observable_instrument_selector{
@@ -220,7 +227,7 @@ p->AddView(std::move(observable_instrument_selector), std::move(observable_meter
220
227
std::move(observable_sum_view));
221
228
```
222
229
223
- ### Further Reading
230
+ ### Further reading
224
231
225
232
- [Metrics API](https://opentelemetry-cpp.readthedocs.io/en/latest/otel_docs/namespace_opentelemetry__metrics.html#)
226
233
- [Metrics SDK](https://opentelemetry-cpp.readthedocs.io/en/latest/otel_docs/namespace_opentelemetry__sdk__metrics.html)
@@ -232,7 +239,7 @@ The documentation for the logs API & SDK is missing, you can help make it
232
239
available by
233
240
[editing this page](https://github.com/open-telemetry/opentelemetry.io/edit/main/content/en/docs/languages/cpp/instrumentation.md).
234
241
235
- ## Next Steps
242
+ ## Next steps
236
243
237
244
You’ll also want to configure an appropriate exporter to
238
245
[export your telemetry data](/docs/languages/cpp/exporters) to one or more
0 commit comments