Skip to content

Commit

Permalink
Gangams/telemetry cloud specific (#1199)
Browse files Browse the repository at this point in the history
* linux agent updates

* windows agent updates

* windows agent updates

* updtes

* refactor the code

* refactor the code

* minor updates
  • Loading branch information
ganga1980 authored Mar 15, 2024
1 parent 947b5c6 commit 7f78af1
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 85 deletions.
85 changes: 57 additions & 28 deletions kubernetes/linux/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,62 @@ startTime=$(date +%s)

echo "startup script start @ $(date +'%Y-%m-%dT%H:%M:%S')"

setCloudSpecificApplicationInsightsConfig() {
echo "setCloudSpecificApplicationInsightsConfig: Cloud environment: $1"
case $1 in
"azurechinacloud")
APPLICATIONINSIGHTS_AUTH="MjkzZWY1MDAtMDJiZS1jZWNlLTk0NmMtNTU3OWNhYjZiYzEzCg=="
APPLICATIONINSIGHTS_ENDPOINT="https://dc.applicationinsights.azure.cn/v2/track"
echo "export APPLICATIONINSIGHTS_AUTH=$APPLICATIONINSIGHTS_AUTH" >>~/.bashrc
echo "export APPLICATIONINSIGHTS_ENDPOINT=$APPLICATIONINSIGHTS_ENDPOINT" >>~/.bashrc
source ~/.bashrc
;;
"azureusgovernmentcloud")
APPLICATIONINSIGHTS_AUTH="ZmQ5MTc2ODktZjlkYi1mNzU3LThiZDQtZDVlODRkNzYxNDQ3Cg=="
APPLICATIONINSIGHTS_ENDPOINT="https://dc.applicationinsights.azure.us/v2/track"
echo "export APPLICATIONINSIGHTS_AUTH=$APPLICATIONINSIGHTS_AUTH" >>~/.bashrc
echo "export APPLICATIONINSIGHTS_ENDPOINT=$APPLICATIONINSIGHTS_ENDPOINT" >>~/.bashrc
source ~/.bashrc
;;
"usnat" | "ussec")
# Check if the instrumentation key needs to be fetched from a storage account (as in airgapped clouds)
if [ ${#APPLICATIONINSIGHTS_AUTH_URL} -ge 1 ]; then # (check if APPLICATIONINSIGHTS_AUTH_URL has length >=1)
for BACKOFF in {1..4}; do
KEY=$(curl -sS $APPLICATIONINSIGHTS_AUTH_URL)
# there's no easy way to get the HTTP status code from curl, so just check if the result is well formatted
if [[ $KEY =~ ^[A-Za-z0-9=]+$ ]]; then
break
else
sleep $((2 ** $BACKOFF / 4)) # (exponential backoff)
fi
done

# validate that the retrieved data is an instrumentation key
if [[ $KEY =~ ^[A-Za-z0-9=]+$ ]]; then
export APPLICATIONINSIGHTS_AUTH=$(echo $KEY)
echo "export APPLICATIONINSIGHTS_AUTH=$APPLICATIONINSIGHTS_AUTH" >>~/.bashrc
echo "Using cloud-specific instrumentation key"
else
# no ikey can be retrieved. Disable telemetry and continue
export DISABLE_TELEMETRY=true
echo "export DISABLE_TELEMETRY=true" >>~/.bashrc
echo "Could not get cloud-specific instrumentation key (network error?). Disabling telemetry"
fi
fi

aikey=$(echo "$APPLICATIONINSIGHTS_AUTH" | base64 -d)
export TELEMETRY_APPLICATIONINSIGHTS_KEY=$aikey
echo "export TELEMETRY_APPLICATIONINSIGHTS_KEY=$aikey" >>~/.bashrc
source ~/.bashrc
;;

*)
echo "default is Public cloud"
;;
esac
}


