Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Azure Functions template should use configurable Serilog settings (🚧) #495

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build/templates/docker-integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ stages:
envVars: |
Arcus__Databricks__Url=$(Arcus.Databricks.Url)
Arcus__ApplicationInsights__MetricName=$(Arcus.ApplicationInsights.MetricName)
APPLICATIONINSIGHTS_INSTRUMENTATIONKEY=$(Arcus.ApplicationInsights.InstrumentationKey)
APPINSIGHTS_INSTRUMENTATIONKEY=$(Arcus.ApplicationInsights.InstrumentationKey)
- template: 'run-new-project-from-template.yml'
parameters:
projectName: 'Arcus.Demo.AzureFunctions.Http'
Expand All @@ -90,7 +90,7 @@ stages:
Where-Object { $_ -notmatch '#error' } |
Set-Content './Arcus.Demo.AzureFunctions.Http/Startup.cs'
envVars: |
APPLICATIONINSIGHTS_INSTRUMENTATIONKEY=$(Arcus.ApplicationInsights.InstrumentationKey)
APPINSIGHTS_INSTRUMENTATIONKEY=$(Arcus.ApplicationInsights.InstrumentationKey)
- task: UseDotNet@2
displayName: 'Import .NET Core SDK ($(DotNet.Sdk.PreviousVersion))'
inputs:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,67 +1,75 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<!--#if (AuthoringMode)-->
<Authors>Arcus</Authors>
<Company>Arcus</Company>
<RepositoryType>Git</RepositoryType>
<Description>Provide a template to easily build an Azure Functions that reports the completed Databricks job runs as metrics.</Description>
<IsPackable>true</IsPackable>
<PackageId>Arcus.Templates.AzureFunctions.Databricks.JobMetrics</PackageId>
<Title>Template for Azure Functions Databricks project</Title>
<PackageType>Template</PackageType>
<PackageTags>Azure;Azure Functions;Databricks;Metric</PackageTags>
<Copyright>Copyright (c) Arcus</Copyright>
<PackageLicenseUrl>https://github.com/arcus-azure/arcus.templates/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/arcus-azure/arcus.templates</PackageProjectUrl>
<RepositoryUrl>https://github.com/arcus-azure/arcus.templates</RepositoryUrl>
<PackageIconUrl>https://raw.githubusercontent.com/arcus-azure/arcus/master/media/arcus.png</PackageIconUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<IncludeContentInPack>true</IncludeContentInPack>
<IncludeBuildOutput>false</IncludeBuildOutput>
<ContentTargetFolders>content</ContentTargetFolders>
<EnableDefaultContentItems>false</EnableDefaultContentItems>
<DockerComposeProjectPath>..\Arcus.Templates.Orchestrator.dcproj</DockerComposeProjectPath>
<NoWarn>NU5048;NU5125;NU5119</NoWarn>
<!--#endif-->
</PropertyGroup>
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<!--#if (AuthoringMode)-->
<Authors>Arcus</Authors>
<Company>Arcus</Company>
<RepositoryType>Git</RepositoryType>
<Description>Provide a template to easily build an Azure Functions that reports the completed Databricks job runs as metrics.</Description>
<IsPackable>true</IsPackable>
<PackageId>Arcus.Templates.AzureFunctions.Databricks.JobMetrics</PackageId>
<Title>Template for Azure Functions Databricks project</Title>
<PackageType>Template</PackageType>
<PackageTags>Azure;Azure Functions;Databricks;Metric</PackageTags>
<Copyright>Copyright (c) Arcus</Copyright>
<PackageLicenseUrl>https://github.com/arcus-azure/arcus.templates/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/arcus-azure/arcus.templates</PackageProjectUrl>
<RepositoryUrl>https://github.com/arcus-azure/arcus.templates</RepositoryUrl>
<PackageIconUrl>https://raw.githubusercontent.com/arcus-azure/arcus/master/media/arcus.png</PackageIconUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<IncludeContentInPack>true</IncludeContentInPack>
<IncludeBuildOutput>false</IncludeBuildOutput>
<ContentTargetFolders>content</ContentTargetFolders>
<EnableDefaultContentItems>false</EnableDefaultContentItems>
<DockerComposeProjectPath>..\Arcus.Templates.Orchestrator.dcproj</DockerComposeProjectPath>
<NoWarn>NU5048;NU5125;NU5119</NoWarn>
<!--#endif-->
</PropertyGroup>

