Skip to content

Commit e0ee9ef

Browse files
committed
Refactoring of the Telemetry handling in a manner that we can easily skip it in Blazor
1 parent 2a78d8e commit e0ee9ef

14 files changed

+81
-132
lines changed

build/build-debug.ps1

+4-12
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,21 @@ $versionIncrement = $versionIncrement -as [int]
33
$versionIncrement = $versionIncrement + 1
44

55
$version = Get-Content .\version.debug -Raw
6-
$blazorVersion = Get-Content .\version.blazor -Raw
76

87
$version = $version.Replace("{incremental}", $versionIncrement)
9-
$blazorVersion = $blazorVersion.Replace("{incremental}", $versionIncrement)
108

11-
Write-Host "Building PnP.Core .Net Standard 2.0 version $version"
9+
Write-Host "Building PnP.Core versions $version"
1210
dotnet build ..\src\sdk\PnP.Core\PnP.Core.csproj --no-incremental /p:Version=$version
1311

14-
Write-Host "Building PnP.Core.Auth .Net Standard 2.0 version $version"
12+
Write-Host "Building PnP.Core.Auth versions $version"
1513
dotnet build ..\src\sdk\PnP.Core.Auth\PnP.Core.Auth.csproj --no-incremental /p:Version=$version
1614

17-
Write-Host "Building PnP.Core Blazor .Net Standard 2.0 version $version"
18-
dotnet build ..\src\sdk\PnP.Core\PnP.Core.csproj --no-incremental --configuration Blazor /p:Version=$blazorVersion
19-
20-
Write-Host "Packinging PnP.Core .Net Standard 2.0 version $version"
15+
Write-Host "Packinging PnP.Core versions $version"
2116
dotnet pack ..\src\sdk\PnP.Core\PnP.Core.csproj --no-build /p:PackageVersion=$version
2217

23-
Write-Host "Packinging PnP.Core.Auth .Net Standard 2.0 version $version"
18+
Write-Host "Packinging PnP.Core.Auth versions $version"
2419
dotnet pack ..\src\sdk\PnP.Core.Auth\PnP.Core.Auth.csproj --no-build /p:PackageVersion=$version
2520

26-
Write-Host "Packinging PnP.Core Blazor .Net Standard 2.0 version $version"
27-
dotnet pack ..\src\sdk\PnP.Core\PnP.Core.csproj --no-build --configuration Blazor /p:PackageVersion=$blazorVersion
28-
2921
#Write-Host "Writing $version to git"
3022
#Set-Content -Path .\version.debug.increment -Value $versionIncrement
3123

build/build.ps1

+4-14
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,30 @@ $versionIncrement = $versionIncrement -as [int]
88
$versionIncrement = $versionIncrement + 1
99

1010
$version = Get-Content ./build/version.debug -Raw
11-
$blazorVersion = Get-Content ./build/version.blazor -Raw
1211

1312
$version = $version.Replace("{incremental}", $versionIncrement)
14-
$blazorVersion = $blazorVersion.Replace("{incremental}", $versionIncrement)
1513

16-
Write-Host "Building PnP.Core .Net Standard 2.0 version $version"
14+
Write-Host "Building PnP.Core versions $version"
1715
dotnet build ./src/sdk/PnP.Core/PnP.Core.csproj --no-incremental /p:Version=$version
1816

19-
Write-Host "Building PnP.Core.Auth .Net Standard 2.0 version $version"
17+
Write-Host "Building PnP.Core.Auth versions $version"
2018
dotnet build ./src/sdk/PnP.Core.Auth/PnP.Core.Auth.csproj --no-incremental /p:Version=$version
2119

22-
Write-Host "Building PnP.Core Blazor .Net Standard 2.0 version $version"
23-
dotnet build ./src/sdk/PnP.Core/PnP.Core.csproj --no-incremental --configuration Blazor /p:Version=$blazorVersion
24-
25-
Write-Host "Packinging PnP.Core .Net Standard 2.0 version $version"
20+
Write-Host "Packinging PnP.Core versions $version"
2621
dotnet pack ./src/sdk/PnP.Core/PnP.Core.csproj --no-build /p:PackageVersion=$version
2722

28-
Write-Host "Packinging PnP.Core.Auth .Net Standard 2.0 version $version"
23+
Write-Host "Packinging PnP.Core.Auth versions $version"
2924
dotnet pack ./src/sdk/PnP.Core.Auth/PnP.Core.Auth.csproj --no-build /p:PackageVersion=$version
3025

