Skip to content

Commit 5c9d454

Browse files
Enable placeholders to start with app insights agent using profiles (#656)
* Add app insights agent as part of worker * Update worker.config.json with profile conditions * Update worker.config.json * Update worker.config.json * Update AI agent version * Updated regex * Updated regex
1 parent f61a038 commit 5c9d454

File tree

3 files changed

+208
-1
lines changed

3 files changed

+208
-1
lines changed

package-pipeline.ps1

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ function StopOnFailedExecution {
99
exit $LastExitCode
1010
}
1111
}
12+
13+
$ApplicationInsightsAgentVersion = '3.4.0'
14+
$ApplicationInsightsAgentFilename = "applicationinsights-agent-${ApplicationInsightsAgentVersion}.jar"
15+
$ApplicationInsightsAgentUrl = "https://repo1.maven.org/maven2/com/microsoft/azure/applicationinsights-agent/${ApplicationInsightsAgentVersion}/${ApplicationInsightsAgentFilename}"
16+
1217
Write-Host "Building azure-functions-java-worker"
1318
mvn clean package --no-transfer-progress -B
1419
StopOnFailedExecution
@@ -24,6 +29,85 @@ StopOnFailedExecution
2429
copy-item ./worker.config.json pkg
2530
copy-item ./tools/AzureFunctionsJavaWorker.nuspec pkg/
2631
copy-item ./annotationLib pkg/annotationLib -Recurse
32+
33+
# Download application insights agent from maven central
34+
$ApplicationInsightsAgentFile = [System.IO.Path]::Combine($PSScriptRoot, $ApplicationInsightsAgentFilename)
35+
36+
# local testing cleanup
37+
if (Test-Path -Path $ApplicationInsightsAgentFile) {
38+
Remove-Item -Path $ApplicationInsightsAgentFile
39+
}
40+
41+
# local testing cleanup
42+
$oldOutput = [System.IO.Path]::Combine($PSScriptRoot, "agent")
43+
if (Test-Path -Path $oldOutput) {
44+
Remove-Item -Path $oldOutput -Recurse
45+
}
46+
47+
# local testing cleanup
48+
$oldExtract = [System.IO.Path]::Combine($PSScriptRoot, "extract")
49+
if (Test-Path -Path $oldExtract) {
50+
Remove-Item -Path $oldExtract -Recurse
51+
}
52+
53+
$extract = new-item -type directory -force $PSScriptRoot\extract
54+
if (-not(Test-Path -Path $extract)) {
55+
echo "Fail to create a new directory $extract"
56+
exit 1
57+
}
58+
59+
echo "Start downloading '$ApplicationInsightsAgentUrl' to '$PSScriptRoot'"
60+
try {
61+
Invoke-WebRequest -Uri $ApplicationInsightsAgentUrl -OutFile $ApplicationInsightsAgentFile
62+
} catch {
63+
echo "An error occurred. Download fails" $ApplicationInsightsAgentFile
64+
echo "Exiting"
65+
exit 1
66+
}
67+
68+
if (-not(Test-Path -Path $ApplicationInsightsAgentFile)) {
69+
echo "$ApplicationInsightsAgentFile do not exist."
70+
exit 1
71+
}
72+
73+
echo "Start extracting content from $ApplicationInsightsAgentFilename to extract folder"
74+
cd -Path $extract -PassThru
75+
jar xf $ApplicationInsightsAgentFile
76+
cd $PSScriptRoot
77+
echo "Done extracting"
78+
79+
echo "Unsign $ApplicationInsightsAgentFilename"
80+
Remove-Item $extract\META-INF\MSFTSIG.*
81+
$manifest = "$extract\META-INF\MANIFEST.MF"
82+
$newContent = (Get-Content -Raw $manifest | Select-String -Pattern '(?sm)^(.*?\r?\n)\r?\n').Matches[0].Groups[1].Value
83+
Set-Content -Path $manifest $newContent
84+
85+
Remove-Item $ApplicationInsightsAgentFile
86+
if (-not(Test-Path -Path $ApplicationInsightsAgentFile)) {
87+
echo "Delete the original $ApplicationInsightsAgentFilename successfully"
88+
} else {
89+
echo "Fail to delete original source $ApplicationInsightsAgentFilename"
90+
exit 1
91+
}
92+
93+
$agent = new-item -type directory -force $PSScriptRoot\agent
94+
$filename = "applicationinsights-agent.jar"
95+
$result = [System.IO.Path]::Combine($agent, $filename)
96+
echo "re-jar $filename"
97+
98+
cd -Path $extract -PassThru
99+
jar cfm $result META-INF/MANIFEST.MF .
100+
101+
if (-not(Test-Path -Path $result)) {
102+
echo "Fail to re-archive $filename"
103+
exit 1
104+
}
105+
Write-Host "Creating the functions.codeless file"
106+
New-Item -path $PSScriptRoot\agent -type file -name "functions.codeless"
107+
108+
cd $PSScriptRoot
109+
Copy-Item $PSScriptRoot/agent $PSScriptRoot/pkg/agent -Recurse -Verbose
110+
27111
set-location pkg
28112
nuget pack -Properties version=$buildNumber
29113
set-location ..