<!--#if (AuthoringMode)-->
<ItemGroup>
<Content Include="**\*" Exclude="**\bin\**\*;**\obj\**\*;**\.vs\**\*;**\launchSettings.json" />
</ItemGroup>
<!--#if (AuthoringMode)-->
<ItemGroup>
<Content Include="**\*" Exclude="**\bin\**\*;**\obj\**\*;**\.vs\**\*;**\launchSettings.json" />
</ItemGroup>

<PropertyGroup>
<DefineConstants>$(DefineConstants);</DefineConstants>
<DockerFastModeProjectMountDirectory>/home/site/wwwroot</DockerFastModeProjectMountDirectory>
</PropertyGroup>
<PropertyGroup>
<DefineConstants>$(DefineConstants);</DefineConstants>
<DockerFastModeProjectMountDirectory>/home/site/wwwroot</DockerFastModeProjectMountDirectory>
</PropertyGroup>

<ItemGroup>
<None Remove="Dockerfile" />
</ItemGroup>
<!--#endif-->
<ItemGroup>
<None Remove="Dockerfile" />
</ItemGroup>
<!--#endif-->

<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Arcus.BackgroundJobs.Databricks" Version="0.3.0" />
<PackageReference Include="Arcus.Observability.Telemetry.AzureFunctions" Version="2.3.0" />
<PackageReference Include="Arcus.Observability.Telemetry.Serilog.Sinks.Applicationinsights" Version="2.3.0" />
<PackageReference Include="Arcus.Security.AzureFunctions" Version="1.6.0" />
<PackageReference Include="Arcus.Security.Providers.AzureKeyVault" Version="1.6.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="4.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Arcus.BackgroundJobs.Databricks" Version="0.3.0" />
<PackageReference Include="Arcus.Observability.Telemetry.AzureFunctions" Version="2.3.0" />
<PackageReference Include="Arcus.Observability.Telemetry.Serilog.Sinks.Applicationinsights" Version="2.3.0" />
<PackageReference Include="Arcus.Security.AzureFunctions" Version="1.6.0" />
<PackageReference Include="Arcus.Security.Providers.AzureKeyVault" Version="1.6.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="4.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
</ItemGroup>

<Target Name="FunctionsPostBuildDepsCopy" AfterTargets="PostBuildEvent">
<Copy SourceFiles="$(OutDir)$(AssemblyName).deps.json" DestinationFiles="$(OutDir)bin\$(AssemblyName).deps.json" />
</Target>
<Target Name="FunctionsPublishDepsCopy" AfterTargets="Publish">
<Copy SourceFiles="$(OutDir)$(AssemblyName).deps.json" DestinationFiles="$(PublishDir)bin\$(AssemblyName).deps.json" />
</Target>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
Expand Down Expand Up @@ -39,21 +40,29 @@ public override void Configure(IFunctionsHostBuilder builder)
stores.AddAzureKeyVaultWithManagedIdentity("https://your-keyvault.vault.azure.net/", CacheConfiguration.Default);
});

var instrumentationKey = config.GetValue<string>("APPLICATIONINSIGHTS_INSTRUMENTATIONKEY");
var configuration = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.Enrich.WithComponentName("Azure Databricks Metrics Scraper")
.Enrich.WithVersion()
.WriteTo.Console()
.WriteTo.AzureApplicationInsights(instrumentationKey);

builder.Services.AddLogging(logging =>
builder.Services.AddLogging(loggingBuilder => ConfigureLogging(loggingBuilder, config));
}

private static void ConfigureLogging(ILoggingBuilder builder, IConfiguration config)
{
var functionDependencyContext = DependencyContext.Load(typeof(Startup).Assembly);

var logConfiguration = new LoggerConfiguration()
.ReadFrom.Configuration(config, sectionName: "AzureFunctionsJobHost:Serilog", dependencyContext: functionDependencyContext)
.Enrich.FromLogContext()
.Enrich.WithComponentName("Azure Databricks Metrics Scraper")
.Enrich.WithVersion()
.WriteTo.Console();

var telemetryKey = config.GetValue<string>("APPINSIGHTS_INSTRUMENTATIONKEY");

if (!string.IsNullOrWhiteSpace(telemetryKey))
{
logging.ClearProvidersExceptFunctionProviders()
.AddSerilog(configuration.CreateLogger(), dispose: true);
});
logConfiguration.WriteTo.AzureApplicationInsights(telemetryKey);
}