gracefulShutdown() {
echo "gracefulShutdown start @ $(date +'%Y-%m-%dT%H:%M:%S')"
echo "gracefulShutdown fluent-bit process start @ $(date +'%Y-%m-%dT%H:%M:%S')"
Expand Down Expand Up @@ -534,34 +590,7 @@ echo "export DOMAIN=$DOMAIN" >>~/.bashrc
export WSID=$workspaceId
echo "export WSID=$WSID" >>~/.bashrc

# Check if the instrumentation key needs to be fetched from a storage account (as in airgapped clouds)
if [ ${#APPLICATIONINSIGHTS_AUTH_URL} -ge 1 ]; then # (check if APPLICATIONINSIGHTS_AUTH_URL has length >=1)
for BACKOFF in {1..4}; do
KEY=$(curl -sS $APPLICATIONINSIGHTS_AUTH_URL)
# there's no easy way to get the HTTP status code from curl, so just check if the result is well formatted
if [[ $KEY =~ ^[A-Za-z0-9=]+$ ]]; then
break
else
sleep $((2 ** $BACKOFF / 4)) # (exponential backoff)
fi
done

# validate that the retrieved data is an instrumentation key
if [[ $KEY =~ ^[A-Za-z0-9=]+$ ]]; then
export APPLICATIONINSIGHTS_AUTH=$(echo $KEY)
echo "export APPLICATIONINSIGHTS_AUTH=$APPLICATIONINSIGHTS_AUTH" >>~/.bashrc
echo "Using cloud-specific instrumentation key"
else
# no ikey can be retrieved. Disable telemetry and continue
export DISABLE_TELEMETRY=true
echo "export DISABLE_TELEMETRY=true" >>~/.bashrc
echo "Could not get cloud-specific instrumentation key (network error?). Disabling telemetry"
fi
fi

aikey=$(echo $APPLICATIONINSIGHTS_AUTH | base64 -d)
export TELEMETRY_APPLICATIONINSIGHTS_KEY=$aikey
echo "export TELEMETRY_APPLICATIONINSIGHTS_KEY=$aikey" >>~/.bashrc
setCloudSpecificApplicationInsightsConfig "$CLOUD_ENVIRONMENT"

source ~/.bashrc
cat packages_version.txt
Expand Down
145 changes: 88 additions & 57 deletions kubernetes/windows/main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,93 @@ function Set-ProcessAndMachineEnvVariables($name, $value) {
[System.Environment]::SetEnvironmentVariable($name, $value, "Machine")
}

function Set-AirgapCloudSpecificApplicationInsightsConfig {
# Need to do this before the SA fetch for AI key for airgapped clouds so that it is not overwritten with defaults.
$appInsightsAuth = [System.Environment]::GetEnvironmentVariable("APPLICATIONINSIGHTS_AUTH", "process")
if (![string]::IsNullOrEmpty($appInsightsAuth)) {
[System.Environment]::SetEnvironmentVariable("APPLICATIONINSIGHTS_AUTH", $appInsightsAuth, "machine")
Write-Host "Successfully set environment variable APPLICATIONINSIGHTS_AUTH - $($appInsightsAuth) for target 'machine'..."
}
else {
Write-Host "Failed to set environment variable APPLICATIONINSIGHTS_AUTH for target 'machine' since it is either null or empty"
}

$appInsightsEndpoint = [System.Environment]::GetEnvironmentVariable("APPLICATIONINSIGHTS_ENDPOINT", "process")
if (![string]::IsNullOrEmpty($appInsightsEndpoint)) {
[System.Environment]::SetEnvironmentVariable("APPLICATIONINSIGHTS_ENDPOINT", $appInsightsEndpoint, "machine")
Write-Host "Successfully set environment variable APPLICATIONINSIGHTS_ENDPOINT - $($appInsightsEndpoint) for target 'machine'..."
}

# Check if the instrumentation key needs to be fetched from a storage account (as in airgapped clouds)
$aiKeyURl = [System.Environment]::GetEnvironmentVariable('APPLICATIONINSIGHTS_AUTH_URL')
if ($aiKeyURl) {
$aiKeyFetched = ""
# retry up to 5 times
for ( $i = 1; $i -le 4; $i++) {
try {
$response = Invoke-WebRequest -uri $aiKeyURl -UseBasicParsing -TimeoutSec 5 -ErrorAction:Stop

if ($response.StatusCode -ne 200) {
Write-Host "Expecting reponse code 200, was: $($response.StatusCode), retrying"
Start-Sleep -Seconds ([MATH]::Pow(2, $i) / 4)
}
else {
$aiKeyFetched = $response.Content
break
}
}
catch {
Write-Host "Exception encountered fetching instrumentation key:"
Write-Host $_.Exception
}
}

# Check if the fetched IKey was properly encoded. if not then turn off telemetry
if ($aiKeyFetched -match '^[A-Za-z0-9=]+$') {
Write-Host "Using cloud-specific instrumentation key"
Set-ProcessAndMachineEnvVariables "APPLICATIONINSIGHTS_AUTH" $aiKeyFetched
}
else {
# Couldn't fetch the Ikey, turn telemetry off
Write-Host "Could not get cloud-specific instrumentation key (network error?). Disabling telemetry"
Set-ProcessAndMachineEnvVariables "DISABLE_TELEMETRY" "True"
}
}

$aiKeyDecoded = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($env:APPLICATIONINSIGHTS_AUTH))
Set-ProcessAndMachineEnvVariables "TELEMETRY_APPLICATIONINSIGHTS_KEY" $aiKeyDecoded
}
function Set-CloudSpecificApplicationInsightsConfig {
param (
[string]$CloudEnvironment
)
Write-Host "Set-CloudSpecificApplicationInsightsConfig: Cloud environment: $CloudEnvironment"
switch ($CloudEnvironment) {
"azurechinacloud" {
Write-Host "Set-CloudSpecificApplicationInsightsConfig: Setting Application Insights configuration for Azure China Cloud"
Set-ProcessAndMachineEnvVariables "APPLICATIONINSIGHTS_AUTH" "MjkzZWY1MDAtMDJiZS1jZWNlLTk0NmMtNTU3OWNhYjZiYzEzCg=="
Set-ProcessAndMachineEnvVariables "APPLICATIONINSIGHTS_ENDPOINT" "https://dc.applicationinsights.azure.cn/v2/track"
Write-Host "Set-CloudSpecificApplicationInsightsConfig: Application Insights configuration set for Azure China Cloud"
}
"azureusgovernmentcloud" {
Write-Host "Set-CloudSpecificApplicationInsightsConfig: Setting Application Insights configuration for Azure US Government Cloud"
Set-ProcessAndMachineEnvVariables "APPLICATIONINSIGHTS_AUTH" "ZmQ5MTc2ODktZjlkYi1mNzU3LThiZDQtZDVlODRkNzYxNDQ3Cg=="
Set-ProcessAndMachineEnvVariables "APPLICATIONINSIGHTS_ENDPOINT" "https://dc.applicationinsights.azure.us/v2/track"
}
"usnat" {
Write-Host "Set-CloudSpecificApplicationInsightsConfig: Setting Application Insights configuration for USNat Cloud"
Set-AirgapCloudSpecificApplicationInsightsConfig
}
"ussec" {
Write-Host "Set-CloudSpecificApplicationInsightsConfig: Setting Application Insights configuration for USSec Cloud"
Set-AirgapCloudSpecificApplicationInsightsConfig
}
default {
Write-Host "Set-CloudSpecificApplicationInsightsConfig: defaulting to Public Cloud Application Insights configuration"
}
}
}

function Set-CommonAMAEnvironmentVariables {
Set-ProcessAndMachineEnvVariables "MONITORING_DATA_DIRECTORY" "C:\\opt\\windowsazuremonitoragent\\datadirectory"
Set-ProcessAndMachineEnvVariables "MONITORING_ROLE_INSTANCE" "cloudAgentRoleInstanceIdentity"
Expand Down Expand Up @@ -265,63 +352,7 @@ function Set-EnvironmentVariables {
[System.Environment]::SetEnvironmentVariable("PROXY", $proxy, "Machine")
}

# Need to do this before the SA fetch for AI key for airgapped clouds so that it is not overwritten with defaults.
$appInsightsAuth = [System.Environment]::GetEnvironmentVariable("APPLICATIONINSIGHTS_AUTH", "process")
if (![string]::IsNullOrEmpty($appInsightsAuth)) {
[System.Environment]::SetEnvironmentVariable("APPLICATIONINSIGHTS_AUTH", $appInsightsAuth, "machine")
Write-Host "Successfully set environment variable APPLICATIONINSIGHTS_AUTH - $($appInsightsAuth) for target 'machine'..."
}
else {
Write-Host "Failed to set environment variable APPLICATIONINSIGHTS_AUTH for target 'machine' since it is either null or empty"
}

$appInsightsEndpoint = [System.Environment]::GetEnvironmentVariable("APPLICATIONINSIGHTS_ENDPOINT", "process")
if (![string]::IsNullOrEmpty($appInsightsEndpoint)) {
[System.Environment]::SetEnvironmentVariable("APPLICATIONINSIGHTS_ENDPOINT", $appInsightsEndpoint, "machine")
Write-Host "Successfully set environment variable APPLICATIONINSIGHTS_ENDPOINT - $($appInsightsEndpoint) for target 'machine'..."
}

# Check if the instrumentation key needs to be fetched from a storage account (as in airgapped clouds)
$aiKeyURl = [System.Environment]::GetEnvironmentVariable('APPLICATIONINSIGHTS_AUTH_URL')
if ($aiKeyURl) {
$aiKeyFetched = ""
# retry up to 5 times
for ( $i = 1; $i -le 4; $i++) {
try {
$response = Invoke-WebRequest -uri $aiKeyURl -UseBasicParsing -TimeoutSec 5 -ErrorAction:Stop

if ($response.StatusCode -ne 200) {
Write-Host "Expecting reponse code 200, was: $($response.StatusCode), retrying"
Start-Sleep -Seconds ([MATH]::Pow(2, $i) / 4)
}
else {
$aiKeyFetched = $response.Content
break
}
}
catch {
Write-Host "Exception encountered fetching instrumentation key:"
Write-Host $_.Exception
}
}

# Check if the fetched IKey was properly encoded. if not then turn off telemetry
if ($aiKeyFetched -match '^[A-Za-z0-9=]+$') {
Write-Host "Using cloud-specific instrumentation key"
[System.Environment]::SetEnvironmentVariable("APPLICATIONINSIGHTS_AUTH", $aiKeyFetched, "Process")
[System.Environment]::SetEnvironmentVariable("APPLICATIONINSIGHTS_AUTH", $aiKeyFetched, "Machine")
}
else {
# Couldn't fetch the Ikey, turn telemetry off
Write-Host "Could not get cloud-specific instrumentation key (network error?). Disabling telemetry"
[System.Environment]::SetEnvironmentVariable("DISABLE_TELEMETRY", "True", "Process")
[System.Environment]::SetEnvironmentVariable("DISABLE_TELEMETRY", "True", "Machine")
}
}

$aiKeyDecoded = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($env:APPLICATIONINSIGHTS_AUTH))
[System.Environment]::SetEnvironmentVariable("TELEMETRY_APPLICATIONINSIGHTS_KEY", $aiKeyDecoded, "Process")
[System.Environment]::SetEnvironmentVariable("TELEMETRY_APPLICATIONINSIGHTS_KEY", $aiKeyDecoded, "Machine")
Set-CloudSpecificApplicationInsightsConfig $cloud_environment

# Setting environment variables required by the fluentd plugins
$aksResourceId = [System.Environment]::GetEnvironmentVariable("AKS_RESOURCE_ID", "process")
Expand Down

0 comments on commit 7f78af1

Please sign in to comment.