diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 2ae653133..cc57e0290 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -11,6 +11,9 @@ updates: - package-ecosystem: nuget directory: "/" groups: + opentelemetry: + patterns: + - OpenTelemetry* polly: patterns: - Polly* diff --git a/Directory.Packages.props b/Directory.Packages.props index 83a262a4e..a75f23e49 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -25,7 +25,9 @@ + + diff --git a/src/LondonTravel.Skill/AlexaFunction.cs b/src/LondonTravel.Skill/AlexaFunction.cs index 48951bdd1..b8ce124de 100644 --- a/src/LondonTravel.Skill/AlexaFunction.cs +++ b/src/LondonTravel.Skill/AlexaFunction.cs @@ -1,6 +1,7 @@ // Copyright (c) Martin Costello, 2017. All rights reserved. // Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information. +using System.Reflection; using Amazon.Lambda.Core; using MartinCostello.LondonTravel.Skill.Extensions; using MartinCostello.LondonTravel.Skill.Intents; @@ -9,8 +10,8 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; -using OpenTelemetry; using OpenTelemetry.Instrumentation.AWSLambda; +using OpenTelemetry.Resources; using OpenTelemetry.Trace; namespace MartinCostello.LondonTravel.Skill; @@ -20,6 +21,11 @@ namespace MartinCostello.LondonTravel.Skill; /// public class AlexaFunction : IAsyncDisposable, IDisposable { + internal static readonly string Version = typeof(AlexaFunction) + .Assembly + .GetCustomAttribute() + .InformationalVersion; + /// /// Whether the instance has been disposed. /// @@ -142,7 +148,17 @@ protected virtual void ConfigureServices(IServiceCollection services) services.AddTransient(); services.AddTransient(); - services.AddSingleton(Sdk.CreateTracerProviderBuilder().AddAWSLambdaConfigurations().Build()); + services.AddOpenTelemetry() + .ConfigureResource((builder) => builder.AddService("LondonTravel.Skill", serviceVersion: Version)) + .WithTracing((builder) => + { + builder.AddHttpClientInstrumentation(); + + if (IsRunningInAwsLambda()) + { + builder.AddAWSLambdaConfigurations(); + } + }); } protected virtual void Dispose(bool disposing) @@ -158,6 +174,10 @@ protected virtual void Dispose(bool disposing) } } + private static bool IsRunningInAwsLambda() + => Environment.GetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME") is { Length: > 0 } && + Environment.GetEnvironmentVariable("AWS_REGION") is { Length: > 0 }; + /// /// Creates the to use. /// diff --git a/src/LondonTravel.Skill/Extensions/IHttpClientBuilderExtensions.cs b/src/LondonTravel.Skill/Extensions/IHttpClientBuilderExtensions.cs index 55e18835b..b154432cf 100644 --- a/src/LondonTravel.Skill/Extensions/IHttpClientBuilderExtensions.cs +++ b/src/LondonTravel.Skill/Extensions/IHttpClientBuilderExtensions.cs @@ -3,7 +3,6 @@ using System.Net; using System.Net.Http.Headers; -using System.Reflection; using Microsoft.Extensions.DependencyInjection; namespace MartinCostello.LondonTravel.Skill.Extensions; @@ -54,10 +53,7 @@ public static IHttpClientBuilder ApplyDefaultConfiguration(this IHttpClientBuild /// private static ProductInfoHeaderValue CreateUserAgent() { - string productVersion = typeof(AlexaFunction) - .Assembly - .GetCustomAttribute() - .InformationalVersion; + string productVersion = AlexaFunction.Version; // Truncate the Git commit SHA to 7 characters, if present int indexOfPlus = productVersion.IndexOf('+', StringComparison.Ordinal); diff --git a/src/LondonTravel.Skill/LondonTravel.Skill.csproj b/src/LondonTravel.Skill/LondonTravel.Skill.csproj index 2bc2feec0..a5f41a32f 100644 --- a/src/LondonTravel.Skill/LondonTravel.Skill.csproj +++ b/src/LondonTravel.Skill/LondonTravel.Skill.csproj @@ -27,7 +27,9 @@ + + diff --git a/test/LondonTravel.Skill.Tests/LondonTravel.Skill.Tests.csproj b/test/LondonTravel.Skill.Tests/LondonTravel.Skill.Tests.csproj index 66d3efa1a..b2dedf108 100644 --- a/test/LondonTravel.Skill.Tests/LondonTravel.Skill.Tests.csproj +++ b/test/LondonTravel.Skill.Tests/LondonTravel.Skill.Tests.csproj @@ -29,6 +29,6 @@ cobertura,json [Alexa.NET*]*,[Amazon.Lambda*]*,[LondonTravel.Skill.EndToEndTests]*,[Refit*]*,[xunit.*]* GeneratedCodeAttribute - 91 + 90