|
13 | 13 | */
|
14 | 14 | package software.amazon.lambda.powertools.tracing;
|
15 | 15 |
|
16 |
| -import java.util.function.Consumer; |
17 |
| - |
18 | 16 | import com.amazonaws.xray.AWSXRay;
|
19 | 17 | import com.amazonaws.xray.entities.Entity;
|
20 | 18 | import com.amazonaws.xray.entities.Subsegment;
|
21 | 19 |
|
| 20 | +import java.util.function.Consumer; |
| 21 | + |
22 | 22 | import static software.amazon.lambda.powertools.core.internal.LambdaHandlerProcessor.serviceName;
|
23 | 23 |
|
| 24 | +/** |
| 25 | + * A class of helper functions to add additional functionality and ease |
| 26 | + * of use. |
| 27 | + * |
| 28 | + */ |
24 | 29 | public final class PowerTracer {
|
25 | 30 |
|
| 31 | + /** |
| 32 | + * Put an annotation to the current subsegment. |
| 33 | + * |
| 34 | + * @param key the key of the annotation |
| 35 | + * @param value the value of the annotation |
| 36 | + */ |
26 | 37 | public static void putAnnotation(String key, String value) {
|
27 | 38 | AWSXRay.getCurrentSubsegmentOptional()
|
28 | 39 | .ifPresent(segment -> segment.putAnnotation(key, value));
|
29 | 40 | }
|
30 | 41 |
|
| 42 | + /** |
| 43 | + * Put additional metadata for the current subsegment. |
| 44 | + * |
| 45 | + * The namespace used will be the namespace of the current subsegment if it |
| 46 | + * is set else it will follow the namespace process as described in |
| 47 | + * {@link PowertoolsTracing} |
| 48 | + * |
| 49 | + * @param key the key of the metadata |
| 50 | + * @param value the value of the metadata |
| 51 | + */ |
31 | 52 | public static void putMetadata(String key, Object value) {
|
32 | 53 | String namespace = AWSXRay.getCurrentSubsegmentOptional()
|
33 | 54 | .map(Subsegment::getNamespace).orElse(serviceName());
|
34 | 55 |
|
35 | 56 | putMetadata(namespace, key, value);
|
36 | 57 | }
|
37 | 58 |
|
| 59 | + /** |
| 60 | + * Put additional metadata for the current subsegment. |
| 61 | + * |
| 62 | + * @param namespace the namespace of the metadata |
| 63 | + * @param key the key of the metadata |
| 64 | + * @param value the value of the metadata |
| 65 | + */ |
38 | 66 | public static void putMetadata(String namespace, String key, Object value) {
|
39 | 67 | AWSXRay.getCurrentSubsegmentOptional()
|
40 | 68 | .ifPresent(segment -> segment.putMetadata(namespace, key, value));
|
41 | 69 | }
|
42 | 70 |
|
| 71 | + /** |
| 72 | + * Adds a new subsegment around the passed consumer. This also provides access to |
| 73 | + * the newly created subsegment. |
| 74 | + * |
| 75 | + * The namespace used follows the flow as described in {@link PowertoolsTracing} |
| 76 | + * |
| 77 | + * This method is intended for use with multi-threaded programming where the |
| 78 | + * context is lost between threads. |
| 79 | + * |
| 80 | + * @param name the name of the subsegment |
| 81 | + * @param entity the current x-ray context |
| 82 | + * @param subsegment the x-ray subsegment for the wrapped consumer |
| 83 | + */ |
43 | 84 | public static void withEntitySubsegment(String name, Entity entity, Consumer<Subsegment> subsegment) {
|
44 | 85 | AWSXRay.setTraceEntity(entity);
|
45 | 86 | withEntitySubsegment(serviceName(), name, entity, subsegment);
|
46 | 87 | }
|
47 | 88 |
|
| 89 | + /** |
| 90 | + * Adds a new subsegment around the passed consumer. This also provides access to |
| 91 | + * the newly created subsegment. |
| 92 | + * |
| 93 | + * This method is intended for use with multi-threaded programming where the |
| 94 | + * context is lost between threads. |
| 95 | + * |
| 96 | + * @param namespace the namespace of the subsegment |
| 97 | + * @param name the name of the subsegment |
| 98 | + * @param entity the current x-ray context |
| 99 | + * @param subsegment the x-ray subsegment for the wrapped consumer |
| 100 | + */ |
48 | 101 | public static void withEntitySubsegment(String namespace, String name, Entity entity, Consumer<Subsegment> subsegment) {
|
49 | 102 | AWSXRay.setTraceEntity(entity);
|
50 | 103 | withSubsegment(namespace, name, subsegment);
|
51 | 104 | }
|
52 | 105 |
|
| 106 | + /** |
| 107 | + * Adds a new subsegment around the passed consumer. This also provides access to |
| 108 | + * the newly created subsegment. |
| 109 | + * |
| 110 | + * The namespace used follows the flow as described in {@link PowertoolsTracing} |
| 111 | + * |
| 112 | + * @param name the name of the subsegment |
| 113 | + * @param subsegment the x-ray subsegment for the wrapped consumer |
| 114 | + */ |
53 | 115 | public static void withSubsegment(String name, Consumer<Subsegment> subsegment) {
|
54 | 116 | withSubsegment(serviceName(), name, subsegment);
|
55 | 117 | }
|
56 | 118 |
|
| 119 | + /** |
| 120 | + * Adds a new subsegment around the passed consumer. This also provides access to |
| 121 | + * the newly created subsegment. |
| 122 | + * |
| 123 | + * @param namespace the namespace for the subsegment |
| 124 | + * @param name the name of the subsegment |
| 125 | + * @param subsegment the x-ray subsegment for the wrapped consumer |
| 126 | + */ |
57 | 127 | public static void withSubsegment(String namespace, String name, Consumer<Subsegment> subsegment) {
|
58 | 128 | Subsegment segment = AWSXRay.beginSubsegment("## " + name);
|
59 | 129 | segment.setNamespace(namespace);
|
|
0 commit comments