-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[C#] feat: add adapter for telemetry headers (#1174)
## Linked issues closes: #745 #454 ## Details add a new type of `BotAdapter` that ensures our telemetry headers are added. - add `TeamsAdapter` and make it the required form of `BotAdapter` - update `js` implementation to match `dotnet`
- Loading branch information
Showing
16 changed files
with
199 additions
and
219 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
37 changes: 37 additions & 0 deletions
37
dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI.Tests/Application/TeamsAdapterTests.cs
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,37 @@ | ||
using System.Net.Http.Headers; | ||
using System.Reflection; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Microsoft.Teams.AI.Tests.Application | ||
{ | ||
public class TeamsAdapterTests | ||
{ | ||
[Fact] | ||
public void Test_TeamsAdapter_HasDefaultHeaders() | ||
{ | ||
string version = Assembly.GetAssembly(typeof(TeamsAdapter))?.GetName().Version?.ToString() ?? ""; | ||
ProductInfoHeaderValue productInfo = new("teamsai-dotnet", version); | ||
TeamsAdapter adapter = new(); | ||
Assert.NotNull(adapter.HttpClientFactory); | ||
|
||
HttpClient client = adapter.HttpClientFactory.CreateClient(); | ||
Assert.NotNull(client); | ||
Assert.True(client.DefaultRequestHeaders.UserAgent.Contains(productInfo)); | ||
} | ||
|
||
[Fact] | ||
public void Test_TeamsAdapter_NoDuplicateDefaultHeaders() | ||
{ | ||
string version = Assembly.GetAssembly(typeof(TeamsAdapter))?.GetName().Version?.ToString() ?? ""; | ||
ProductInfoHeaderValue productInfo = new("teamsai-dotnet", version); | ||
ConfigurationBuilder config = new(); | ||
TeamsAdapter adapter = new(config.Build(), new TeamsHttpClientFactory()); | ||
Assert.NotNull(adapter.HttpClientFactory); | ||
|
||
HttpClient client = adapter.HttpClientFactory.CreateClient(); | ||
Assert.NotNull(client); | ||
Assert.True(client.DefaultRequestHeaders.UserAgent.Contains(productInfo)); | ||
Assert.True(client.DefaultRequestHeaders.UserAgent.Count == 1); | ||
} | ||
} | ||
} |
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
70 changes: 70 additions & 0 deletions
70
dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/Application/TeamsAdapter.cs
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,70 @@ | ||
using Microsoft.Bot.Builder.Integration.AspNet.Core; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
using System.Net.Http.Headers; | ||
using System.Reflection; | ||
|
||
// Note: this class should never modify the way `CloudAdapter` is intended to work. | ||
|
||
namespace Microsoft.Teams.AI | ||
{ | ||
/// <summary> | ||
/// An adapter that implements the Bot Framework Protocol and can be hosted in different cloud environments both public and private. | ||
/// </summary> | ||
public class TeamsAdapter : CloudAdapter | ||
{ | ||
/// <summary> | ||
/// The Http Client Factory | ||
/// </summary> | ||
public IHttpClientFactory HttpClientFactory { get; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="TeamsAdapter"/> class. (Public cloud. No auth. For testing.) | ||
/// </summary> | ||
public TeamsAdapter() : base() | ||
{ | ||
HttpClientFactory = new TeamsHttpClientFactory(); | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="TeamsAdapter"/> class. | ||
/// </summary> | ||
/// <param name="configuration">The <see cref="IConfiguration"/> instance.</param> | ||
/// <param name="httpClientFactory">The <see cref="IHttpClientFactory"/> this adapter should use.</param> | ||
/// <param name="logger">The <see cref="ILogger"/> implementation this adapter should use.</param> | ||
public TeamsAdapter( | ||
IConfiguration configuration, | ||
IHttpClientFactory? httpClientFactory = null, | ||
ILogger? logger = null) : base( | ||
configuration, | ||
new TeamsHttpClientFactory(httpClientFactory), | ||
logger) | ||
{ | ||
HttpClientFactory = new TeamsHttpClientFactory(httpClientFactory); | ||
} | ||
} | ||
|
||
internal class TeamsHttpClientFactory : IHttpClientFactory | ||
{ | ||
private readonly IHttpClientFactory? _parent; | ||
|
||
public TeamsHttpClientFactory(IHttpClientFactory? parent = null) | ||
{ | ||
_parent = parent; | ||
} | ||
|
||
public HttpClient CreateClient(string name) | ||
{ | ||
HttpClient client = _parent != null ? _parent.CreateClient(name) : new(); | ||
string version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); | ||
ProductInfoHeaderValue productInfo = new("teamsai-dotnet", version); | ||
|
||
if (!client.DefaultRequestHeaders.UserAgent.Contains(productInfo)) | ||
{ | ||
client.DefaultRequestHeaders.UserAgent.Add(productInfo); | ||
} | ||
|
||
return client; | ||
} | ||
} | ||
} |
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
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.