Skip to content

Commit

Permalink
update env variable for app insights and adjust tests accordingly (#344)
Browse files Browse the repository at this point in the history
* update env variable for app insights and adjust tests accordingly

* update condition

* update tests

* update tests
  • Loading branch information
arroyc authored Sep 6, 2019
1 parent 2c52ada commit 7cf32a5
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 60 deletions.
2 changes: 2 additions & 0 deletions build/constants.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
application-insights-instrumentation-key-env-var-name: ORYX_AI_INSTRUMENTATION_KEY
app-service-app-name-env-var-name: APPSETTING_WEBSITE_SITE_NAME
scm-commit-id-env-var-name: SCM_COMMIT_ID
user-app-insights-key-env: APPINSIGHTS_INSTRUMENTATIONKEY
user-app-insights-enable-env: ApplicationInsightsAgent_EXTENSION_VERSION
outputs:
- type: csharp
directory: src/Common
Expand Down
2 changes: 2 additions & 0 deletions src/Common/ExtVarNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ public static class ExtVarNames
public const string ApplicationInsightsInstrumentationKeyEnvVarName = "ORYX_AI_INSTRUMENTATION_KEY";
public const string AppServiceAppNameEnvVarName = "APPSETTING_WEBSITE_SITE_NAME";
public const string ScmCommitIdEnvVarName = "SCM_COMMIT_ID";
public const string UserAppInsightsKeyEnv = "APPINSIGHTS_INSTRUMENTATIONKEY";
public const string UserAppInsightsEnableEnv = "ApplicationInsightsAgent_EXTENSION_VERSION";
}
}
2 changes: 2 additions & 0 deletions src/startupscriptgenerator/src/common/consts/ext_var_names.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ const EnvironmentType string = "ORYX_ENV_TYPE"
const ApplicationInsightsInstrumentationKeyEnvVarName string = "ORYX_AI_INSTRUMENTATION_KEY"
const AppServiceAppNameEnvVarName string = "APPSETTING_WEBSITE_SITE_NAME"
const ScmCommitIdEnvVarName string = "SCM_COMMIT_ID"
const UserAppInsightsKeyEnv string = "APPINSIGHTS_INSTRUMENTATIONKEY"
const UserAppInsightsEnableEnv string = "ApplicationInsightsAgent_EXTENSION_VERSION"
12 changes: 6 additions & 6 deletions src/startupscriptgenerator/src/node/scriptgenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ const inspectParamVariableName = "ORYX_NODE_INSPECT_PARAM"

