@@ -4,76 +4,45 @@ const { OTLPTraceExporter: OTLPTraceExporterHttp } = require('@opentelemetry/exp
44const { OTLPTraceExporter : OTLPTraceExporterProto } = require ( '@opentelemetry/exporter-trace-otlp-proto' ) ;
55const { Resource } = require ( '@opentelemetry/resources' ) ;
66const { ATTR_SERVICE_NAME } = require ( '@opentelemetry/semantic-conventions' ) ;
7- const api = require ( '@opentelemetry/api' ) ;
87
98exports . handler = async ( event , context ) => {
109 const serviceName = context . functionName ;
1110 const requestId = context . awsRequestId ;
1211 const endpoint = process . env . OTEL_EXPORTER_OTLP_ENDPOINT || 'http://localhost:4318' ;
1312
14- const results = {
15- requestId,
16- functionName : serviceName ,
17- json : { success : false } ,
18- protobuf : { success : false } ,
19- } ;
20-
21- try {
22- const jsonProvider = new NodeTracerProvider ( {
23- resource : new Resource ( { [ ATTR_SERVICE_NAME ] : serviceName } ) ,
24- } ) ;
25-
26- const jsonExporter = new OTLPTraceExporterHttp ( {
27- url : endpoint + '/v1/traces' ,
28- headers : { 'Content-Type' : 'application/json' } ,
29- } ) ;
30-
31- jsonProvider . addSpanProcessor ( new SimpleSpanProcessor ( jsonExporter ) ) ;
32- jsonProvider . register ( ) ;
33-
34- const jsonTracer = jsonProvider . getTracer ( 'json-test' ) ;
35- const jsonSpan = jsonTracer . startSpan ( 'test-span-json' ) ;
36- jsonSpan . setAttribute ( 'request_id' , requestId ) ;
37- jsonSpan . setAttribute ( 'encoding' , 'json' ) ;
38- jsonSpan . end ( ) ;
39-
40- await jsonProvider . forceFlush ( ) ;
41- await jsonProvider . shutdown ( ) ;
42-
43- results . json = { success : true , message : 'JSON encoding span sent successfully' } ;
44- } catch ( error ) {
45- results . json = { success : false , error : error . message } ;
46- }
47-
48- try {
49- const protoProvider = new NodeTracerProvider ( {
50- resource : new Resource ( { [ ATTR_SERVICE_NAME ] : serviceName } ) ,
51- } ) ;
52-
53- const protoExporter = new OTLPTraceExporterProto ( {
54- url : endpoint + '/v1/traces' ,
55- } ) ;
56-
57- protoProvider . addSpanProcessor ( new SimpleSpanProcessor ( protoExporter ) ) ;
58-
59- const protoTracer = protoProvider . getTracer ( 'proto-test' ) ;
60- const protoSpan = protoTracer . startSpan ( 'test-span-protobuf' ) ;
61- protoSpan . setAttribute ( 'request_id' , requestId ) ;
62- protoSpan . setAttribute ( 'encoding' , 'protobuf' ) ;
63- protoSpan . end ( ) ;
64-
65- await protoProvider . forceFlush ( ) ;
66- await protoProvider . shutdown ( ) ;
67-
68- results . protobuf = { success : true , message : 'Protobuf encoding span sent successfully' } ;
69- } catch ( error ) {
70- results . protobuf = { success : false , error : error . message } ;
71- }
72-
73- const allPassed = results . json . success && results . protobuf . success ;
74-
75- return {
76- statusCode : allPassed ? 200 : 500 ,
77- body : JSON . stringify ( { success : allPassed , ...results } , null , 2 ) ,
78- } ;
13+ const jsonProvider = new NodeTracerProvider ( {
14+ resource : new Resource ( { [ ATTR_SERVICE_NAME ] : serviceName } ) ,
15+ } ) ;
16+ const jsonExporter = new OTLPTraceExporterHttp ( {
17+ url : endpoint + '/v1/traces' ,
18+ headers : { 'Content-Type' : 'application/json' } ,
19+ } ) ;
20+ jsonProvider . addSpanProcessor ( new SimpleSpanProcessor ( jsonExporter ) ) ;
21+ jsonProvider . register ( ) ;
22+
23+ const jsonSpan = jsonProvider . getTracer ( 'json-test' ) . startSpan ( 'test-span-json' ) ;
24+ jsonSpan . setAttribute ( 'request_id' , requestId ) ;
25+ jsonSpan . setAttribute ( 'encoding' , 'json' ) ;
26+ jsonSpan . end ( ) ;
27+
28+ await jsonProvider . forceFlush ( ) ;
29+ await jsonProvider . shutdown ( ) ;
30+
31+ const protoProvider = new NodeTracerProvider ( {
32+ resource : new Resource ( { [ ATTR_SERVICE_NAME ] : serviceName } ) ,
33+ } ) ;
34+ const protoExporter = new OTLPTraceExporterProto ( {
35+ url : endpoint + '/v1/traces' ,
36+ } ) ;
37+ protoProvider . addSpanProcessor ( new SimpleSpanProcessor ( protoExporter ) ) ;
38+
39+ const protoSpan = protoProvider . getTracer ( 'proto-test' ) . startSpan ( 'test-span-protobuf' ) ;
40+ protoSpan . setAttribute ( 'request_id' , requestId ) ;
41+ protoSpan . setAttribute ( 'encoding' , 'protobuf' ) ;
42+ protoSpan . end ( ) ;
43+
44+ await protoProvider . forceFlush ( ) ;
45+ await protoProvider . shutdown ( ) ;
46+
47+ return { statusCode : 200 , body : JSON . stringify ( { message : 'Success' } ) } ;
7948} ;
0 commit comments