github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/registered_span_test.go (about) 1 // (c) Copyright IBM Corp. 2021 2 // (c) Copyright Instana Inc. 2021 3 4 package instana_test 5 6 import ( 7 "errors" 8 "strings" 9 "testing" 10 11 "github.com/instana/testify/assert" 12 instana "github.com/mier85/go-sensor" 13 "github.com/opentracing/opentracing-go" 14 ) 15 16 func TestRegisteredSpanType_ExtractData(t *testing.T) { 17 examples := map[string]struct { 18 Operation string 19 Expected interface{} 20 }{ 21 "net/http.Server": { 22 Operation: "g.http", 23 Expected: instana.HTTPSpanData{}, 24 }, 25 "net/http.Client": { 26 Operation: "http", 27 Expected: instana.HTTPSpanData{}, 28 }, 29 "golang.google.org/gppc.Server": { 30 Operation: "rpc-server", 31 Expected: instana.RPCSpanData{}, 32 }, 33 "github.com/Shopify/sarama": { 34 Operation: "kafka", 35 Expected: instana.KafkaSpanData{}, 36 }, 37 "sdk": { 38 Operation: "test", 39 Expected: instana.SDKSpanData{}, 40 }, 41 "aws lambda": { 42 Operation: "aws.lambda.entry", 43 Expected: instana.AWSLambdaSpanData{}, 44 }, 45 "aws s3": { 46 Operation: "s3", 47 Expected: instana.AWSS3SpanData{}, 48 }, 49 "aws sqs": { 50 Operation: "sqs", 51 Expected: instana.AWSSQSSpanData{}, 52 }, 53 "aws sns": { 54 Operation: "sns", 55 Expected: instana.AWSSNSSpanData{}, 56 }, 57 "aws dynamodb": { 58 Operation: "dynamodb", 59 Expected: instana.AWSDynamoDBSpanData{}, 60 }, 61 "aws invoke": { 62 Operation: "aws.lambda.invoke", 63 Expected: instana.AWSLambdaInvokeSpanData{}, 64 }, 65 "logger": { 66 Operation: "log.go", 67 Expected: instana.LogSpanData{}, 68 }, 69 "mongodb": { 70 Operation: "mongo", 71 Expected: instana.MongoDBSpanData{}, 72 }, 73 "postgresql": { 74 Operation: "postgres", 75 Expected: instana.PostgreSQLSpanData{}, 76 }, 77 "redis": { 78 Operation: "redis", 79 Expected: instana.RedisSpanData{}, 80 }, 81 "rabbitmq": { 82 Operation: "rabbitmq", 83 Expected: instana.RabbitMQSpanData{}, 84 }, 85 } 86 87 for name, example := range examples { 88 t.Run(name, func(t *testing.T) { 89 recorder := instana.NewTestRecorder() 90 tracer := instana.NewTracerWithEverything(&instana.Options{}, recorder) 91 92 sp := tracer.StartSpan(example.Operation) 93 sp.Finish() 94 95 spans := recorder.GetQueuedSpans() 96 assert.Equal(t, 1, len(spans)) 97 span := spans[0] 98 99 assert.IsType(t, example.Expected, span.Data) 100 }) 101 } 102 } 103 104 func TestNewAWSLambdaSpanData(t *testing.T) { 105 examples := map[string]struct { 106 Tags opentracing.Tags 107 Expected instana.AWSLambdaSpanData 108 }{ 109 "aws:api.gateway": { 110 Tags: opentracing.Tags{ 111 "http.protocol": "https", 112 "http.url": "https://example.com/lambda", 113 "http.host": "example.com", 114 "http.method": "GET", 115 "http.path": "/lambda", 116 "http.params": "q=test&secret=classified", 117 "http.header": map[string]string{"x-custom-header-1": "test"}, 118 "http.status": 404, 119 "http.error": "Not Found", 120 }, 121 Expected: instana.AWSLambdaSpanData{ 122 Snapshot: instana.AWSLambdaSpanTags{ 123 ARN: "lambda-arn-1", 124 Runtime: "go", 125 Name: "test-lambda", 126 Version: "42", 127 Trigger: "aws:api.gateway", 128 ColdStart: true, 129 MillisecondsLeft: 5, 130 Error: "Not Found", 131 }, 132 HTTP: &instana.HTTPSpanTags{ 133 URL: "https://example.com/lambda", 134 Status: 404, 135 Method: "GET", 136 Path: "/lambda", 137 Params: "q=test&secret=classified", 138 Headers: map[string]string{"x-custom-header-1": "test"}, 139 Host: "example.com", 140 Protocol: "https", 141 Error: "Not Found", 142 }, 143 }, 144 }, 145 "aws:application.load.balancer": { 146 Tags: opentracing.Tags{ 147 "http.protocol": "https", 148 "http.url": "https://example.com/lambda", 149 "http.host": "example.com", 150 "http.method": "GET", 151 "http.path": "/lambda", 152 "http.params": "q=test&secret=classified", 153 "http.header": map[string]string{"x-custom-header-1": "test"}, 154 "http.status": 404, 155 "http.error": "Not Found", 156 }, 157 Expected: instana.AWSLambdaSpanData{ 158 Snapshot: instana.AWSLambdaSpanTags{ 159 ARN: "lambda-arn-1", 160 Runtime: "go", 161 Name: "test-lambda", 162 Version: "42", 163 Trigger: "aws:application.load.balancer", 164 ColdStart: true, 165 MillisecondsLeft: 5, 166 Error: "Not Found", 167 }, 168 HTTP: &instana.HTTPSpanTags{ 169 URL: "https://example.com/lambda", 170 Status: 404, 171 Method: "GET", 172 Path: "/lambda", 173 Params: "q=test&secret=classified", 174 Headers: map[string]string{"x-custom-header-1": "test"}, 175 Host: "example.com", 176 Protocol: "https", 177 Error: "Not Found", 178 }, 179 }, 180 }, 181 "aws:cloudwatch.events": { 182 Tags: opentracing.Tags{ 183 "cloudwatch.events.id": "cw-event-1", 184 "cloudwatch.events.resources": []string{ 185 "res1", 186 strings.Repeat("long ", 40) + "res2", 187 "res3", 188 "res4", 189 }, 190 }, 191 Expected: instana.AWSLambdaSpanData{ 192 Snapshot: instana.AWSLambdaSpanTags{ 193 ARN: "lambda-arn-1", 194 Runtime: "go", 195 Name: "test-lambda", 196 Version: "42", 197 Trigger: "aws:cloudwatch.events", 198 ColdStart: true, 199 MillisecondsLeft: 5, 200 Error: "Not Found", 201 CloudWatch: &instana.AWSLambdaCloudWatchSpanTags{ 202 Events: &instana.AWSLambdaCloudWatchEventTags{ 203 ID: "cw-event-1", 204 Resources: []string{ 205 "res1", 206 strings.Repeat("long ", 40), 207 "res3", 208 }, 209 More: true, 210 }, 211 }, 212 }, 213 }, 214 }, 215 "aws:cloudwatch.logs": { 216 Tags: opentracing.Tags{ 217 "cloudwatch.logs.group": "cw-log-group-1", 218 "cloudwatch.logs.stream": "cw-log-stream-1", 219 "cloudwatch.logs.events": []string{ 220 "log1", 221 strings.Repeat("long ", 40) + "log2", 222 "log3", 223 "log4", 224 }, 225 "cloudwatch.logs.decodingError": errors.New("none"), 226 }, 227 Expected: instana.AWSLambdaSpanData{ 228 Snapshot: instana.AWSLambdaSpanTags{ 229 ARN: "lambda-arn-1", 230 Runtime: "go", 231 Name: "test-lambda", 232 Version: "42", 233 Trigger: "aws:cloudwatch.logs", 234 ColdStart: true, 235 MillisecondsLeft: 5, 236 Error: "Not Found", 237 CloudWatch: &instana.AWSLambdaCloudWatchSpanTags{ 238 Logs: &instana.AWSLambdaCloudWatchLogsTags{ 239 Group: "cw-log-group-1", 240 Stream: "cw-log-stream-1", 241 Events: []string{ 242 "log1", 243 strings.Repeat("long ", 40), 244 "log3", 245 }, 246 More: true, 247 DecodingError: "none", 248 }, 249 }, 250 }, 251 }, 252 }, 253 "aws:s3": { 254 Tags: opentracing.Tags{ 255 "s3.events": []instana.AWSS3EventTags{ 256 {Name: "event1", Bucket: "bucket1", Object: "object1"}, 257 {Name: "event2", Bucket: "bucket2", Object: strings.Repeat("long ", 40) + "object2"}, 258 {Name: "event3", Bucket: "bucket3"}, 259 {Name: "event4", Bucket: "bucket4"}, 260 }, 261 }, 262 Expected: instana.AWSLambdaSpanData{ 263 Snapshot: instana.AWSLambdaSpanTags{ 264 ARN: "lambda-arn-1", 265 Runtime: "go", 266 Name: "test-lambda", 267 Version: "42", 268 Trigger: "aws:s3", 269 ColdStart: true, 270 MillisecondsLeft: 5, 271 Error: "Not Found", 272 S3: &instana.AWSLambdaS3SpanTags{ 273 Events: []instana.AWSS3EventTags{ 274 {Name: "event1", Bucket: "bucket1", Object: "object1"}, 275 {Name: "event2", Bucket: "bucket2", Object: strings.Repeat("long ", 40)}, 276 {Name: "event3", Bucket: "bucket3"}, 277 }, 278 }, 279 }, 280 }, 281 }, 282 "aws:sqs": { 283 Tags: opentracing.Tags{ 284 "sqs.messages": []instana.AWSSQSMessageTags{{Queue: "q1"}, {Queue: "q2"}, {Queue: "q3"}, {Queue: "q4"}}, 285 }, 286 Expected: instana.AWSLambdaSpanData{ 287 Snapshot: instana.AWSLambdaSpanTags{ 288 ARN: "lambda-arn-1", 289 Runtime: "go", 290 Name: "test-lambda", 291 Version: "42", 292 Trigger: "aws:sqs", 293 ColdStart: true, 294 MillisecondsLeft: 5, 295 Error: "Not Found", 296 SQS: &instana.AWSLambdaSQSSpanTags{ 297 Messages: []instana.AWSSQSMessageTags{{Queue: "q1"}, {Queue: "q2"}, {Queue: "q3"}}, 298 }, 299 }, 300 }, 301 }, 302 } 303 304 for trigger, example := range examples { 305 t.Run(trigger, func(t *testing.T) { 306 recorder := instana.NewTestRecorder() 307 tracer := instana.NewTracerWithEverything(&instana.Options{}, recorder) 308 309 sp := tracer.StartSpan("aws.lambda.entry", opentracing.Tags{ 310 "lambda.arn": "lambda-arn-1", 311 "lambda.name": "test-lambda", 312 "lambda.version": "42", 313 "lambda.trigger": trigger, 314 "lambda.coldStart": true, 315 "lambda.msleft": 5, 316 "lambda.error": "Not Found", 317 }, example.Tags) 318 sp.Finish() 319 320 spans := recorder.GetQueuedSpans() 321 assert.Equal(t, 1, len(spans)) 322 span := spans[0] 323 324 assert.Equal(t, "aws.lambda.entry", span.Name) 325 assert.Equal(t, example.Expected, span.Data) 326 }) 327 } 328 }