Skip to content

Commit

Permalink
Refactor OpenTelemetry
Browse files Browse the repository at this point in the history
More OpenTelemetry refactoring.
  • Loading branch information
martincostello committed Jan 23, 2024
1 parent f2ec516 commit 7570e77
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ updates:
- package-ecosystem: nuget
directory: "/"
groups:
opentelemetry:
patterns:
- OpenTelemetry*
polly:
patterns:
- Polly*
Expand Down
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="OpenTelemetry" Version="1.7.0" />
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.AWSLambda" Version="1.2.0-beta.1.168" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Http" Version="1.7.0" />
<PackageVersion Include="Polly.Core" Version="$(PollyVersion)" />
<PackageVersion Include="Polly.Extensions" Version="$(PollyVersion)" />
<PackageVersion Include="Polly.RateLimiting" Version="$(PollyVersion)" />
Expand Down
24 changes: 22 additions & 2 deletions src/LondonTravel.Skill/AlexaFunction.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -20,6 +21,11 @@ namespace MartinCostello.LondonTravel.Skill;
/// </summary>
public class AlexaFunction : IAsyncDisposable, IDisposable
{
internal static readonly string Version = typeof(AlexaFunction)
.Assembly
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion;

/// <summary>
/// Whether the instance has been disposed.
/// </summary>
Expand Down Expand Up @@ -142,7 +148,17 @@ protected virtual void ConfigureServices(IServiceCollection services)
services.AddTransient<DisruptionIntent>();
services.AddTransient<StatusIntent>();

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)
Expand All @@ -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 };

/// <summary>
/// Creates the <see cref="ServiceProvider"/> to use.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Net;
using System.Net.Http.Headers;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;

namespace MartinCostello.LondonTravel.Skill.Extensions;
Expand Down Expand Up @@ -54,10 +53,7 @@ public static IHttpClientBuilder ApplyDefaultConfiguration(this IHttpClientBuild
/// </returns>
private static ProductInfoHeaderValue CreateUserAgent()
{
string productVersion = typeof(AlexaFunction)
.Assembly
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion;
string productVersion = AlexaFunction.Version;

// Truncate the Git commit SHA to 7 characters, if present
int indexOfPlus = productVersion.IndexOf('+', StringComparison.Ordinal);
Expand Down
2 changes: 2 additions & 0 deletions src/LondonTravel.Skill/LondonTravel.Skill.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
<PackageReference Include="Microsoft.Extensions.Http.Resilience" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
<PackageReference Include="OpenTelemetry" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
<PackageReference Include="OpenTelemetry.Instrumentation.AWSLambda" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" />
<PackageReference Include="Polly.Core" />
<PackageReference Include="Polly.Extensions" />
<PackageReference Include="Polly.RateLimiting" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
<CoverletOutputFormat>cobertura,json</CoverletOutputFormat>
<Exclude>[Alexa.NET*]*,[Amazon.Lambda*]*,[LondonTravel.Skill.EndToEndTests]*,[Refit*]*,[xunit.*]*</Exclude>
<ExcludeByAttribute>GeneratedCodeAttribute</ExcludeByAttribute>
<Threshold>91</Threshold>
<Threshold>90</Threshold>
</PropertyGroup>
</Project>

0 comments on commit 7570e77

Please sign in to comment.