31-
Write-Host "Packinging PnP.Core Blazor .Net Standard 2.0 version $version"
32-
dotnet pack ./src/sdk/PnP.Core/PnP.Core.csproj --no-build --configuration Blazor /p:PackageVersion=$blazorVersion
33-
3426
Write-Host "Publishing to nuget"
3527
$nupkg = $("./src/sdk/PnP.Core/bin/Debug/PnP.Core.$version.nupkg")
3628
$authNupkg = $("./src/sdk/PnP.Core.Auth/bin/Debug/PnP.Core.Auth.$version.nupkg")
37-
$blazorNupkg = $("./src/sdk/PnP.Core/bin/Blazor/PnP.Core.$blazorVersion.nupkg")
3829
$apiKey = $("$env:NUGET_API_KEY")
3930

4031
#Write-Host "API Key starts with:" $apiKey.Substring(0,10)
4132

4233
dotnet nuget push $nupkg --api-key $apiKey --source https://api.nuget.org/v3/index.json
4334
dotnet nuget push $authNupkg --api-key $apiKey --source https://api.nuget.org/v3/index.json
44-
dotnet nuget push $blazorNupkg --api-key $apiKey --source https://api.nuget.org/v3/index.json
4535

4636
Write-Host "Writing $version to git"
4737
Set-Content -Path ./build/version.debug.increment -Value $versionIncrement

build/version.blazor

-1
This file was deleted.

src/sdk/PnP.Core.Auth/PnP.Core.Auth.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
24-
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.15.0" />
24+
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.13.1" />
2525
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="1.1.1" />
2626
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="2.2.0" />
2727
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.2.0" />
@@ -34,7 +34,7 @@
3434
</ItemGroup>
3535

3636
<ItemGroup Condition=" '$(TargetFramework)' == 'net5.0' ">
37-
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.16.0" />
37+
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.16.0" />
3838
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="5.0.0" />
3939
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0" />
4040
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="5.0.0" />

src/sdk/PnP.Core.Test/PnP.Core.Test.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<IsPackable>false</IsPackable>
77

8-
<Configurations>Debug;Release;Blazor</Configurations>
8+
<Configurations>Debug;Release</Configurations>
99
</PropertyGroup>
1010

1111
<ItemGroup>

src/sdk/PnP.Core.Test/Services/Core/TestPnPContextFactory.cs

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using Microsoft.ApplicationInsights;
2-
using Microsoft.Extensions.Logging;
1+
using Microsoft.Extensions.Logging;
32
using Microsoft.Extensions.Options;
43
using PnP.Core.Services;
4+
using PnP.Core.Test.Utilities;
55
using System;
66
using System.Collections.Generic;
77
using System.Threading.Tasks;
@@ -48,9 +48,13 @@ public TestPnPContextFactory(
4848
SharePointRestClient sharePointRestClient,
4949
MicrosoftGraphClient microsoftGraphClient,
5050
IOptions<PnPContextFactoryOptions> contextOptions,
51-
IOptions<PnPGlobalSettingsOptions> globalOptions,
52-
TelemetryClient telemetryClient) : base(logger, sharePointRestClient, microsoftGraphClient, contextOptions, globalOptions, telemetryClient)
51+
IOptions<PnPGlobalSettingsOptions> globalOptions) : base(logger, sharePointRestClient, microsoftGraphClient, contextOptions, globalOptions)
5352
{
53+
if (TelemetryManager != null && !TestCommon.RunningInGitHubWorkflow())
54+
{
55+
// Send telemetry to the test Azure AppInsights instance
56+
TelemetryManager.TelemetryClient.InstrumentationKey = "6073339d-9e70-4004-9ff7-1345316ade97";
57+
}
5458
}
5559

