Esempio n. 1
0
def _prepare_invocation_for_python_lambda(
    context: InvocationContext, ) -> AdditionalInvocationOptions:
    # Download and initialize Python agent
    _ensure_python_agent_initialized()

    # If agent could not be initialized, skip here
    if not THUNDRA_PYTHON_AGENT_INITIALIZED:
        return None

    result = AdditionalInvocationOptions()

    # Make sure API key is contained in environment
    result.env_updates[THUNDRA_APIKEY_ENV_VAR_NAME] = _get_apikey(
        context.environment)

    # Switch handler to Thundra and pass original handler to Thundra through environment variable
    result.env_updates[
        THUNDRA_AGENT_LAMBDA_HANDLER_ENV_VAR_NAME] = context.handler
    result.updated_handler = "thundra.handler.wrapper"

    # If log disable is not configured explicitly, set it to false to enable log capturing by default
    log_disabled = context.environment.get(
        THUNDRA_AGENT_LOG_DISABLE_ENV_VAR_NAME)
    if not log_disabled:
        result.env_updates[THUNDRA_AGENT_LOG_DISABLE_ENV_VAR_NAME] = "false"

    # Map Thundra agent path into container so it will be accessible by Lambda function Python environment
    agent_path_mapping = "-v %s/:/opt/python/" % THUNDRA_PYTHON_AGENT_LOCAL_PATH_ON_HOST

    if context.docker_flags:
        context.docker_flags = f"{context.docker_flags} {agent_path_mapping}"
    else:
        context.docker_flags = agent_path_mapping

    return result
Esempio n. 2
0
def _prepare_invocation_for_java_lambda(
        context: InvocationContext) -> AdditionalInvocationOptions:
    # Download and initialize Java agent
    _ensure_java_agent_initialized()

    # If agent could not be initialized, skip here
    if not THUNDRA_JAVA_AGENT_INITIALIZED:
        return None

    result = AdditionalInvocationOptions()
    environment = context.environment
    agent_flag = "-javaagent:{agent_path}"

    # Inject Thundra agent path into "JAVA_TOOL_OPTIONS" env var,
    # so it will be automatically loaded on JVM startup
    java_tool_opts = environment.get("JAVA_TOOL_OPTIONS", "")
    if agent_flag not in java_tool_opts:
        java_tool_opts += f" {agent_flag}"
    result.env_updates["JAVA_TOOL_OPTIONS"] = java_tool_opts.strip()

    # Disable CDS (Class Data Sharing),
    # because "-javaagent" cannot be enabled when CDS is enabled on JDK 8.
    # CDS can only be disabled by "_JAVA_OPTIONS" env var,
    # because by default it is enabled ("-Xshare:on")
    # on Lambci by command line parameters and
    # "_JAVA_OPTIONS" has precedence over command line parameters
    # but "JAVA_TOOL_OPTIONS" is not.
    if _is_java8_lambda(context.lambda_function):
        java_opts = environment.get("_JAVA_OPTIONS", "")
        java_opts += " -Xshare:off"
        result.env_updates["_JAVA_OPTIONS"] = java_opts.strip()

    # If log disable is not configured explicitly, set it to false to enable log capturing by default
    log_disabled = environment.get(THUNDRA_AGENT_LOG_DISABLE_ENV_VAR_NAME)
    if not log_disabled:
        result.env_updates[THUNDRA_AGENT_LOG_DISABLE_ENV_VAR_NAME] = "false"

    # Make sure API key is contained in environment
    result.env_updates[THUNDRA_APIKEY_ENV_VAR_NAME] = _get_apikey(environment)

    # Note: The code below doesn't seem to be required, as LAMBDA_EXECUTOR=local also picks up $JAVA_TOOL_OPTIONS
    # if context.lambda_command:
    #     result.updated_command = context.lambda_command.replace(
    #         "java ", f"java {agent_flag} ", 1
    #     )
    # construct agent file path mapping
    result.files_to_add["agent_path"] = THUNDRA_JAVA_AGENT_LOCAL_PATH

    return result