setup-tests-pipeline.ps1

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ else
2929

3030
$FUNC_CLI_DIRECTORY = Join-Path $PSScriptRoot 'Azure.Functions.Cli'
3131

32+
$ApplicationInsightsAgentVersion = '3.4.0'
33+
$ApplicationInsightsAgentFilename = "applicationinsights-agent-${ApplicationInsightsAgentVersion}.jar"
34+
$ApplicationInsightsAgentUrl = "https://repo1.maven.org/maven2/com/microsoft/azure/applicationinsights-agent/${ApplicationInsightsAgentVersion}/${ApplicationInsightsAgentFilename}"
35+
3236
Write-Host 'Deleting the Core Tools if exists...'
3337
Remove-Item -Force "$FUNC_CLI_DIRECTORY.zip" -ErrorAction Ignore
3438
Remove-Item -Recurse -Force $FUNC_CLI_DIRECTORY -ErrorAction Ignore
@@ -55,4 +59,82 @@ if (-not $UseCoreToolsBuildFromIntegrationTests.IsPresent)
5559
Write-Host "Copying worker.config.json and annotationLib to worker directory"
5660
Copy-Item "$PSScriptRoot/annotationLib" "$FUNC_CLI_DIRECTORY/workers/java/annotationLib" -Recurse -Verbose
5761

62+
# Download application insights agent from maven central
63+
$ApplicationInsightsAgentFile = [System.IO.Path]::Combine($PSScriptRoot, $ApplicationInsightsAgentFilename)
64+
65+
# local testing cleanup
66+
if (Test-Path -Path $ApplicationInsightsAgentFile) {
67+
Remove-Item -Path $ApplicationInsightsAgentFile
68+
}
69+
70+
# local testing cleanup
71+
$oldOutput = [System.IO.Path]::Combine($PSScriptRoot, "agent")
72+
if (Test-Path -Path $oldOutput) {
73+
Remove-Item -Path $oldOutput -Recurse
74+
}
75+
76+
# local testing cleanup
77+
$oldExtract = [System.IO.Path]::Combine($PSScriptRoot, "extract")
78+
if (Test-Path -Path $oldExtract) {
79+
Remove-Item -Path $oldExtract -Recurse
80+
}
81+
82+
echo "Start downloading '$ApplicationInsightsAgentUrl' to '$PSScriptRoot'"
83+
try {
84+
Invoke-WebRequest -Uri $ApplicationInsightsAgentUrl -OutFile $ApplicationInsightsAgentFile
85+
} catch {
86+
echo "An error occurred. Download fails" $ApplicationInsightsAgentFile
87+
echo "Exiting"
88+
exit 1
89+
}
90+
91+
if (-not(Test-Path -Path $ApplicationInsightsAgentFile)) {
92+
echo "$ApplicationInsightsAgentFile do not exist."
93+
exit 1
94+
}
95+
96+
$extract = new-item -type directory -force $PSScriptRoot\extract
97+
if (-not(Test-Path -Path $extract)) {
98+
echo "Fail to create a new directory $extract"
99+
exit 1
100+
}
101+
102+
echo "Start extracting content from $ApplicationInsightsAgentFilename to extract folder"
103+
cd -Path $extract -PassThru
104+
jar xf $ApplicationInsightsAgentFile
105+
cd $PSScriptRoot
106+
echo "Done extracting"
107+
108+
echo "Unsign $ApplicationInsightsAgentFilename"
109+
Remove-Item $extract\META-INF\MSFTSIG.*
110+
$manifest = "$extract\META-INF\MANIFEST.MF"
111+
$newContent = (Get-Content -Raw $manifest | Select-String -Pattern '(?sm)^(.*?\r?\n)\r?\n').Matches[0].Groups[1].Value
112+
Set-Content -Path $manifest $newContent
113+
114+
Remove-Item $ApplicationInsightsAgentFile
115+
if (-not(Test-Path -Path $ApplicationInsightsAgentFile)) {
116+
echo "Delete the original $ApplicationInsightsAgentFilename successfully"
117+
} else {
118+
echo "Fail to delete original source $ApplicationInsightsAgentFilename"
119+
exit 1
120+
}
121+
122+
$agent = new-item -type directory -force $PSScriptRoot\agent
123+
$filename = "applicationinsights-agent.jar"
124+
$result = [System.IO.Path]::Combine($agent, $filename)
125+
echo "re-jar $filename"
126+
127+
cd -Path $extract -PassThru
128+
jar cfm $result META-INF/MANIFEST.MF .
129+
130+
if (-not(Test-Path -Path $result)) {
131+
echo "Fail to re-archive $filename"
132+
exit 1
133+
}
134+
135+
Write-Host "Creating the functions.codeless file"
136+
New-Item -path $PSScriptRoot\agent -type file -name "functions.codeless"
137+
138+
Write-Host "Copying the unsigned Application Insights Agent to worker directory"
139+
Copy-Item "$PSScriptRoot/agent" "$FUNC_CLI_DIRECTORY/workers/java/agent" -Recurse -Verbose
58140
}