5660
public override PnPContext Create(string name)
@@ -96,7 +100,7 @@ public override PnPContext Create(Guid groupId, IAuthenticationProvider authenti
96100

97101
public async override Task<PnPContext> CreateAsync(Guid groupId, IAuthenticationProvider authenticationProvider)
98102
{
99-
var context = new PnPContext(Log, authenticationProvider, SharePointRestClient, MicrosoftGraphClient, ContextOptions, GlobalOptions, TelemetryClient);
103+
var context = new PnPContext(Log, authenticationProvider, SharePointRestClient, MicrosoftGraphClient, ContextOptions, GlobalOptions, TelemetryManager);
100104

101105
ConfigurePnPContextForTesting(ref context);
102106

@@ -112,7 +116,7 @@ public override PnPContext Create(Guid groupId)
112116

113117
public async override Task<PnPContext> CreateAsync(Guid groupId)
114118
{
115-
var context = new PnPContext(Log, ContextOptions.DefaultAuthenticationProvider, SharePointRestClient, MicrosoftGraphClient, ContextOptions, GlobalOptions, TelemetryClient);
119+
var context = new PnPContext(Log, ContextOptions.DefaultAuthenticationProvider, SharePointRestClient, MicrosoftGraphClient, ContextOptions, GlobalOptions, TelemetryManager);
116120

117121
ConfigurePnPContextForTesting(ref context);
118122

src/sdk/PnP.Core.Test/Services/Core/TestPnPContextFactoryCollectionExtensions.cs

-20
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using Microsoft.Extensions.DependencyInjection;
2-
using Microsoft.Extensions.Options;
32
using PnP.Core.Services;
43
using PnP.Core.Services.Builder.Configuration;
5-
using PnP.Core.Test.Utilities;
64
using System;
75

86
namespace PnP.Core.Test.Services
@@ -39,7 +37,6 @@ public static IServiceCollection AddTestPnPContextFactory(this IServiceCollectio
3937

4038
// Add a PnP Context Factory service instance
4139
return collection
42-
.AddTelemetryServices()
4340
.AddHttpHandlers()
4441
.AddHttpClients()
4542
.AddPnPServices();
@@ -68,22 +65,5 @@ private static IServiceCollection AddPnPServices(this IServiceCollection collect
6865
return collection
6966
.AddTransient<IPnPContextFactory, TestPnPContextFactory>();
7067
}
71-
72-
private static IServiceCollection AddTelemetryServices(this IServiceCollection collection)
73-
{
74-
var globalOptionsService = collection.BuildServiceProvider().GetRequiredService<IOptions<PnPGlobalSettingsOptions>>();
75-
76-
// Setup Azure App Insights
77-
// See https://github.com/microsoft/ApplicationInsights-Home/tree/master/Samples/WorkerServiceSDK/WorkerServiceSampleWithApplicationInsights as example
78-
return collection.AddApplicationInsightsTelemetryWorkerService(options =>
79-
{
80-
if (!globalOptionsService.Value.DisableTelemetry && !TestCommon.RunningInGitHubWorkflow())
81-
{
82-
// Test AppInsights
83-
options.InstrumentationKey = "6073339d-9e70-4004-9ff7-1345316ade97";
84-
}
85-
});
86-
87-
}
8868
}
8969
}

src/sdk/PnP.Core/PnP.Core.csproj

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@
1414
<PackageProjectUrl>https://aka.ms/pnp/coresdk</PackageProjectUrl>
1515
<RepositoryUrl>https://github.com/pnp/pnpcore</RepositoryUrl>
1616
<RepositoryType>git</RepositoryType>
17-
<Configurations>Debug;Release;Blazor</Configurations>
17+
<Configurations>Debug;Release</Configurations>
1818
<PackageIcon>nugeticon.png</PackageIcon>
1919
<GenerateDocumentationFile>true</GenerateDocumentationFile>
2020
</PropertyGroup>
2121

22+
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netstandard2.0|AnyCPU'">
23+
<DefineConstants>TRACE</DefineConstants>
24+
</PropertyGroup>
25+
2226
<ItemGroup>
2327
<Compile Remove="Services\Authentication\ToRemove\**" />
2428
<EmbeddedResource Remove="Services\Authentication\ToRemove\**" />
@@ -31,7 +35,7 @@
3135
</ItemGroup>
3236

3337
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
34-
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.15.0" />
38+
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.13.1" />
3539
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="1.1.1" />
3640
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="2.2.0" />
3741
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.2.0" />
@@ -44,7 +48,7 @@
4448
</ItemGroup>
4549

4650
<ItemGroup Condition=" '$(TargetFramework)' == 'net5.0' ">
47-
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.16.0" />
51+
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.16.0" />
4852
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="5.0.0" />
4953
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0" />
5054
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="5.0.0" />

src/sdk/PnP.Core/Services/Core/BatchClient.cs

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Microsoft.ApplicationInsights;
2-
using Microsoft.Extensions.Logging;
1+
using Microsoft.Extensions.Logging;
32
using PnP.Core.Model;
43
using System;
54
using System.Collections.Generic;
@@ -121,18 +120,11 @@ internal class BatchResultMerge
121120
/// </summary>
122121
/// <param name="context">PnP Context</param>
123122
/// <param name="globalOptions">Global settings to use</param>
124-
/// <param name="telemetryClient">Azure AppInsihgts telemetry client to use</param>
125-
internal BatchClient(PnPContext context, PnPGlobalSettingsOptions globalOptions, TelemetryClient telemetryClient)
123+
/// <param name="telemetry">Telemetry manager</param>
124+
internal BatchClient(PnPContext context, PnPGlobalSettingsOptions globalOptions, TelemetryManager telemetry)
126125
{
127126
PnPContext = context;
128-
if (telemetryClient != null)
129-
{
130-
telemetryManager = new TelemetryManager(telemetryClient, globalOptions);
131-
}
132-
else
133-
{
134-
telemetryManager = null;
135-
}
127+
telemetryManager = telemetry;
136128

137129
HttpMicrosoftGraphMaxRetries = globalOptions.HttpMicrosoftGraphMaxRetries;
138130
HttpMicrosoftGraphDelayInSeconds = globalOptions.HttpMicrosoftGraphDelayInSeconds;

src/sdk/PnP.Core/Services/Core/Http/RetryHandlerBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private async Task<HttpResponseMessage> SendRetryAsync(HttpResponseMessage respo
6868
// Drain response content to free responses.
6969
if (response.Content != null)
7070
{
71-
#if NET5_0
71+
#if !NETSTANDARD2_0
7272
await response.Content.ReadAsByteArrayAsync(cancellationToken).ConfigureAwait(false);
7373
#else
7474
await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);

src/sdk/PnP.Core/Services/Core/PnPContext.cs

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Microsoft.ApplicationInsights;
2-
using Microsoft.Extensions.Logging;
1+
using Microsoft.Extensions.Logging;
32
using Microsoft.Extensions.Options;
43
using PnP.Core.Model.Security;
54
using PnP.Core.Model.SharePoint;
@@ -56,7 +55,7 @@ public class PnPContext : IDisposable
5655

5756
private readonly PnPGlobalSettingsOptions globalOptions;
5857
private readonly PnPContextFactoryOptions contextOptions;
59-
private readonly TelemetryClient telemetry;
58+
private readonly TelemetryManager telemetry;
6059
private Batch currentBatch;
6160

6261
#endregion
@@ -68,14 +67,14 @@ internal PnPContext(ILogger logger,
6867
MicrosoftGraphClient microsoftGraphClient,
6968
IOptions<PnPContextFactoryOptions> contextOptions,
7069
IOptions<PnPGlobalSettingsOptions> globalOptions,
71-
TelemetryClient telemetryClient) :
70+
TelemetryManager telemetryManager) :
7271
this(logger,
7372
authenticationProvider,
7473
sharePointRestClient,
7574
microsoftGraphClient,
7675
contextOptions?.Value,
7776
globalOptions?.Value,
78-
telemetryClient)
77+
telemetryManager)
7978
{
8079
}
8180

@@ -85,7 +84,7 @@ internal PnPContext(ILogger logger,
8584
MicrosoftGraphClient microsoftGraphClient,
8685
PnPContextFactoryOptions contextOptions,
8786
PnPGlobalSettingsOptions globalOptions,
88-
TelemetryClient telemetryClient)
87+
TelemetryManager telemetryManager)
8988
{
9089
Id = Guid.NewGuid();
9190
Logger = logger;
@@ -98,7 +97,7 @@ internal PnPContext(ILogger logger,
9897
GraphClient = microsoftGraphClient;
9998
this.globalOptions = globalOptions;
10099
this.contextOptions = contextOptions;
101-
telemetry = telemetryClient;
100+
telemetry = telemetryManager;
102101

103102
if (this.contextOptions != null)
104103
{
@@ -107,7 +106,7 @@ internal PnPContext(ILogger logger,
107106
GraphCanUseBeta = this.contextOptions.GraphCanUseBeta;
108107
}
109108

110-
BatchClient = new BatchClient(this, this.globalOptions, telemetryClient);
109+
BatchClient = new BatchClient(this, this.globalOptions, telemetryManager);
111110
}
112111
#endregion
113112

@@ -485,11 +484,8 @@ protected virtual void Dispose(bool disposing)
485484

486485
if (disposing)
487486
{
488-
// Flush telemetry
489-
if (telemetry != null)
490-
{
491-
telemetry.Flush();
492-
}
487+
// Future, lightweight, custom logic comes here
488+
// Note: flushing telemetry does not belong here since apps potentially can create/dispose a log of contexts
493489
}
494490

495491
disposed = true;

0 commit comments

Comments
 (0)