@@ -22,6 +22,11 @@ import {
22
22
readTraceFromSQSEvent ,
23
23
readTraceFromHTTPEvent ,
24
24
readTraceFromLambdaContext ,
25
+ hexToBinary ,
26
+ deterministicMd5HashInBinary ,
27
+ deterministicMd5HashToBigIntString ,
28
+ readTraceFromStepFunctionsContext ,
29
+ StepFunctionContext ,
25
30
} from "./context" ;
26
31
27
32
let sentSegment : any ;
@@ -1052,6 +1057,38 @@ describe("extractTraceContext", () => {
1052
1057
expect ( sentSegment ) . toBeUndefined ( ) ;
1053
1058
} ) ;
1054
1059
1060
+ it ( "returns trace read from step functions event with the extractor as the highest priority" , ( ) => {
1061
+ const stepFunctionEvent = {
1062
+ MyInput : "MyValue" ,
1063
+ Execution : {
1064
+ Id : "arn:aws:states:sa-east-1:425362996713:express:logs-to-traces-sequential:85a9933e-9e11-83dc-6a61-b92367b6c3be:3f7ef5c7-c8b8-4c88-90a1-d54aa7e7e2bf" ,
1065
+ Input : {
1066
+ MyInput : "MyValue" ,
1067
+ } ,
1068
+ Name : "85a9933e-9e11-83dc-6a61-b92367b6c3be" ,
1069
+ RoleArn : "arn:aws:iam::425362996713:role/service-role/StepFunctions-logs-to-traces-sequential-role-ccd69c03" ,
1070
+ StartTime : "2022-12-08T21:08:17.924Z" ,
1071
+ } ,
1072
+ State : {
1073
+ Name : "step-one" ,
1074
+ EnteredTime : "2022-12-08T21:08:19.224Z" ,
1075
+ RetryCount : 2 ,
1076
+ } ,
1077
+ StateMachine : {
1078
+ Id : "arn:aws:states:sa-east-1:425362996713:stateMachine:logs-to-traces-sequential" ,
1079
+ Name : "my-state-machine" ,
1080
+ } ,
1081
+ } ;
1082
+
1083
+ const result = extractTraceContext ( stepFunctionEvent , { } as Context , undefined ) ;
1084
+ expect ( result ) . toEqual ( {
1085
+ parentID : "4602916161841036335" ,
1086
+ sampleMode : 1 ,
1087
+ traceID : "947965466153612645" ,
1088
+ source : "event" ,
1089
+ } ) ;
1090
+ } ) ;
1091
+
1055
1092
it ( "skips adding datadog metadata to x-ray when x-ray trace isn't sampled" , ( ) => {
1056
1093
jest . spyOn ( Date , "now" ) . mockImplementation ( ( ) => 1487076708000 ) ;
1057
1094
process . env [ xrayTraceEnvVar ] = "Root=1-5e272390-8c398be037738dc042009320;Parent=94ae789b969f1cc5;Sampled=0" ;
@@ -1109,3 +1146,83 @@ describe("extractTraceContext", () => {
1109
1146
` ) ;
1110
1147
} ) ;
1111
1148
} ) ;
1149
+
1150
+ describe . each ( [
1151
+ [ "0" , "0000" ] ,
1152
+ [ "1" , "0001" ] ,
1153
+ [ "2" , "0010" ] ,
1154
+ [ "3" , "0011" ] ,
1155
+ [ "4" , "0100" ] ,
1156
+ [ "5" , "0101" ] ,
1157
+ [ "6" , "0110" ] ,
1158
+ [ "7" , "0111" ] ,
1159
+ [ "8" , "1000" ] ,
1160
+ [ "9" , "1001" ] ,
1161
+ [ "a" , "1010" ] ,
1162
+ [ "b" , "1011" ] ,
1163
+ [ "c" , "1100" ] ,
1164
+ [ "d" , "1101" ] ,
1165
+ [ "e" , "1110" ] ,
1166
+ [ "f" , "1111" ] ,
1167
+ ] ) ( `test hexToBinary` , ( hex , expected ) => {
1168
+ test ( `${ hex } to binary returns ${ expected } ` , ( ) => {
1169
+ expect ( hexToBinary ( hex ) ) . toBe ( expected ) ;
1170
+ } ) ;
1171
+ } ) ;
1172
+
1173
+ describe ( "test_deterministicMd5HashInBinary" , ( ) => {
1174
+ it ( "test same hashing is generated as logs-backend for a random string" , ( ) => {
1175
+ const actual = deterministicMd5HashInBinary ( "some_testing_random_string" ) ;
1176
+ expect ( actual ) . toEqual ( "0001111100111110001000110110011110010111000110001001001111110001" ) ;
1177
+ } ) ;
1178
+
1179
+ it ( "test same hashing is generated as logs-backend for an execution id" , ( ) => {
1180
+ const actual = deterministicMd5HashInBinary (
1181
+ "arn:aws:states:sa-east-1:601427271234:express:DatadogStateMachine:acaf1a67-336a-e854-1599-2a627eb2dd8a:c8baf081-31f1-464d-971f-70cb17d041f4" ,
1182
+ ) ;
1183
+ expect ( actual ) . toEqual ( "0010010000101100100000101011111101111100110110001110111100111101" ) ;
1184
+ } ) ;
1185
+
1186
+ it ( "test same hashing is generated as logs-backend for another execution id" , ( ) => {
1187
+ const actual = deterministicMd5HashInBinary (
1188
+ "arn:aws:states:sa-east-1:601427271234:express:DatadogStateMachine:acaf1a67-336a-e854-1599-2a627eb2dd8a:c8baf081-31f1-464d-971f-70cb17d01111" ,
1189
+ ) ;
1190
+ expect ( actual ) . toEqual ( "0010001100110000011011011111010000100111100000110000100100101010" ) ;
1191
+ } ) ;
1192
+
1193
+ it ( "test same hashing is generated as logs-backend for execution id # state name # entered time" , ( ) => {
1194
+ const actual = deterministicMd5HashInBinary (
1195
+ "arn:aws:states:sa-east-1:601427271234:express:DatadogStateMachine:acaf1a67-336a-e854-1599-2a627eb2dd8a:c8baf081-31f1-464d-971f-70cb17d01111#step-one#2022-12-08T21:08:19.224Z" ,
1196
+ ) ;
1197
+ expect ( actual ) . toEqual ( "0110111110000000010011011001111101110011100111000000011010100001" ) ;
1198
+ } ) ;
1199
+
1200
+ it ( "test hashing different strings would generate different hashes" , ( ) => {
1201
+ const times = 20 ;
1202
+ for ( let i = 0 ; i < times ; i ++ ) {
1203
+ for ( let j = i + 1 ; j < times ; j ++ ) {
1204
+ expect ( deterministicMd5HashInBinary ( i . toString ( ) ) ) . not . toMatch ( deterministicMd5HashInBinary ( j . toString ( ) ) ) ;
1205
+ }
1206
+ }
1207
+ } ) ;
1208
+
1209
+ it ( "test always leading with 0" , ( ) => {
1210
+ for ( let i = 0 ; i < 20 ; i ++ ) {
1211
+ expect ( deterministicMd5HashInBinary ( i . toString ( ) ) . substring ( 0 , 1 ) ) . toMatch ( "0" ) ;
1212
+ }
1213
+ } ) ;
1214
+ } ) ;
1215
+
1216
+ describe ( "test_deterministicMd5HashToBigIntString" , ( ) => {
1217
+ it ( "test same hashing number is generated as logs-backend for a random string" , ( ) => {
1218
+ const actual = deterministicMd5HashToBigIntString ( "some_testing_random_string" ) ;
1219
+ expect ( actual ) . toEqual ( "2251275791555400689" ) ;
1220
+ } ) ;
1221
+
1222
+ it ( "test same hashing number is generated as logs-backend for execution id # state name # entered time" , ( ) => {
1223
+ const actual = deterministicMd5HashToBigIntString (
1224
+ "arn:aws:states:sa-east-1:601427271234:express:DatadogStateMachine:acaf1a67-336a-e854-1599-2a627eb2dd8a:c8baf081-31f1-464d-971f-70cb17d01111#step-one#2022-12-08T21:08:19.224Z" ,
1225
+ ) ;
1226
+ expect ( actual ) . toEqual ( "8034507082463708833" ) ;
1227
+ } ) ;
1228
+ } ) ;
0 commit comments