Exemple #1
0
def test_non_initialized():
    # Context that hasn't been initialized by lambda container should not add subsegments to the facade segment.
    temp_header_var = os.environ[lambda_launcher.LAMBDA_TRACE_HEADER_KEY]
    del os.environ[lambda_launcher.LAMBDA_TRACE_HEADER_KEY]

    temp_context = lambda_launcher.LambdaContext()
    facade_segment = temp_context.get_trace_entity()
    subsegment = Subsegment("TestSubsegment", "local", facade_segment)
    temp_context.put_subsegment(subsegment)

    assert temp_context.get_trace_entity() == facade_segment

    # "Lambda" container added metadata now. Should see subsegment now.
    os.environ[lambda_launcher.LAMBDA_TRACE_HEADER_KEY] = temp_header_var
    temp_context.put_subsegment(subsegment)

    assert temp_context.get_trace_entity() == subsegment
def test_lambda_serverless():
    TRACE_ID = '1-5759e988-bd862e3fe1be46a994272793'
    PARENT_ID = '53995c3f42cd8ad8'
    HEADER_VAR = 'Root=%s;Parent=%s;Sampled=1' % (TRACE_ID, PARENT_ID)

    os.environ[lambda_launcher.LAMBDA_TRACE_HEADER_KEY] = HEADER_VAR
    lambda_context = lambda_launcher.LambdaContext()

    new_recorder = get_new_stubbed_recorder()
    new_recorder.configure(service='test', sampling=False, context=lambda_context)
    new_app = Bottle()

    @new_app.route('/subsegment')
    def subsegment_():
        # Test in between request and make sure Serverless creates a subsegment instead of a segment.
        # Ensure that the parent segment is a facade segment.
        assert new_recorder.current_subsegment()
        assert type(new_recorder.current_segment()) == facade_segment.FacadeSegment
        return 'ok'

    @new_app.route('/trace_header')
    def trace_header():
        # Ensure trace header is preserved.
        subsegment = new_recorder.current_subsegment()
        header = subsegment.get_origin_trace_header()
        assert header.data['k1'] == 'v1'
        return 'ok'

    plugin = XRayMiddleware(new_recorder)
    plugin._in_lambda_ctx = True
    new_app.install(plugin)

    app_client = WebApp(new_app)

    path = '/subsegment'
    app_client.get(path)
    new_app.get(path)
    segment = recorder.emitter.pop()
    assert not segment  # Segment should be none because it's created and ended by the plugin

    path2 = '/trace_header'
    app_client.get(path2, headers={http.XRAY_HEADER: 'k1=v1'})
Exemple #3
0
    def test_lambda_serverless(self):
        TRACE_ID = '1-5759e988-bd862e3fe1be46a994272793'
        PARENT_ID = '53995c3f42cd8ad8'
        HEADER_VAR = "Root=%s;Parent=%s;Sampled=1" % (TRACE_ID, PARENT_ID)

        os.environ[lambda_launcher.LAMBDA_TRACE_HEADER_KEY] = HEADER_VAR
        lambda_context = lambda_launcher.LambdaContext()

        new_recorder = get_new_stubbed_recorder()
        new_recorder.configure(service='test',
                               sampling=False,
                               context=lambda_context)
        subsegment = new_recorder.begin_subsegment("subsegment")
        assert type(subsegment.parent_segment) == facade_segment.FacadeSegment
        new_recorder.end_subsegment()

        url = reverse('200ok')
        self.client.get(url)
        segment = new_recorder.emitter.pop()
        assert not segment
    def test_lambda_serverless(self):
        TRACE_ID = '1-5759e988-bd862e3fe1be46a994272793'
        PARENT_ID = '53995c3f42cd8ad8'
        HEADER_VAR = "Root=%s;Parent=%s;Sampled=1" % (TRACE_ID, PARENT_ID)

        os.environ[lambda_launcher.LAMBDA_TRACE_HEADER_KEY] = HEADER_VAR
        lambda_context = lambda_launcher.LambdaContext()

        new_recorder = get_new_stubbed_recorder()
        new_recorder.configure(service='test',
                               sampling=False,
                               context=lambda_context)
        subsegment = new_recorder.begin_subsegment("subsegment")
        assert type(subsegment.parent_segment) == facade_segment.FacadeSegment
        new_recorder.end_subsegment()

        url = reverse('200ok')
        self.client.get(url)
        segment = new_recorder.emitter.pop()
        assert not segment

        # Test Fault in Lambda
        url = reverse('500fault')
        try:
            self.client.get(url)
        except Exception:
            pass
        segment = xray_recorder.emitter.pop()
        assert segment.fault

        request = segment.http['request']
        response = segment.http['response']

        assert request['method'] == 'GET'
        assert request['client_ip'] == '127.0.0.1'
        assert response['status'] == 500

        exception = segment.cause['exceptions'][0]
        assert exception.type == 'KeyError'
Exemple #5
0
import os

from aws_xray_sdk import global_sdk_config
import pytest
from aws_xray_sdk.core import lambda_launcher
from aws_xray_sdk.core.models.subsegment import Subsegment

TRACE_ID = '1-5759e988-bd862e3fe1be46a994272793'
PARENT_ID = '53995c3f42cd8ad8'
HEADER_VAR = "Root=%s;Parent=%s;Sampled=1" % (TRACE_ID, PARENT_ID)

os.environ[lambda_launcher.LAMBDA_TRACE_HEADER_KEY] = HEADER_VAR
context = lambda_launcher.LambdaContext()


@pytest.fixture(autouse=True)
def setup():
    yield
    global_sdk_config.set_sdk_enabled(True)


def test_facade_segment_generation():

    segment = context.get_trace_entity()
    assert segment.id == PARENT_ID
    assert segment.trace_id == TRACE_ID
    assert segment.sampled


def test_put_subsegment():