Debian Codegraph @

aws_xray_sdk/core/plugins/ec2_plugin.py

raw
1 import logging
2 import requests
3
4 log = logging.getLogger(__name__)
5
6 SERVICE_NAME = 'ec2'
7 ORIGIN = 'AWS::EC2::Instance'
8
9
10 def initialize():
11 """
12 Try to get EC2 instance-id and AZ if running on EC2
13 by querying http://169.254.169.254/latest/meta-data/.
14 If not continue.
15 """
16 global runtime_context
17
18 try:
19 runtime_context = {}
20
21 r = requests.get('http://169.254.169.254/latest/meta-data/instance-id', timeout=1)
22 runtime_context['instance_id'] = r.text
23
24 r = requests.get('http://169.254.169.254/latest/meta-data/placement/availability-zone', timeout=1)
25 runtime_context['availability_zone'] = r.text
26
27 except Exception:
28 runtime_context = None
29 log.warning("failed to get ec2 instance metadata.")