Skip to content

Commit adfff28

Browse files
committed
Add detailed JavaDocs to improve documentation of TracingOpenTelemetry, context extractors, and related classes and methods.
1 parent f1d0ba6 commit adfff28

19 files changed

Lines changed: 897 additions & 26 deletions

powertools-tracing-opentelemetry/src/main/java/software/amazon/lambda/powertools/tracing/opentelemetry/CaptureMode.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2023 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. 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+
*/
16+
117
package software.amazon.lambda.powertools.tracing.opentelemetry;
218

319
/**

powertools-tracing-opentelemetry/src/main/java/software/amazon/lambda/powertools/tracing/opentelemetry/Tracing.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2023 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. 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+
*/
16+
117
package software.amazon.lambda.powertools.tracing.opentelemetry;
218

319
import java.lang.annotation.ElementType;

powertools-tracing-opentelemetry/src/main/java/software/amazon/lambda/powertools/tracing/opentelemetry/TracingOpenTelemetry.java

Lines changed: 203 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,18 @@
3636
import software.amazon.lambda.powertools.tracing.opentelemetry.internal.SpanScope;
3737
import software.amazon.lambda.powertools.tracing.opentelemetry.provider.OpenTelemetryProvider;
3838

39-
39+
/**
40+
* A utility class responsible for managing OpenTelemetry tracing functionality,
41+
* including creating and managing spans, handling context propagation, and facilitating
42+
* relevant operations for distributed tracing.
43+
* <p>
44+
* This class provides methods to manage the life cycle of spans, propagate and extract
45+
* context, flush telemetry data, and execute operations within spans. It also supports
46+
* configuration via a builder pattern.
47+
* <p>
48+
* The class is designed to be thread-safe and offers a default singleton instance
49+
* for convenience.
50+
*/
4051
public final class TracingOpenTelemetry {
4152

4253
private static final TracingOpenTelemetry DEFAULT_INSTANCE = new TracingOpenTelemetry();
@@ -77,50 +88,130 @@ public TracingOpenTelemetry(
7788
);
7889
}
7990

91+
/**
92+
* Provides access to the current Tracer instance.
93+
*
94+
* @return the Tracer instance associated with the current context
95+
*/
8096
public Tracer tracer() {
8197
return tracer;
8298
}
8399

100+
/**
101+
* Provides the current TextMapPropagator instance.
102+
*
103+
* @return the TextMapPropagator instance used for propagating context information.
104+
*/
84105
public TextMapPropagator propagator() {
85106
return propagator;
86107
}
87108

109+
/**
110+
* Retrieves the instance of LambdaEventContextExtractorResolver.
111+
*
112+
* @return the resolver used to extract context from Lambda events.
113+
*/
88114
public LambdaEventContextExtractorResolver eventContextExtractorResolver() {
89115
return eventContextExtractorResolver;
90116
}
91117

118+
/**
119+
* Retrieves the current active span within the context.
120+
*
121+
* @return the currently active span, or null if there is no active span
122+
*/
92123
public Span currentSpan() {
93124
return Span.current();
94125
}
95126

127+
/**
128+
* Forces all pending spans and related telemetry data to be processed and exported.
129+
* This method sends the pending data using the default timeout period.
130+
*
131+
* @return a {@code CompletableResultCode} indicating the success or failure of the flush operation
132+
*/
96133
public CompletableResultCode flush() {
97134
return flush(5, TimeUnit.SECONDS);
98135
}
99136

137+
/**
138+
* Forces all pending spans and related telemetry data to be processed and exported
139+
* within a specified timeout period.
140+
*
141+
* @param timeout the maximum duration to wait for the flush operation to complete
142+
* @param unit the time unit of the {@code timeout} parameter
143+
* @return a {@code CompletableResultCode} indicating the success or failure of the flush operation
144+
*/
100145
public CompletableResultCode flush(long timeout, TimeUnit unit) {
101146
return OpenTelemetryProvider.forceFlush().join(timeout, unit);
102147
}
103148

149+
/**
150+
* Starts a new OpenTelemetry span with the given name and a default {@link SpanKind#INTERNAL} kind.
151+
*
152+
* @param name the name of the span to be created
153+
* @return a {@link SpanScope} instance that manages the lifecycle of the span and its associated context
154+
*/
104155
public SpanScope addSpan(String name) {
105156
return addSpan(name, SpanKind.INTERNAL);
106157
}
107158

108-
159+
/**
160+
* Starts a new OpenTelemetry span with the given name, kind, and default attributes.
161+
*
162+
* @param name the name of the span to be created
163+
* @param kind the kind of the span, e.g., {@link SpanKind#INTERNAL}, {@link SpanKind#CLIENT}, etc.
164+
* @return a {@link SpanScope} instance that manages the lifecycle of the span and its associated context
165+
*/
109166
public SpanScope addSpan(String name, SpanKind kind) {
110167

111168
return addSpan(name, kind, Attributes.empty());
112169
}
113170

