Skip to content

Commit 813025a

Browse files
authored
Update the QUICKSTART.md for the 0.10.0 release. (open-telemetry#2048)
1 parent 1883c57 commit 813025a

1 file changed

Lines changed: 59 additions & 49 deletions

File tree

QUICKSTART.md

Lines changed: 59 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,19 @@ monitored. More information is available in the specification chapter [Obtaining
4343

4444
```java
4545
Tracer tracer =
46-
OpenTelemetry.getTracer("instrumentation-library-name","semver:1.0.0");
46+
OpenTelemetry.getGlobalTracer("instrumentation-library-name","semver:1.0.0");
4747
```
4848

4949
### Create basic Span
5050
To create a basic span, you only need to specify the name of the span.
5151
The start and end time of the span is automatically set by the OpenTelemetry SDK.
5252
```java
5353
Span span = tracer.spanBuilder("my span").startSpan();
54-
try (Scope scope = TracingContextUtils.currentContextWith(span)) {
54+
try (Scope scope = span.makeCurrent()) {
5555
// your use case
5656
...
5757
} catch (Throwable t) {
58-
span.setStatus(StatusCanonicalCode.ERROR, "Change it to your error message");
58+
span.setStatus(StatusCode.ERROR, "Change it to your error message");
5959
} finally {
6060
span.end(); // closing the scope does not end the span, this has to be done manually
6161
}
@@ -70,35 +70,38 @@ processes, see [Context Propagation](#context-propagation).
7070
For a method `a` calling a method `b`, the spans could be manually linked in the following way:
7171
```java
7272
void a() {
73-
Span parentSpan = tracer.spanBuilder("a")
74-
.startSpan();
75-
b(parentSpan);
76-
parentSpan.end();
73+
Span parentSpan = tracer.spanBuilder("a").startSpan();
74+
try {
75+
b(parentSpan);
76+
} finally {
77+
parentSpan.end();
78+
}
7779
}
80+
7881
void b(Span parentSpan) {
7982
Span childSpan = tracer.spanBuilder("b")
80-
.setParent(parentSpan)
83+
.setParent(Context.current().with(parentSpan))
8184
.startSpan();
8285
// do stuff
8386
childSpan.end();
8487
}
8588
```
86-
The OpenTelemetry API offers also an automated way to propagate the `parentSpan`:
89+
The OpenTelemetry API offers also an automated way to propagate the parent span on the current thread:
8790
```java
8891
void a() {
8992
Span parentSpan = tracer.spanBuilder("a").startSpan();
90-
try(Scope scope = TracingContextUtils.currentContextWith(parentSpan)) {
93+
try(Scope scope = parentSpan.makeCurrent()) {
9194
b();
9295
} finally {
9396
parentSpan.end();
9497
}
9598
}
9699
void b() {
97100
Span childSpan = tracer.spanBuilder("b")
98-
// NOTE: setParent(parentSpan) is not required;
99-
// `TracingContextUtils.getCurrentSpan()` is automatically added as parent
101+
// NOTE: setParent(...) is not required;
102+
// `Span.current()` is automatically added as the parent
100103
.startSpan();
101-
try(Scope scope = TracingContextUtils.currentContextWith(childSpan)) {
104+
try(Scope scope = childSpan.makeCurrent()) {
102105
// do stuff
103106
} finally {
104107
childSpan.end();
@@ -155,10 +158,10 @@ representing a single incoming item being processed in the batch.
155158

156159
```java
157160
Span child = tracer.spanBuilder("childWithLink")
158-
.addLink(parentSpan1.getContext())
159-
.addLink(parentSpan2.getContext())
160-
.addLink(parentSpan3.getContext())
161-
.addLink(remoteContext)
161+
.addLink(parentSpan1.getSpanContext())
162+
.addLink(parentSpan2.getSpanContext())
163+
.addLink(parentSpan3.getSpanContext())
164+
.addLink(remoteSpanContext)
162165
.startSpan();
163166
```
164167

@@ -177,22 +180,22 @@ The following presents an example of an outgoing HTTP request using `HttpURLConn
177180
TextMapPropagator.Setter<HttpURLConnection> setter =
178181
new TextMapPropagator.Setter<HttpURLConnection>() {
179182
@Override
180-
public void put(HttpURLConnection carrier, String key, String value) {
183+
public void set(HttpURLConnection carrier, String key, String value) {
181184
// Insert the context as Header
182185
carrier.setRequestProperty(key, value);
183186
}
184187
};
185188

186189
URL url = new URL("http://127.0.0.1:8080/resource");
187190
Span outGoing = tracer.spanBuilder("/resource").setSpanKind(Span.Kind.CLIENT).startSpan();
188-
try (Scope scope = TracingContextUtils.currentContextWith(outGoing)) {
191+
try (Scope scope = outGoing.makeCurrent()) {
189192
// Semantic Convention.
190-
// (Observe that to set these, Span does not *need* to be the current instance.)
191-
outGoing.setAttribute("http.method", "GET");
192-
outGoing.setAttribute("http.url", url.toString());
193+
// (Note that to set these, Span does not *need* to be the current instance in Context or Scope.)
194+
outGoing.setAttribute(SemanticAttributes.HTTP_METHOD, "GET");
195+
outGoing.setAttribute(SemanticAttributes.HTTP_URL, url.toString());
193196
HttpURLConnection transportLayer = (HttpURLConnection) url.openConnection();
194197
// Inject the request with the *current* Context, which contains our current Span.
195-
OpenTelemetry.getPropagators().getTextMapPropagator().inject(Context.current(), transportLayer, setter);
198+
OpenTelemetry.getGlobalPropagators().getTextMapPropagator().inject(Context.current(), transportLayer, setter);
196199
// Make outgoing call
197200
} finally {
198201
outGoing.end();
@@ -214,26 +217,31 @@ TextMapPropagator.Getter<HttpExchange> getter =
214217
}
215218
return null;
216219
}
220+
221+
@Override
222+
public Iterable<String> keys(HttpExchange carrier) {
223+
return carrier.getRequestHeaders().keySet();
224+
}
217225
};
218226
...
219227
public void handle(HttpExchange httpExchange) {
220228
// Extract the SpanContext and other elements from the request.
221-
Context extractedContext = OpenTelemetry.getPropagators().getTextMapPropagator()
229+
Context extractedContext = OpenTelemetry.getGlobalPropagators().getTextMapPropagator()
222230
.extract(Context.current(), httpExchange, getter);
223-
Span serverSpan = null;
224-
try (Scope scope = ContextUtils.withScopedContext(extractedContext)) {
231+
try (Scope scope = extractedContext.makeCurrent()) {
225232
// Automatically use the extracted SpanContext as parent.
226-
serverSpan = tracer.spanBuilder("/resource").setSpanKind(Span.Kind.SERVER)
233+
Span serverSpan = tracer.spanBuilder("GET /resource")
234+
.setSpanKind(Span.Kind.SERVER)
227235
.startSpan();
228-
// Add the attributes defined in the Semantic Conventions
229-
serverSpan.setAttribute("http.method", "GET");
230-
serverSpan.setAttribute("http.scheme", "http");
231-
serverSpan.setAttribute("http.host", "localhost:8080");
232-
serverSpan.setAttribute("http.target", "/resource");
233-
// Serve the request
234-
...
235-
} finally {
236-
if (serverSpan != null) {
236+
try {
237+
// Add the attributes defined in the Semantic Conventions
238+
serverSpan.setAttribute(SemanticAttributes.HTTP_METHOD, "GET");
239+
serverSpan.setAttribute(SemanticAttributes.HTTP_SCHEME, "http");
240+
serverSpan.setAttribute(SemanticAttributes.HTTP_HOST, "localhost:8080");
241+
serverSpan.setAttribute(SemanticAttributes.HTTP_TARGET, "/resource");
242+
// Serve the request
243+
...
244+
} finally {
237245
serverSpan.end();
238246
}
239247
}
@@ -254,7 +262,7 @@ The following is an example of counter usage:
254262

255263
```java
256264
// Gets or creates a named meter instance
257-
Meter meter = OpenTelemetry.getMeter("instrumentation-library-name","semver:1.0.0");
265+
Meter meter = OpenTelemetry.getGlobalMeter("instrumentation-library-name","semver:1.0.0");
258266

259267
// Build counter e.g. LongCounter
260268
LongCounter counter = meter
@@ -311,8 +319,8 @@ For example, a basic configuration instantiates the SDK tracer registry and sets
311319
traces to a logging stream.
312320

313321
```java
314-
// Get the tracer
315-
TracerSdkManagement tracerSdkManagement = OpenTelemetrySdk.getTracerManagement();
322+
// Get the tracer management interface
323+
TracerSdkManagement tracerSdkManagement = OpenTelemetrySdk.getGlobalTracerManagement();
316324

317325
// Set to export the traces to a logging stream
318326
tracerSdkManagement.addSpanProcessor(
@@ -326,24 +334,25 @@ tracerSdkManagement.addSpanProcessor(
326334
It is not always feasible to trace and export every user request in an application.
327335
In order to strike a balance between observability and expenses, traces can be sampled.
328336

329-
The OpenTelemetry SDK offers three samplers out of the box:
337+
The OpenTelemetry SDK offers four samplers out of the box:
330338
- [AlwaysOnSampler] which samples every trace regardless of upstream sampling decisions.
331339
- [AlwaysOffSampler] which doesn't sample any trace, regardless of upstream sampling decisions.
332-
- [Probability] which samples a configurable percentage of traces, and additionally samples any
340+
- [ParentBased] which uses the parent span to make sampling decisions, if present.
341+
- [TraceIdRatioBased] which samples a configurable percentage of traces, and additionally samples any
333342
trace that was sampled upstream.
334343

335344
Additional samplers can be provided by implementing the `io.opentelemetry.sdk.trace.Sampler`
336345
interface.
337346

338347
```java
339348
TraceConfig alwaysOn = TraceConfig.getDefault().toBuilder().setSampler(
340-
Samplers.alwaysOn()
349+
Sampler.alwaysOn()
341350
).build();
342351
TraceConfig alwaysOff = TraceConfig.getDefault().toBuilder().setSampler(
343-
Samplers.alwaysOff()
352+
Sampler.alwaysOff()
344353
).build();
345354
TraceConfig half = TraceConfig.getDefault().toBuilder().setSampler(
346-
Samplers.probability(0.5)
355+
Sampler.traceIdRatioBased(0.5)
347356
).build();
348357
// Configure the sampler to use
349358
tracerProvider.updateActiveTraceConfig(
@@ -406,7 +415,7 @@ environment variables and builder `set*` methods.
406415

407416
```java
408417
// Get TraceConfig associated with TracerSdk
409-
TracerConfig traceConfig = OpenTelemetrySdk.getTracerManagement().getActiveTraceConfig();
418+
TracerConfig traceConfig = OpenTelemetrySdk.getGlobalTracerManagement().getActiveTraceConfig();
410419

411420
// Get TraceConfig Builder
412421
Builder builder = traceConfig.toBuilder();
@@ -421,7 +430,7 @@ builder.readEnvironmentVariables()
421430
builder.setMaxNumberOfLinks(10);
422431

423432
// Update the resulting TraceConfig instance
424-
OpenTelemetrySdk.getTracerManagement().updateActiveTraceConfig(builder.build());
433+
OpenTelemetrySdk.getGlobalTracerManagement().updateActiveTraceConfig(builder.build());
425434
```
426435

427436
Supported system properties and environment variables:
@@ -436,9 +445,10 @@ Supported system properties and environment variables:
436445
| otel.config.max.link.attrs | OTEL_CONFIG_MAX_LINK_ATTRS | Max number of attributes per link, extra will be dropped (default: 32) |
437446
| otel.config.max.attr.length | OTEL_CONFIG_MAX_ATTR_LENGTH | Max length of string attribute value in characters, too long will be truncated (default: unlimited) |
438447

439-
[AlwaysOnSampler]: https://github.com/open-telemetry/opentelemetry-java/blob/master/sdk/src/main/java/io/opentelemetry/sdk/trace/Samplers.java#L82--L105
440-
[AlwaysOffSampler]:https://github.com/open-telemetry/opentelemetry-java/blob/master/sdk/src/main/java/io/opentelemetry/sdk/trace/Samplers.java#L108--L131
441-
[Probability]:https://github.com/open-telemetry/opentelemetry-java/blob/master/sdk/src/main/java/io/opentelemetry/sdk/trace/Samplers.java#L142--L203
448+
[AlwaysOnSampler]: https://github.com/open-telemetry/opentelemetry-java/blob/master/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/samplers/Sampler.java#L29
449+
[AlwaysOffSampler]:https://github.com/open-telemetry/opentelemetry-java/blob/master/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/samplers/Sampler.java#L40
450+
[ParentBased]:https://github.com/open-telemetry/opentelemetry-java/blob/master/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/samplers/Sampler.java#L54
451+
[TraceIdRatioBased]:https://github.com/open-telemetry/opentelemetry-java/blob/master/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/samplers/Sampler.java#L78
442452
[Library Guidelines]: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/library-guidelines.md
443453
[OpenTelemetry Collector]: https://github.com/open-telemetry/opentelemetry-collector
444454
[OpenTelemetry Registry]: https://opentelemetry.io/registry/?s=exporter

0 commit comments

Comments
 (0)