This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from microsoft/andrueastman/FixActivitySources
Adds registry for activity sources
- Loading branch information
Showing
13 changed files
with
109 additions
and
40 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
48 changes: 48 additions & 0 deletions
48
Microsoft.Kiota.Http.HttpClientLibrary.Tests/Middleware/ActivitySourceRegistryTests.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,48 @@ | ||
using System; | ||
using Microsoft.Kiota.Http.HttpClientLibrary.Middleware; | ||
using Xunit; | ||
|
||
namespace Microsoft.Kiota.Http.HttpClientLibrary.Tests.Middleware.Registries | ||
{ | ||
public class ActivitySourceRegistryTests | ||
{ | ||
[Fact] | ||
public void Defensive() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => ActivitySourceRegistry.DefaultInstance.GetOrCreateActivitySource("")); | ||
Assert.Throws<ArgumentNullException>(() => ActivitySourceRegistry.DefaultInstance.GetOrCreateActivitySource(null)); | ||
} | ||
|
||
[Fact] | ||
public void CreatesNewInstanceOnFirstCallAndReturnsSameInstance() | ||
{ | ||
// Act | ||
var activitySource = ActivitySourceRegistry.DefaultInstance.GetOrCreateActivitySource("sample source"); | ||
Assert.NotNull(activitySource); | ||
|
||
var activitySource2 = ActivitySourceRegistry.DefaultInstance.GetOrCreateActivitySource("sample source"); | ||
Assert.NotNull(activitySource); | ||
|
||
// They are the same instance | ||
Assert.Equal(activitySource, activitySource2); | ||
Assert.Equal("sample source", activitySource.Name); | ||
Assert.Equal("sample source", activitySource2.Name); | ||
} | ||
|
||
[Fact] | ||
public void CreatesDifferentInstances() | ||
{ | ||
// Act | ||
var activitySource = ActivitySourceRegistry.DefaultInstance.GetOrCreateActivitySource("sample source"); | ||
Assert.NotNull(activitySource); | ||
Assert.Equal("sample source", activitySource.Name); | ||
|
||
var activitySource2 = ActivitySourceRegistry.DefaultInstance.GetOrCreateActivitySource("sample source 2"); | ||
Assert.NotNull(activitySource); | ||
Assert.Equal("sample source 2", activitySource2.Name); | ||
|
||
// They are not the same instance | ||
Assert.NotEqual(activitySource, activitySource2); | ||
} | ||
} | ||
} |
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,37 @@ | ||
// ------------------------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. | ||
// ------------------------------------------------------------------------------ | ||
|
||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Diagnostics; | ||
|
||
namespace Microsoft.Kiota.Http.HttpClientLibrary.Middleware; | ||
|
||
/// <summary> | ||
/// Internal static registry for activity sources during tracing. | ||
/// </summary> | ||
internal class ActivitySourceRegistry | ||
{ | ||
private readonly ConcurrentDictionary<string, ActivitySource> _activitySources = new (StringComparer.OrdinalIgnoreCase); | ||
|
||
/// <summary> | ||
/// The default instance of the registry | ||
/// </summary> | ||
public static readonly ActivitySourceRegistry DefaultInstance = new(); | ||
|
||
/// <summary> | ||
/// Get an a <see cref="ActivitySource"/> instance with the given name or create one. | ||
/// </summary> | ||
/// <param name="sourceName">The name of the <see cref="ActivitySource"/></param> | ||
/// <returns></returns> | ||
/// <exception cref="ArgumentNullException">When the parameter is null or empty</exception> | ||
public ActivitySource GetOrCreateActivitySource(string sourceName) | ||
{ | ||
if(string.IsNullOrEmpty(sourceName)) | ||
throw new ArgumentNullException(nameof(sourceName)); | ||
|
||
return _activitySources.GetOrAdd(sourceName, static source => new ActivitySource(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
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
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