171+
/**
172+
* Starts a new OpenTelemetry span with the given name, kind, and attributes,
173+
* using the current thread context as the parent context.
174+
*
175+
* @param name the name of the span to be created
176+
* @param kind the kind of the span, such as {@code SpanKind.INTERNAL}, {@code SpanKind.CLIENT}, etc.
177+
* @param attributes the attributes to associate with the span
178+
* @return a {@code SpanScope} instance that manages the lifecycle of the span and its associated context
179+
*/
114180
public SpanScope addSpan(String name, SpanKind kind, Attributes attributes) {
115181

116182
return addSpan(name, kind, attributes, Context.current());
117183
}
118184

185+
/**
186+
* Starts a new OpenTelemetry span with the given name, kind, attributes, and parent context.
187+
* The span is returned encapsulated in a {@code SpanScope}, which manages the lifecycle
188+
* of the span and its associated context.
189+
*
190+
* @param name the name of the span to be created
191+
* @param kind the type of the span, such as {@code SpanKind.INTERNAL}, {@code SpanKind.CLIENT}, etc.
192+
* @param attributes the attributes to associate with the span
193+
* @param parentContext the parent context to use for the span
194+
* @return a {@code SpanScope} instance that manages the lifecycle of the span and its related context
195+
*/
119196
public SpanScope addSpan(String name, SpanKind kind, Attributes attributes, Context parentContext) {
120197

121198
return addSpan(name, kind, attributes, parentContext, Collections.emptyList());
122199
}
123200

