2929import java .util .Collections ;
3030import java .util .List ;
3131import java .util .Objects ;
32- import software .amazon .lambda .powertools .common .internal .LambdaHandlerProcessor ;
3332import software .amazon .lambda .powertools .tracing .opentelemetry .context .LambdaEventContextExtractorResolver ;
34- import software .amazon .lambda .powertools .tracing .opentelemetry .internal .AttributesConstants ;
3533import software .amazon .lambda .powertools .tracing .opentelemetry .internal .SpanOperation ;
3634import software .amazon .lambda .powertools .tracing .opentelemetry .internal .SpanScope ;
3735import software .amazon .lambda .powertools .tracing .opentelemetry .provider .OpenTelemetryProvider ;
@@ -76,6 +74,10 @@ public TracingOpenTelemetry(
7674 );
7775 }
7876
77+ public Tracer tracer () {
78+ return tracer ;
79+ }
80+
7981 public TextMapPropagator propagator () {
8082 return propagator ;
8183 }
@@ -99,33 +101,33 @@ public SpanScope addSpan(String name, SpanKind kind) {
99101 return addSpan (name , kind , Attributes .empty ());
100102 }
101103
102-
103104 public SpanScope addSpan (String name , SpanKind kind , Attributes attributes ) {
104105
105- Objects .requireNonNull (name , "name must not be null" );
106- Objects .requireNonNull (kind , "kind must not be null" );
107- Objects .requireNonNull (attributes , "attributes must not be null" );
108106 return addSpan (name , kind , attributes , Context .current ());
109107 }
110108
111-
112109 public SpanScope addSpan (String name , SpanKind kind , Attributes attributes , Context parentContext ) {
113110
114- Objects .requireNonNull (parentContext , "parentContext must not be null" );
115111 return addSpan (name , kind , attributes , parentContext , Collections .emptyList ());
116112 }
117113
118114 public SpanScope addSpan (
119- String spanName ,
120- SpanKind spanKind ,
115+ String name ,
116+ SpanKind kind ,
121117 Attributes attributes ,
122118 Context parentContext ,
123119 List <SpanContext > spanContexts
124120 ) {
125121
122+ Objects .requireNonNull (name , "name must not be null" );
123+ Objects .requireNonNull (kind , "kind must not be null" );
124+ Objects .requireNonNull (attributes , "attributes must not be null" );
125+ Objects .requireNonNull (parentContext , "parentContext must not be null" );
126+ Objects .requireNonNull (spanContexts , "spanContexts must not be null" );
127+
126128 SpanBuilder spanBuilder = tracer
127- .spanBuilder (spanName )
128- .setSpanKind (spanKind )
129+ .spanBuilder (name )
130+ .setSpanKind (kind )
129131 .setParent (parentContext )
130132 .setAllAttributes (attributes );
131133
@@ -137,43 +139,20 @@ public SpanScope addSpan(
137139
138140 public <T > T withSpan (String name , SpanOperation <T > operation ) throws Exception {
139141
140- Objects .requireNonNull (operation , "operation must not be null" );
141-
142- try (SpanScope scope = addSpan (name )) {
143- try {
144- return operation .execute (scope .span ());
145- } catch (Exception exception ) {
146- scope .recordException (exception );
147- throw exception ;
148- }
149- }
142+ return withSpan (name , SpanKind .INTERNAL , Attributes .empty (), operation );
150143 }
151144
152- public <T > T captureLambdaHandler (
145+ public <T > T withSpan (
153146 String name ,
154- com . amazonaws . services . lambda . runtime . Context lambdaContext ,
155- io . opentelemetry . context . Context parentContext ,
147+ SpanKind kind ,
148+ Attributes attributes ,
156149 SpanOperation <T > operation
157150 ) throws Exception {
158-
159- Objects .requireNonNull (name , "name must not be null" );
160- Objects .requireNonNull (parentContext , "parentContext must not be null" );
161151 Objects .requireNonNull (operation , "operation must not be null" );
162152
163- Span span = tracer .spanBuilder (name )
164- .setParent (parentContext )
165- .setSpanKind (SpanKind .SERVER )
166- .setAttribute (AttributesConstants .AWS_LAMBDA_FUNCTION_ARN , LambdaHandlerProcessor .isColdStart ())
167- .setAttribute (AttributesConstants .FAAS_INVOCATION_ID , lambdaContext .getAwsRequestId ())
168- .startSpan ();
169-
170- try (SpanScope scope = new SpanScope (span )) {
153+ try (SpanScope scope = addSpan (name , kind , attributes )) {
171154 try {
172- T result = operation .execute (span );
173-
174- LambdaHandlerProcessor .coldStartDone ();
175-
176- return result ;
155+ return operation .execute (scope .span ());
177156 } catch (Exception exception ) {
178157 scope .recordException (exception );
179158 throw exception ;
@@ -186,25 +165,24 @@ public <T> Context extractContext(T carrier, TextMapGetter<T> getter) {
186165 return extractContext (Context .current (), carrier , getter );
187166 }
188167
189-
190168 public <T > Context extractContext (Context context , T carrier , TextMapGetter <T > getter ) {
191169
192170 Objects .requireNonNull (context , "context must not be null" );
171+ Objects .requireNonNull (carrier , "carrier must not be null" );
193172 Objects .requireNonNull (getter , "getter must not be null" );
194173
195174 return propagator .extract (context , carrier , getter );
196175 }
197176
198-
199177 public <T > void injectContext (T carrier , TextMapSetter <T > setter ) {
200178
201179 injectContext (Context .current (), carrier , setter );
202180 }
203181
204-
205182 public <T > void injectContext (Context context , T carrier , TextMapSetter <T > setter ) {
206183
207184 Objects .requireNonNull (context , "context must not be null" );
185+ Objects .requireNonNull (carrier , "carrier must not be null" );
208186 Objects .requireNonNull (setter , "setter must not be null" );
209187
210188 propagator .inject (context , carrier , setter );
@@ -218,12 +196,10 @@ private static LambdaEventContextExtractorResolver createDefaultEventContextExtr
218196 return LambdaEventContextExtractorResolver .create ();
219197 }
220198
221-
222199 public static TracingOpenTelemetry create () {
223200 return new TracingOpenTelemetry ();
224201 }
225202
226-
227203 public static Builder builder () {
228204 return new Builder ();
229205 }
0 commit comments