-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathaws_test.rb
More file actions
267 lines (226 loc) · 7.9 KB
/
aws_test.rb
File metadata and controls
267 lines (226 loc) · 7.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# (c) Copyright IBM Corp. 2021
# (c) Copyright Instana Inc. 2021
require 'test_helper'
class AwsTest < Minitest::Test
def setup
clear_all!
end
def test_dynamo_db
dynamo = Aws::DynamoDB::Client.new(
region: "local",
access_key_id: "placeholder",
secret_access_key: "placeholder",
endpoint: "http://localhost:8000"
)
assert_raises Aws::DynamoDB::Errors::ResourceNotFoundException do
Instana.tracer.in_span(:dynamo_test, attributes: {}) do
dynamo.get_item(
table_name: 'sample_table',
key: { s: 'sample_item' }
)
end
end
spans = ::Instana.processor.queued_spans
dynamo_span, entry_span, *rest = spans
assert rest.empty?
assert_equal entry_span[:s], dynamo_span[:p]
assert_equal :dynamodb, dynamo_span[:n]
assert_equal 'get', dynamo_span[:data][:dynamodb][:op]
assert_equal 'sample_table', dynamo_span[:data][:dynamodb][:table]
end
def test_s3
s3_client = Aws::S3::Client.new(
region: "local",
access_key_id: "minioadmin",
secret_access_key: "minioadmin",
force_path_style: "true",
endpoint: "http://localhost:9000"
)
assert_raises Aws::S3::Errors::NoSuchBucket do
Instana.tracer.in_span(:s3_test, attributes: {}) do
s3_client.get_object(
bucket: 'sample-bucket',
key: 'sample_key'
)
end
end
spans = ::Instana.processor.queued_spans
s3_span, entry_span, *rest = spans
assert rest.empty?
assert_equal entry_span[:s], s3_span[:p]
assert_equal :s3, s3_span[:n]
assert_equal 'get', s3_span[:data][:s3][:op]
assert_equal 'sample-bucket', s3_span[:data][:s3][:bucket]
assert_equal 'sample_key', s3_span[:data][:s3][:key]
end
def test_sns_publish
sns = Aws::SNS::Client.new(
region: "local",
access_key_id: "test",
secret_access_key: "test",
endpoint: "http://localhost:9911"
)
assert_raises Aws::SNS::Errors::NotFound do
Instana.tracer.in_span(:sns_test, attributes: {}) do
sns.publish(
topic_arn: 'topic:arn',
target_arn: 'target:arn',
phone_number: '555-0100',
subject: 'Test Subject',
message: 'Test Message'
)
end
end
spans = ::Instana.processor.queued_spans
aws_span, entry_span, *rest = spans
assert rest.empty?
assert_equal entry_span[:s], aws_span[:p]
assert_equal :sns, aws_span[:n]
assert_equal 'topic:arn', aws_span[:data][:sns][:topic]
assert_equal 'target:arn', aws_span[:data][:sns][:target]
assert_equal '555-0100', aws_span[:data][:sns][:phone]
assert_equal 'Test Subject', aws_span[:data][:sns][:subject]
end
def test_sns_other
sns = Aws::SNS::Client.new(
region: "local",
access_key_id: "test",
secret_access_key: "test",
endpoint: "http://localhost:9911"
)
Instana.tracer.in_span(:sns_test, attributes: {}) do
sns.list_subscriptions
end
spans = ::Instana.processor.queued_spans
aws_span, entry_span, *rest = spans
assert rest.empty?
assert_equal entry_span[:s], aws_span[:p]
assert_equal :"net-http", aws_span[:n]
end
def test_sqs
sqs = Aws::SQS::Client.new(
region: "local",
access_key_id: "test",
secret_access_key: "test",
endpoint: "http://localhost:9324"
)
create_response = nil
get_url_response = nil
Instana.tracer.in_span(:sqs_test, attributes: {}) do
create_response = sqs.create_queue(queue_name: 'test')
get_url_response = sqs.get_queue_url(queue_name: 'test')
sqs.send_message(queue_url: create_response.queue_url, message_body: 'Sample')
end
received = sqs.receive_message(
queue_url: create_response.queue_url,
message_attribute_names: ['All']
)
sqs.delete_queue(queue_url: create_response.queue_url)
message = received.messages.first
create_span, get_span, send_span, _root = ::Instana.processor.queued_spans
assert_equal :sqs, create_span[:n]
assert_equal create_response.queue_url, create_span[:data][:sqs][:queue]
assert_equal 'exit', create_span[:data][:sqs][:sort]
assert_equal 'create.queue', create_span[:data][:sqs][:type]
assert_equal :sqs, get_span[:n]
assert_equal get_url_response.queue_url, get_span[:data][:sqs][:queue]
assert_equal 'exit', get_span[:data][:sqs][:sort]
assert_equal 'get.queue', get_span[:data][:sqs][:type]
assert_equal :sqs, send_span[:n]
assert_equal get_url_response.queue_url, send_span[:data][:sqs][:queue]
assert_equal 'exit', send_span[:data][:sqs][:sort]
assert_equal 'single.sync', send_span[:data][:sqs][:type]
assert_equal send_span[:t], message.message_attributes['X_INSTANA_T'].string_value
assert_equal send_span[:s], message.message_attributes['X_INSTANA_S'].string_value
assert_equal 'Sample', message.body
end
def test_lambda
stub_request(:post, "https://lambda.local.amazonaws.com/2015-03-31/functions/Test/invocations")
.with(
body: "data",
headers: {
'X-Amz-Client-Context' => /.+/
}
)
.to_return(status: 200, body: "", headers: {})
lambda = Aws::Lambda::Client.new(
endpoint: 'https://lambda.local.amazonaws.com',
region: 'local',
access_key_id: "test",
secret_access_key: "test"
)
Instana.tracer.in_span(:lambda_test, attributes: {}) do
lambda.invoke(
function_name: 'Test',
invocation_type: 'Event',
payload: 'data'
)
end
spans = ::Instana.processor.queued_spans
lambda_span, _entry_span, *rest = spans
assert rest.empty?
assert_equal :"aws.lambda.invoke", lambda_span[:n]
assert_equal 'Test', lambda_span[:data][:aws][:lambda][:invoke][:function]
assert_equal 'Event', lambda_span[:data][:aws][:lambda][:invoke][:type]
assert_equal 200, lambda_span[:data][:http][:status]
assert_nil lambda_span[:ec]
assert_nil lambda_span[:stack]
end
def test_lambda_with_500_status
stub_request(:post, "https://lambda.local.amazonaws.com/2015-03-31/functions/Test/invocations")
.with(
body: "data",
headers: {
'X-Amz-Client-Context' => /.+/
}
)
.to_return(status: 500, body: '{"message": "Internal Server Error" }', headers: {})
lambda = Aws::Lambda::Client.new(
endpoint: 'https://lambda.local.amazonaws.com',
region: 'local',
access_key_id: "test",
secret_access_key: "test"
)
assert_raises(RuntimeError) do
Instana.tracer.in_span(:lambda_test, attributes: {}) do
lambda.invoke(
function_name: 'Test',
invocation_type: 'Event',
payload: 'data'
)
end
end
spans = ::Instana.processor.queued_spans
lambda_span, _entry_span, *rest = spans
assert rest.empty?
assert_equal :"aws.lambda.invoke", lambda_span[:n]
assert_equal 'Test', lambda_span[:data][:aws][:lambda][:invoke][:function]
assert_equal 'Event', lambda_span[:data][:aws][:lambda][:invoke][:type]
refute_nil lambda_span[:ec]
assert_equal 1, lambda_span[:ec]
refute_nil lambda_span[:stack]
assert_equal 30, lambda_span[:stack].length # default limit is 30 in span.add_stack
end
def test_no_error_is_raised_and_no_spans_are_created_when_agent_is_not_ready
error = nil
::Instana.agent.stub(:ready?, false) do
dynamo = Aws::DynamoDB::Client.new(
region: "local",
access_key_id: "placeholder",
secret_access_key: "placeholder",
endpoint: "http://localhost:8000"
)
assert_silent do
dynamo.get_item(
table_name: 'sample_table',
key: { s: 'sample_item' }
)
rescue StandardError => e
error = e
end
end
# Should not raise instrumentation errors, only the expected AWS error
assert error.is_a?(Aws::DynamoDB::Errors::ServiceError)
assert_empty ::Instana.processor.queued_spans
end
end