builder.ClearProvidersExceptFunctionProviders()
.AddSerilog(logConfiguration.CreateLogger(), dispose: true);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingExcludedTypes": "Request",
"samplingSettings": {
"isEnabled": true
}
"logging": {
"applicationInsights": {
"samplingExcludedTypes": "Request",
"samplingSettings": {
"isEnabled": true
}
}
},
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Information"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi" Version="1.0.0" Condition="'$(ExcludeOpenApi)' == 'false'" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
</ItemGroup>

<Target Name="FunctionsPostBuildDepsCopy" AfterTargets="PostBuildEvent">
<Copy SourceFiles="$(OutDir)$(AssemblyName).deps.json" DestinationFiles="$(OutDir)bin\$(AssemblyName).deps.json" />
</Target>
<Target Name="FunctionsPublishDepsCopy" AfterTargets="Publish">
<Copy SourceFiles="$(OutDir)$(AssemblyName).deps.json" DestinationFiles="$(PublishDir)bin\$(AssemblyName).deps.json" />
</Target>
</Project>
35 changes: 22 additions & 13 deletions src/Arcus.Templates.AzureFunctions.Http/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
Expand Down Expand Up @@ -52,21 +53,29 @@ public override void Configure(IFunctionsHostBuilder builder)
stores.AddAzureKeyVaultWithManagedIdentity("https://your-keyvault.vault.azure.net/", CacheConfiguration.Default);
});

var instrumentationKey = config.GetValue<string>("APPLICATIONINSIGHTS_INSTRUMENTATIONKEY");
var configuration = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.Enrich.WithComponentName("Azure HTTP Trigger")
.Enrich.WithVersion()
.WriteTo.Console()
.WriteTo.AzureApplicationInsights(instrumentationKey);
builder.Services.AddLogging(loggingBuilder => ConfigureLogging(loggingBuilder, config));
}

private static void ConfigureLogging(ILoggingBuilder builder, IConfiguration config)
{
var functionDependencyContext = DependencyContext.Load(typeof(Startup).Assembly);

var logConfiguration = new LoggerConfiguration()
.ReadFrom.Configuration(config, sectionName: "AzureFunctionsJobHost:Serilog", dependencyContext: functionDependencyContext)
.Enrich.FromLogContext()
.Enrich.WithComponentName("Azure HTTP Trigger")
.Enrich.WithVersion()
.WriteTo.Console();

builder.Services.AddLogging(logging =>
var telemetryKey = config.GetValue<string>("APPINSIGHTS_INSTRUMENTATIONKEY");

if (!string.IsNullOrWhiteSpace(telemetryKey))
{
logging.ClearProvidersExceptFunctionProviders()
.AddSerilog(configuration.CreateLogger(), dispose: true);
});
logConfiguration.WriteTo.AzureApplicationInsights(telemetryKey);
}

builder.ClearProvidersExceptFunctionProviders()
.AddSerilog(logConfiguration.CreateLogger(), dispose: true);
}
}
}
22 changes: 15 additions & 7 deletions src/Arcus.Templates.AzureFunctions.Http/host.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@
}
}
},
"logging": {
"applicationInsights": {
"samplingExcludedTypes": "Request",
"samplingSettings": {
"isEnabled": true
}
}
"logging": {
"applicationInsights": {
"samplingExcludedTypes": "Request",
"samplingSettings": {
"isEnabled": true
}
}
},
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Information"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Arcus.Templates.Tests.Integration.AzureFunctions
/// </summary>
public abstract class AzureFunctionsProject : TemplateProject
{
protected const string ApplicationInsightsInstrumentationKeyVariable = "APPLICATIONINSIGHTS_INSTRUMENTATIONKEY";
protected const string ApplicationInsightsInstrumentationKeyVariable = "APPINSIGHTS_INSTRUMENTATIONKEY";

private static readonly HttpClient HttpClient = new HttpClient();

Expand Down
Loading