-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdatadog_wrapper
126 lines (103 loc) · 4.91 KB
/
datadog_wrapper
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
#!/bin/bash
# This is a wrapper script that is intended be used in conjunction with the
# AWS_LAMBDA_EXEC_WRAPPER environment variable on an AWS Lambda function. It
# enables universal instrumentation support for our Java and .NET runtimes.
args=("$@")
# lowercase DD_LOG_LEVEL
DD_LOG_LEVEL=$(echo "$DD_LOG_LEVEL" | tr '[:upper:]' '[:lower:]')
# debug_log is a variadic function that prints a message to stdout if the log level is debug
debug_log() {
if [ "$DD_LOG_LEVEL" == "debug" ]
then
echo "[datadog-wrapper]" "$@"
fi
}
DD_SERVERLESS_APPSEC_ENABLED=$(echo "$DD_SERVERLESS_APPSEC_ENABLED" | tr '[:upper:]' '[:lower:]')
if [ "$DD_EXPERIMENTAL_ENABLE_PROXY" == "true" ] || [[ "$DD_SERVERLESS_APPSEC_ENABLED" =~ ^(1|t|true)$ ]]
then
debug_log "Enabling Datadog's Runtime API proxy"
debug_log "The original AWS_LAMBDA_RUNTIME_API value is $AWS_LAMBDA_RUNTIME_API"
# Replace the Runtime API address with the proxy address of the extension
export AWS_LAMBDA_RUNTIME_API="127.0.0.1:9000"
debug_log "Rerouting AWS_LAMBDA_RUNTIME_API to the Datadog extension at $AWS_LAMBDA_RUNTIME_API"
fi
if [[ "$DD_SERVERLESS_APPSEC_ENABLED" =~ ^(1|t|true)$ ]]
then
debug_log "Enabling Datadog Application Security Management"
# Enable the library's instrumentation telemetry needed for ASM OSS VM
export DD_INSTRUMENTATION_TELEMETRY_ENABLED="${DD_INSTRUMENTATION_TELEMETRY_ENABLED:-true}" # the standard env var to enable telemetry
export DD_TRACE_TELEMETRY_ENABLED="${DD_TRACE_TELEMETRY_ENABLED:-true}" # but dd-trace-js < v4.18.0 uses this other one
# Automatically enable the library's APM tracing required by ASM in order to slightly ease the onboarding experience
export DD_TRACE_ENABLED="true"
fi
debug_log "The runtime is $AWS_EXECUTION_ENV"
# if it is .NET
if [[ "$AWS_EXECUTION_ENV" == *"dotnet"* ]]
then
debug_log "Configuring for the .NET runtime!"
if [ -z "$CORECLR_PROFILER_PATH" ]; then
# CORECLR_PROFILER_PATH is not set, try to find the library in these paths, in order:
PROFILER_PATHS=(
# use the shared loader if it's present
"/opt/datadog/linux-x64/Datadog.Trace.ClrProfiler.Native.so"
"/opt/datadog/linux-arm64/Datadog.Trace.ClrProfiler.Native.so"
# if shared loader is not found, use the tracer library directly (not recommended)
"/opt/datadog/linux-x64/Datadog.Tracer.Native.so"
"/opt/datadog/linux-arm64/Datadog.Tracer.Native.so"
# keep this as a fallback for backwards compatibility with older .NET layers
"/opt/datadog/Datadog.Trace.ClrProfiler.Native.so"
)
PROFILER_FOUND=false
## Search from all possible places
for PROFILER_PATH in "${PROFILER_PATHS[@]}"
do
if [ -f "$PROFILER_PATH" ]
then
export CORECLR_PROFILER_PATH="$PROFILER_PATH"
PROFILER_FOUND=true
break
fi
done
if [ $PROFILER_FOUND == false ]
then
echo ".NET library not found. APM instrumentation may not work correctly."
fi
fi
# Required environment variables for .NET library
export CORECLR_ENABLE_PROFILING="${CORECLR_ENABLE_PROFILING:-"1"}"
export CORECLR_PROFILER="${CORECLR_PROFILER:-"{846F5F1C-F9AE-4B07-969E-05C26BC060D8}"}"
export DD_DOTNET_TRACER_HOME="${DD_DOTNET_TRACER_HOME:-"/opt/datadog"}"
# Disable some .NET library features by default
export DD_TRACE_STARTUP_LOGS="${DD_TRACE_STARTUP_LOGS:-"0"}" # we can't completely disable all logs to disk, but we can at least disable startup logs
export DD_CIVISIBILITY_ENABLED="${DD_CIVISIBILITY_ENABLED:-"0"}" # disable CI Visibility's auto-detect mechanism
# log the environment variables for troubleshooting
debug_log "CORECLR_PROFILER_PATH: $CORECLR_PROFILER_PATH"
debug_log "CORECLR_ENABLE_PROFILING: $CORECLR_ENABLE_PROFILING"
debug_log "CORECLR_PROFILER: $CORECLR_PROFILER"
debug_log "DD_DOTNET_TRACER_HOME: $DD_DOTNET_TRACER_HOME"
debug_log "DD_TRACE_STARTUP_LOGS: $DD_TRACE_STARTUP_LOGS"
debug_log "DD_CIVISIBILITY_ENABLED: $DD_CIVISIBILITY_ENABLED"
fi # .NET
# if it is java
DD_Agent_Jar=/opt/java/lib/dd-java-agent.jar
if [[ "$AWS_EXECUTION_ENV" == *"java"* ]] && [ -f "$DD_Agent_Jar" ]
then
debug_log "Configuring for the Java runtime!"
export DD_JMXFETCH_ENABLED="false"
export DD_RUNTIME_METRICS_ENABLED="false"
export DD_REMOTE_CONFIG_ENABLED="false"
export DD_APPSEC_ENABLED="false"
# Removes the -XX:-TieredCompilation flag from the java command passed
# through from the Lambda runtime. Allows the JVM to use the C1 compiler
# and Interpreter, which is much faster on cold start and
# uses less memory.
START_COMMAND=${args[0]}
REMAINDER_ARGS=("${args[@]:1}")
for index in "${!REMAINDER_ARGS[@]}" ; do
[[ ${REMAINDER_ARGS[$index]} == "-XX:-TieredCompilation" ]] && unset -v 'REMAINDER_ARGS[$index]' ;
done
REMAINDER_ARGS="${REMAINDER_ARGS[@]}"
# -XX:TieredStopAtLevel=1 tells the compiler to stop at the C1 compiler
args=($START_COMMAND -javaagent:$DD_Agent_Jar -XX:TieredStopAtLevel=1 ${REMAINDER_ARGS[@]})
fi
exec "${args[@]}"