201+
/**
202+
* Starts a new OpenTelemetry span with the given configuration, including name, kind, attributes,
203+
* parent context, and links to other spans represented by their {@code SpanContext}s.
204+
* The resulting span is encapsulated within a {@code SpanScope} for proper lifecycle management.
205+
*
206+
* @param name the name of the span to be created; must not be null
207+
* @param kind the type of the span, such as {@code SpanKind.INTERNAL}, {@code SpanKind.CLIENT}, etc.;
208+
* must not be null
209+
* @param attributes the attributes to associate with the span; must not be null
210+
* @param parentContext the parent context to use for the span; must not be null
211+
* @param spanContexts the list of {@code SpanContext} instances to link to the created span; must not be null
212+
* @return a {@code SpanScope} instance that manages the lifecycle of the span and its associated context
213+
* @throws NullPointerException if any of the parameters are null
214+
*/
124215
public SpanScope addSpan(
125216
String name,
126217
SpanKind kind,
@@ -146,12 +237,37 @@ public SpanScope addSpan(
146237
return new SpanScope(spanBuilder.startSpan());
147238
}
148239

149-
240+
/**
241+
* Executes a given operation within the context of an OpenTelemetry span with the specified name.
242+
* The span is created with the default {@link SpanKind#INTERNAL} and no additional attributes.
243+
* Any exceptions thrown during the operation will be recorded in the span.
244+
*
245+
* @param <T> the type of result returned by the operation
246+
* @param name the name of the span to be created; must not be null
247+
* @param operation the operation to execute within the span context; must not be null
248+
* @return the result of the operation
249+
* @throws Exception if an error occurs during the execution of the operation
250+
*/
150251
public <T> T withSpan(String name, SpanOperation<T> operation) throws Exception {
151252

152253
return withSpan(name, SpanKind.INTERNAL, Attributes.empty(), operation);
153254
}
154255

256+
/**
257+
* Executes a given operation within the context of an OpenTelemetry span
258+
* with the specified name, kind, and attributes. The span is created and
259+
* managed within the method. Any exceptions thrown during the operation
260+
* are recorded in the span before being propagated.
261+
*
262+
* @param <T> the type of result returned by the operation
263+
* @param name the name of the span to be created; must not be null
264+
* @param kind the kind of the span, such as {@code SpanKind.INTERNAL}
265+
* or {@code SpanKind.CLIENT}; must not be null
266+
* @param attributes the attributes to associate with the span; must not be null
267+
* @param operation the operation to execute within the span context; must not be null
268+
* @return the result of the operation
269+
* @throws Exception if an error occurs during the execution of the operation
270+
*/
155271
public <T> T withSpan(
156272
String name,
157273
SpanKind kind,
@@ -170,11 +286,28 @@ public <T> T withSpan(
170286
}
171287
}
172288

289+
/**
290+
* Extracts a {@code Context} from the given carrier using the specified {@link TextMapGetter}.
291+
*
292+
* @param <T> the type of the carrier from which the context is extracted
293+
* @param carrier the carrier object that holds context propagation data; must not be null
294+
* @param getter the {@link TextMapGetter} used to read propagation fields from the carrier; must not be null
295+
* @return the extracted {@code Context}, or the current context if no context could be extracted
296+
* @throws NullPointerException if the carrier or getter is null
297+
*/
173298
public <T> Context extractContext(T carrier, TextMapGetter<T> getter) {
174299

175300
return extractContext(Context.current(), carrier, getter);
176301
}
177302

303+
/**
304+
* Extracts a {@link Context} from the given carrier using the specified {@link TextMapGetter}.
305+
*
306+
* @param context the initial {@link Context} used as the baseline for extraction; must not be null
307+
* @param carrier the carrier of the propagation fields; must not be null
308+
* @param getter the {@link TextMapGetter} used to read propagation fields from the carrier; must not be null
309+
* @return the extracted {@link Context} containing the propagated values
310+
*/
178311
public <T> Context extractContext(Context context, T carrier, TextMapGetter<T> getter) {
179312

180313
Objects.requireNonNull(context, "context must not be null");
@@ -184,11 +317,26 @@ public <T> Context extractContext(Context context, T carrier, TextMapGetter<T> g
184317
return propagator.extract(context, carrier, getter);
185318
}
186319

320+
/**
321+
* Injects the current context into the specified carrier using the provided TextMapSetter.
322+
*
323+
* @param <T> The type of the carrier into which the context will be injected.
324+
* @param carrier The carrier object that will hold the injected context.
325+
* @param setter The TextMapSetter implementation used to set the context into the carrier.
326+
*/
187327
public <T> void injectContext(T carrier, TextMapSetter<T> setter) {
188328

189329
injectContext(Context.current(), carrier, setter);
190330
}
191331

332+
/**
333+
* Injects the provided {@code Context} into the specified carrier using the given {@code TextMapSetter}.
334+
*
335+
* @param context the context to inject; must not be null
336+
* @param carrier the carrier into which the context will be injected; must not be null
337+
* @param setter the {@code TextMapSetter} used to define how the context is set on the carrier; must not be null
338+
* @param <T> the type of the carrier
339+
*/
192340
public <T> void injectContext(Context context, T carrier, TextMapSetter<T> setter) {
193341

194342
Objects.requireNonNull(context, "context must not be null");
@@ -206,37 +354,89 @@ private static LambdaEventContextExtractorResolver createDefaultEventContextExtr
206354
return LambdaEventContextExtractorResolver.create();
207355
}
208356

357+
/**
358+
* Creates and returns the default instance of the TracingOpenTelemetry.
359+
*
360+
* @return The default instance of TracingOpenTelemetry.
361+
*/
209362
public static TracingOpenTelemetry create() {
210363
return DEFAULT_INSTANCE;
211364
}
212365

366+
/**
367+
* Creates and returns a new instance of the Builder.
368+
*
369+
* @return a new Builder instance
370+
*/
213371
public static Builder builder() {
214372
return new Builder();
215373
}
216374

375+
/**
376+
* Builder class for creating instances of TracingOpenTelemetry.
377+
* This class provides a fluent API for configuring and constructing
378+
* a TracingOpenTelemetry object.
379+
* <p>
380+
* The Builder allows customization of the following components:
381+
* - Tracer: A tracer instance used for tracing operations.
382+
* - TextMapPropagator: A propagator responsible for context propagation.
383+
* - LambdaEventContextExtractorResolver: A resolver for extracting context from Lambda events.
384+
*/
217385
public static final class Builder {
218386

219387
private Tracer tracer;
220388
private TextMapPropagator propagator = createDefaultPropagator();
221389
private LambdaEventContextExtractorResolver eventContextExtractorResolver =
222390
createDefaultEventContextExtractorResolver();
223391

392+
/**
393+
* Sets the tracer instance to be used for tracing operations.
394+
* This method allows specifying a custom tracer, which will
395+
* be used to create and manage spans in tracing contexts.
396+
*
397+
* @param tracer the tracer instance to be used for tracing
398+
* @return the updated Builder instance for method chaining
399+
*/
224400
public Builder tracer(Tracer tracer) {
225401
this.tracer = tracer;
226402
return this;
227403
}
228404

405+
/**
406+
* Sets the {@link TextMapPropagator} to be used for context propagation.
407+
* This allows specifying a custom propagator to handle the injection and extraction
408+
* of context data across process boundaries.
409+
*
410+
* @param propagator the {@link TextMapPropagator} instance to be used for context propagation
411+
* @return the updated Builder instance for method chaining
412+
*/
229413
public Builder propagator(TextMapPropagator propagator) {
230414
this.propagator = propagator;
231415
return this;
232416
}
233417

418+
/**
419+
* Sets the {@link LambdaEventContextExtractorResolver} to be used for extracting
420+
* context from AWS Lambda events. This allows specifying a custom resolver
421+
* to handle the extraction of trace context from various types of AWS Lambda
422+
* event sources.
423+
*
424+
* @param eventContextExtractorResolver the {@link LambdaEventContextExtractorResolver} instance
425+
* to be used for extracting trace context from Lambda events
426+
* @return the updated Builder instance for method chaining
427+
*/
234428
public Builder eventContextExtractorResolver(
235429
LambdaEventContextExtractorResolver eventContextExtractorResolver) {
236430
this.eventContextExtractorResolver = eventContextExtractorResolver;
237431
return this;
238432
}
239433

434+
/**
435+
* Constructs a new instance of TracingOpenTelemetry using the current state of the Builder.
436+
* This method finalizes the configuration and returns the configured TracingOpenTelemetry instance.
437+
*
438+
* @return a fully configured TracingOpenTelemetry instance
439+
*/
240440
public TracingOpenTelemetry build() {
241441
return new TracingOpenTelemetry(this);
242442
}

0 commit comments

Comments
 (0)