worker.config.json

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,46 @@
55
"defaultExecutablePath": "%JAVA_HOME%/bin/java",
66
"defaultWorkerPath": "azure-functions-java-worker.jar",
77
"arguments": ["-XX:+TieredCompilation -XX:TieredStopAtLevel=1 -noverify -Djava.net.preferIPv4Stack=true -jar", "%JAVA_OPTS%", "%AZURE_FUNCTIONS_MESH_JAVA_OPTS%"]
8-
}
8+
},
9+
"profiles":
10+
[
11+
{
12+
"profileName":"AppInsightsOptOutWindows",
13+
"conditions":[
14+
{"conditionType":"environment","conditionName":"languageWorkers:java:arguments","conditionExpression":".*-DaiAgentOptOut.*"},
15+
{"conditionType":"hostProperty","conditionName":"platform","conditionExpression":"WINDOWS"}
16+
],
17+
"description":{
18+
"arguments": ["-XX:+TieredCompilation -XX:TieredStopAtLevel=1 -noverify -Djava.net.preferIPv4Stack=true -jar", "%JAVA_OPTS%", "%AZURE_FUNCTIONS_MESH_JAVA_OPTS%"]
19+
}
20+
},
21+
{
22+
"profileName":"AppInsightsOptOutLinux",
23+
"conditions":[
24+
{"conditionType":"environment","conditionName":"languageWorkers__java__arguments","conditionExpression":".*-DaiAgentOptOut.*"},
25+
{"conditionType":"hostProperty","conditionName":"platform","conditionExpression":"LINUX"}
26+
],
27+
"description":{
28+
"arguments": ["-XX:+TieredCompilation -XX:TieredStopAtLevel=1 -noverify -Djava.net.preferIPv4Stack=true -jar", "%JAVA_OPTS%", "%AZURE_FUNCTIONS_MESH_JAVA_OPTS%"]
29+
}
30+
},
31+
{
32+
"profileName":"AppInsightsPlaceholder",
33+
"conditions":[
34+
{"conditionType":"environment","conditionName":"INITIALIZED_FROM_PLACEHOLDER","conditionExpression":"(?i)true$"}
35+
],
36+
"description":{
37+
"arguments":["-XX:+TieredCompilation -XX:TieredStopAtLevel=1 -noverify -Djava.net.preferIPv4Stack=true -javaagent:\"{workerDirectoryPath}/agent/applicationinsights-agent.jar\" -DLazySetOptIn=false -jar", "%JAVA_OPTS%", "%AZURE_FUNCTIONS_MESH_JAVA_OPTS%"]
38+
}
39+
},
40+
{
41+
"profileName":"AppInsightsNoPlaceholder",
42+
"conditions":[
43+
{"conditionType":"environment","conditionName":"APPLICATIONINSIGHTS_ENABLE_AGENT","conditionExpression":"(?i)true$"}
44+
],
45+
"description":{
46+
"arguments": ["-XX:+TieredCompilation -XX:TieredStopAtLevel=1 -noverify -Djava.net.preferIPv4Stack=true -javaagent:\"{workerDirectoryPath}/agent/applicationinsights-agent.jar\" -jar", "%JAVA_OPTS%", "%AZURE_FUNCTIONS_MESH_JAVA_OPTS%"]
47+
}
48+
}
49+
]
950
}

0 commit comments

Comments
 (0)