-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add .NET generic host Co-authored-by: Caleb Magiya (from Dev Box) <calebmagiya@microsoft.com> Co-authored-by: Vincent Biret <vibiret@microsoft.com>
- Loading branch information
1 parent
d529768
commit 7976e7e
Showing
35 changed files
with
1,595 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System.Diagnostics; | ||
|
||
namespace kiota.Extension; | ||
|
||
internal static class CollectionExtensions | ||
{ | ||
public static TagList AddAll(this TagList tagList, IEnumerable<KeyValuePair<string, object?>> tags) | ||
{ | ||
foreach (var tag in tags) tagList.Add(tag); | ||
return tagList; | ||
} | ||
|
||
public static T[] OrEmpty<T>(this T[]? source) | ||
{ | ||
return source ?? []; | ||
} | ||
public static List<T> OrEmpty<T>(this List<T>? source) | ||
{ | ||
return source ?? []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace kiota.Extension; | ||
|
||
internal static class EnumerableExtensions | ||
{ | ||
public static IEnumerable<T>? ConcatNullable<T>(this IEnumerable<T>? left, IEnumerable<T>? right) | ||
{ | ||
if (left is not null && right is not null) return left.Concat(right); | ||
// At this point, either left is null, right is null or both are null | ||
return left ?? right; | ||
} | ||
|
||
public static IEnumerable<T> OrEmpty<T>(this IEnumerable<T>? source) | ||
{ | ||
return source ?? []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using Azure.Monitor.OpenTelemetry.Exporter; | ||
using kiota.Telemetry; | ||
using kiota.Telemetry.Config; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using OpenTelemetry.Exporter; | ||
using OpenTelemetry.Metrics; | ||
using OpenTelemetry.Resources; | ||
using OpenTelemetry.Trace; | ||
|
||
namespace kiota.Extension; | ||
|
||
internal static class KiotaHostExtensions | ||
{ | ||
internal static IHostBuilder ConfigureKiotaTelemetryServices(this IHostBuilder hostBuilder) | ||
{ | ||
return hostBuilder.ConfigureServices(ConfigureServiceContainer); | ||
|
||
static void ConfigureServiceContainer(HostBuilderContext context, IServiceCollection services) | ||
{ | ||
TelemetryConfig cfg = new(); | ||
var section = context.Configuration.GetSection(TelemetryConfig.ConfigSectionKey); | ||
section.Bind(cfg); | ||
if (!cfg.Disabled) | ||
{ | ||
// Only register if telemetry is enabled. | ||
var openTelemetryBuilder = services.AddOpenTelemetry() | ||
.ConfigureResource(static r => | ||
{ | ||
r.AddService(serviceName: "kiota", | ||
serviceNamespace: "microsoft.openapi", | ||
serviceVersion: Kiota.Generated.KiotaVersion.Current()); | ||
if (OsName() is { } osName) | ||
{ | ||
r.AddAttributes([ | ||
new KeyValuePair<string, object>("os.name", osName), | ||
new KeyValuePair<string, object>("os.version", Environment.OSVersion.Version.ToString()) | ||
]); | ||
} | ||
}); | ||
openTelemetryBuilder.WithMetrics(static mp => | ||
{ | ||
mp.AddMeter($"{TelemetryLabels.ScopeName}*") | ||
.AddHttpClientInstrumentation() | ||
// Decide if runtime metrics are useful | ||
.AddRuntimeInstrumentation() | ||
.SetExemplarFilter(ExemplarFilterType.TraceBased); | ||
}) | ||
.WithTracing(static tp => | ||
{ | ||
tp.AddSource($"{TelemetryLabels.ScopeName}*") | ||
.AddHttpClientInstrumentation(); | ||
}); | ||
if (cfg.OpenTelemetry.Enabled) | ||
{ | ||
// Only register OpenTelemetry exporter if enabled. | ||
Action<OtlpExporterOptions> exporterOpts = op => | ||
{ | ||
if (!string.IsNullOrWhiteSpace(cfg.OpenTelemetry.EndpointAddress)) | ||
{ | ||
op.Endpoint = new Uri(cfg.OpenTelemetry.EndpointAddress); | ||
} | ||
}; | ||
openTelemetryBuilder | ||
.WithMetrics(mp => mp.AddOtlpExporter(exporterOpts)) | ||
.WithTracing(tp => tp.AddOtlpExporter(exporterOpts)); | ||
} | ||
if (cfg.AppInsights.Enabled && !string.IsNullOrWhiteSpace(cfg.AppInsights.ConnectionString)) | ||
{ | ||
// Only register app insights exporter if it's enabled and we have a connection string. | ||
Action<AzureMonitorExporterOptions> azureMonitorExporterOptions = options => | ||
{ | ||
options.ConnectionString = cfg.AppInsights.ConnectionString; | ||
}; | ||
openTelemetryBuilder | ||
.WithMetrics(mp => mp.AddAzureMonitorMetricExporter(azureMonitorExporterOptions)) | ||
.WithTracing(tp => tp.AddAzureMonitorTraceExporter(azureMonitorExporterOptions)); | ||
} | ||
services.AddSingleton<Instrumentation>(); | ||
} | ||
} | ||
static string? OsName() | ||
{ | ||
if (OperatingSystem.IsWindows()) return "windows"; | ||
if (OperatingSystem.IsLinux()) return "linux"; | ||
if (OperatingSystem.IsMacOS()) return "macos"; | ||
|
||
return OperatingSystem.IsFreeBSD() ? "freebsd" : null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace kiota.Extension; | ||
|
||
internal static class StringExtensions | ||
{ | ||
public static string OrEmpty(this string? source) | ||
{ | ||
// Investigate if using spans instead of strings helps perf. i.e. source?.AsSpan() ?? ReadOnlySpan<char>.Empty | ||
return source ?? string.Empty; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.