// Checks if the application insights needs to be enabled for the current runtime
func shouldApplicationInsightsBeConfigured() bool {
nodeAppInsightsKeyEnv := os.Getenv("APPINSIGHTS_INSTRUMENTATIONKEY")
nodeAppInsightsEnabledEnv := os.Getenv("APPLICATIONINSIGHTSAGENT_EXTENSION_ENABLED")
nodeAppInsightsKeyEnv := os.Getenv(consts.UserAppInsightsKeyEnv)
nodeAppInsightsEnabledEnv := os.Getenv(consts.UserAppInsightsEnableEnv)

if nodeAppInsightsKeyEnv != "" && strings.ToLower(nodeAppInsightsEnabledEnv) == "true" {
if nodeAppInsightsKeyEnv != "" && strings.ToLower(nodeAppInsightsEnabledEnv) != "disabled" {
fmt.Printf("Environment Variables for Application Insight's Codeless Configuration exists..\n")
return true
}
Expand All @@ -75,7 +75,7 @@ func createApplicationInsightsLoaderFile(appInsightsLoaderFilePath string) {
return true;
}
if (process.env.APPINSIGHTS_INSTRUMENTATIONKEY && process.env.APPLICATIONINSIGHTSAGENT_EXTENSION_ENABLED === "true") {
if (process.env.` + consts.UserAppInsightsKeyEnv + ` && process.env.` + consts.UserAppInsightsEnableEnv + ` !== "disabled") {
appInsights
.setup()
.setSendLiveMetrics(true)
Expand Down Expand Up @@ -130,7 +130,7 @@ func (gen *NodeStartupScriptGenerator) GenerateEntrypointScript() string {
scriptBuilder.WriteString("mkdir -p " + targetNodeModulesDir + "\n")
scriptBuilder.WriteString("echo Extracting modules...\n")
scriptBuilder.WriteString("$extractionCommand\n")

// Some versions of node, in particular Node 4.8 and 6.2 according to our tests, do not find the node_modules
// folder at the root. To handle these versions, we also add /node_modules to the NODE_PATH directory.
scriptBuilder.WriteString("export NODE_PATH=\"" + targetNodeModulesDir + "\":$NODE_PATH\n")
Expand Down Expand Up @@ -229,7 +229,7 @@ func (gen *NodeStartupScriptGenerator) GenerateEntrypointScript() string {
loaderFile := filepath.Join(gen.SourcePath, consts.NodeAppInsightsLoaderFileName)

if !common.FileExists(loaderFile) {
createApplicationInsightsLoaderFile(loaderFile)
createApplicationInsightsLoaderFile(loaderFile)
}

var nodeOptions = "'--require ./" + consts.NodeAppInsightsLoaderFileName + " ' $NODE_OPTIONS"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public async Task CanBuildAndRun_NodeApp_WithAppInsights_Configured(string nodeV
var volume = CreateAppVolume(appName);
var appDir = volume.ContainerDir;
var spcifyNodeVersionCommand = "--platform nodejs --platform-version=" + nodeVersion;
var aIKey = "APPINSIGHTS_INSTRUMENTATIONKEY";
var aIEnabled = "APPLICATIONINSIGHTSAGENT_EXTENSION_ENABLED";
var aIKey = ExtVarNames.UserAppInsightsKeyEnv;
var aIEnabled = ExtVarNames.UserAppInsightsEnableEnv;
var buildScript = new ShellScriptBuilder()
.AddCommand($"oryx build {appDir} -o {appDir} {spcifyNodeVersionCommand} --log-file {appDir}/1.log")
.AddDirectoryExistsCheck($"{appDir}/node_modules").ToString();
Expand Down Expand Up @@ -81,8 +81,8 @@ public async Task CanBuildAndRun_NodeApp_WithAppInsights_As_Package_Dependency(s
var volume = CreateAppVolume(appName);
var appDir = volume.ContainerDir;
var spcifyNodeVersionCommand = "--platform nodejs --platform-version=" + nodeVersion;
var aIKey = "APPINSIGHTS_INSTRUMENTATIONKEY";
var aIEnabled = "APPLICATIONINSIGHTSAGENT_EXTENSION_ENABLED";
var aIKey = ExtVarNames.UserAppInsightsKeyEnv;
var aIEnabled = ExtVarNames.UserAppInsightsEnableEnv;
var buildScript = new ShellScriptBuilder()
.AddCommand($"oryx build {appDir} -o {appDir} {spcifyNodeVersionCommand} --log-file {appDir}/1.log")
.AddDirectoryExistsCheck($"{appDir}/node_modules").ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public async Task GeneratesScript_CanRun_AppInsightsModule_NotFound(string nodeV
var volume = DockerVolume.CreateMirror(hostDir);
var appDir = volume.ContainerDir;
var imageName = string.Concat("oryxdevmcr.azurecr.io/public/oryx/node-", nodeVersion);
var aIKey = "APPINSIGHTS_INSTRUMENTATIONKEY";
var aIEnabled = "APPLICATIONINSIGHTSAGENT_EXTENSION_ENABLED";
var aIKey = ExtVarNames.UserAppInsightsKeyEnv;
var aIEnabled = ExtVarNames.UserAppInsightsEnableEnv;
int containerDebugPort = 8080;

var script = new ShellScriptBuilder()
Expand Down Expand Up @@ -86,11 +86,11 @@ public async Task GeneratesScript_CanRun_AppInsights_NotConfigured(string nodeVe
var volume = DockerVolume.CreateMirror(hostDir);
var appDir = volume.ContainerDir;
var imageName = string.Concat("oryxdevmcr.azurecr.io/public/oryx/node-", nodeVersion);
var aIEnabled = "APPLICATIONINSIGHTSAGENT_EXTENSION_ENABLED";
var aIEnabled = ExtVarNames.UserAppInsightsEnableEnv;
int containerDebugPort = 8080;

var script = new ShellScriptBuilder()
.AddCommand($"export {aIEnabled}=TRUE")
.AddCommand($"export {aIEnabled}=disabled")
.AddCommand($"cd {appDir}")
.AddCommand("npm install")
.AddCommand($"oryx -appPath {appDir}")
Expand Down
44 changes: 0 additions & 44 deletions tests/Oryx.RuntimeImage.Tests/Node/NodeRuntimeImageOtherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,50 +125,6 @@ public void GeneratedScript_CanRunStartupScriptsFromAppRoot()
RunAsserts(() => Assert.Equal(result.ExitCode, exitCodeSentinel), result.GetDebugInfo());
}

[Fact]
public void GeneratedScript_CanRunStartupScripts_WithAppInsightsConfigured()
{
// Arrange
const int exitCodeSentinel = 222;
var appPath = "/tmp/app";
var aiNodesdkLoaderContent = @"var appInsights = require('applicationinsights');
if (process.env.APPINSIGHTS_INSTRUMENTATIONKEY)
{
try
{
appInsights.setup().start();
} catch (e) {
console.error(e);
}
}";
var manifestFileContent = $"'{NodeConstants.InjectedAppInsights}=\"True\"'";

var script = new ShellScriptBuilder()
.CreateDirectory(appPath)
.CreateFile($"{appPath}/entry.sh", $"exit {exitCodeSentinel}")
.CreateFile($"{appPath}/{FilePaths.BuildManifestFileName}", manifestFileContent)
.CreateFile($"{appPath}/oryx-appinsightsloader.js", $"\"{aiNodesdkLoaderContent}\"")
.AddCommand("oryx -userStartupCommand entry.sh -appPath " + appPath)
.AddCommand(". ./run.sh") // Source the default output path
.AddStringExistsInFileCheck("export NODE_OPTIONS='--require ./oryx-appinsightsloader.js'", "./run.sh")
.ToString();

// Act
var result = _dockerCli.Run(new DockerRunArguments
{
ImageId = "oryxdevmcr.azurecr.io/public/oryx/node-10.14",
EnvironmentVariables = new List<EnvironmentVariable>
{
new EnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY", "asdas")
},
CommandToExecuteOnRun = "/bin/sh",
CommandArguments = new[] { "-c", script }
});

// Assert
RunAsserts(() => Assert.Equal(result.ExitCode, exitCodeSentinel), result.GetDebugInfo());
}

[Theory(Skip = "Investigating debugging using pm2")]
[MemberData(
nameof(TestValueGenerator.GetNodeVersions_SupportDebugging),
Expand Down
4 changes: 2 additions & 2 deletions tests/SampleApps/nodejs/linxnodeexpress-appinsights/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ var appInsights = require('applicationinsights');
var responseString = "";

console.log(process.env.APPINSIGHTS_INSTRUMENTATIONKEY)
console.log(process.env.APPLICATIONINSIGHTSAGENT_EXTENSION_ENABLED)
console.log(process.env.ApplicationInsightsAgent_EXTENSION_VERSION)

if (process.env.APPINSIGHTS_INSTRUMENTATIONKEY && process.env.APPLICATIONINSIGHTSAGENT_EXTENSION_ENABLED === "true") {
if (process.env.APPINSIGHTS_INSTRUMENTATIONKEY && process.env.ApplicationInsightsAgent_EXTENSION_VERSION !== "disabled") {
console.log("hello world here")
appInsights
.setup(process.env.APPINSIGHTS_INSTRUMENTATIONKEY)
Expand Down

0 comments on commit 7cf32a5

Please sign in to comment.