diff --git a/sign.thirdparty.props b/sign.thirdparty.props index 31e5aaf95..58882052c 100644 --- a/sign.thirdparty.props +++ b/sign.thirdparty.props @@ -30,7 +30,7 @@ - + diff --git a/src/Catalog/NuGet.Services.Metadata.Catalog.csproj b/src/Catalog/NuGet.Services.Metadata.Catalog.csproj index 46846e121..8d3b008b5 100644 --- a/src/Catalog/NuGet.Services.Metadata.Catalog.csproj +++ b/src/Catalog/NuGet.Services.Metadata.Catalog.csproj @@ -292,7 +292,7 @@ 4.4.5-dev-3213233 - 2.58.0 + 2.66.0 1.0.6 @@ -309,7 +309,7 @@ all - 2.58.0 + 2.66.0 9.3.3 diff --git a/src/Catalog/Telemetry/TelemetryService.cs b/src/Catalog/Telemetry/TelemetryService.cs index 2b2f256eb..886ff0fda 100644 --- a/src/Catalog/Telemetry/TelemetryService.cs +++ b/src/Catalog/Telemetry/TelemetryService.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; -using Microsoft.ApplicationInsights; using NuGet.Services.Logging; using NuGet.Services.Metadata.Catalog.Helpers; using NuGet.Versioning; @@ -12,19 +11,14 @@ namespace NuGet.Services.Metadata.Catalog { public class TelemetryService : ITelemetryService { - private readonly TelemetryClientWrapper _telemetryClient; + private readonly ITelemetryClient _telemetryClient; public IDictionary GlobalDimensions { get; } - public TelemetryService(TelemetryClient telemetryClient) + public TelemetryService(ITelemetryClient telemetryClient, IDictionary globalDimensions) { - if (telemetryClient == null) - { - throw new ArgumentNullException(nameof(telemetryClient)); - } - - _telemetryClient = new TelemetryClientWrapper(telemetryClient); - GlobalDimensions = new Dictionary(); + _telemetryClient = telemetryClient ?? throw new ArgumentNullException(nameof(telemetryClient)); + GlobalDimensions = globalDimensions ?? throw new ArgumentNullException(nameof(globalDimensions)); } public void TrackCatalogIndexReadDuration(TimeSpan duration, Uri uri) diff --git a/src/Ng/Jobs/Catalog2DnxJob.cs b/src/Ng/Jobs/Catalog2DnxJob.cs index 911a04437..8e5de8cb6 100644 --- a/src/Ng/Jobs/Catalog2DnxJob.cs +++ b/src/Ng/Jobs/Catalog2DnxJob.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Logging; using NuGet.Protocol.Catalog; using NuGet.Services.Configuration; +using NuGet.Services.Logging; using NuGet.Services.Metadata.Catalog; using NuGet.Services.Metadata.Catalog.Dnx; using NuGet.Services.Metadata.Catalog.Persistence; @@ -21,8 +22,10 @@ public class Catalog2DnxJob : LoopingNgJob private ReadCursor _back; private Uri _destination; - public Catalog2DnxJob(ITelemetryService telemetryService, ILoggerFactory loggerFactory) - : base(telemetryService, loggerFactory) + public Catalog2DnxJob(ILoggerFactory loggerFactory, + ITelemetryClient telemetryClient, + IDictionary telemetryGlobalDimensions) + : base(loggerFactory, telemetryClient, telemetryGlobalDimensions) { } diff --git a/src/Ng/Jobs/Catalog2IconJob.cs b/src/Ng/Jobs/Catalog2IconJob.cs index 9cca6e9d5..4abd64856 100644 --- a/src/Ng/Jobs/Catalog2IconJob.cs +++ b/src/Ng/Jobs/Catalog2IconJob.cs @@ -10,6 +10,7 @@ using Microsoft.Extensions.Logging; using NuGet.Protocol.Catalog; using NuGet.Services.Configuration; +using NuGet.Services.Logging; using NuGet.Services.Metadata.Catalog; using NuGet.Services.Metadata.Catalog.Icons; using NuGet.Services.Metadata.Catalog.Persistence; @@ -23,8 +24,11 @@ public class Catalog2IconJob : LoopingNgJob private IconsCollector _collector; private DurableCursor _front; - public Catalog2IconJob(ITelemetryService telemetryService, ILoggerFactory loggerFactory) - : base(telemetryService, loggerFactory) + public Catalog2IconJob( + ILoggerFactory loggerFactory, + ITelemetryClient telemetryClient, + IDictionary telemetryGlobalDimensions) + : base(loggerFactory, telemetryClient, telemetryGlobalDimensions) { } diff --git a/src/Ng/Jobs/Catalog2LuceneJob.cs b/src/Ng/Jobs/Catalog2LuceneJob.cs index 72fef79d1..827e2d8ea 100644 --- a/src/Ng/Jobs/Catalog2LuceneJob.cs +++ b/src/Ng/Jobs/Catalog2LuceneJob.cs @@ -11,6 +11,7 @@ using Microsoft.Extensions.Logging; using NuGet.Indexing; using NuGet.Services.Configuration; +using NuGet.Services.Logging; using NuGet.Services.Metadata.Catalog; namespace Ng.Jobs @@ -30,8 +31,11 @@ public class Catalog2LuceneJob : LoopingNgJob private Func _handlerFunc; private string _destination; - public Catalog2LuceneJob(ITelemetryService telemetryService, ILoggerFactory loggerFactory) - : base(telemetryService, loggerFactory) + public Catalog2LuceneJob( + ILoggerFactory loggerFactory, + ITelemetryClient telemetryClient, + IDictionary telemetryGlobalDimensions) + : base(loggerFactory, telemetryClient, telemetryGlobalDimensions) { } diff --git a/src/Ng/Jobs/Catalog2MonitoringJob.cs b/src/Ng/Jobs/Catalog2MonitoringJob.cs index 2d079441f..a6edebfb9 100644 --- a/src/Ng/Jobs/Catalog2MonitoringJob.cs +++ b/src/Ng/Jobs/Catalog2MonitoringJob.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using Microsoft.Extensions.Logging; using NuGet.Services.Configuration; +using NuGet.Services.Logging; using NuGet.Services.Metadata.Catalog; using NuGet.Services.Metadata.Catalog.Monitoring; @@ -17,10 +18,13 @@ namespace Ng.Jobs /// public class Catalog2MonitoringJob : LoopingNgJob { - private PackageValidatorContextEnqueuer _enqueuer; + private PackageValidatorContextEnqueuer _enqueuer; - public Catalog2MonitoringJob(ITelemetryService telemetryService, ILoggerFactory loggerFactory) - : base(telemetryService, loggerFactory) + public Catalog2MonitoringJob( + ILoggerFactory loggerFactory, + ITelemetryClient telemetryClient, + IDictionary telemetryGlobalDimensions) + : base(loggerFactory, telemetryClient, telemetryGlobalDimensions) { } diff --git a/src/Ng/Jobs/Catalog2PackageFixupJob.cs b/src/Ng/Jobs/Catalog2PackageFixupJob.cs index 901d89ccb..a6d86a2b0 100644 --- a/src/Ng/Jobs/Catalog2PackageFixupJob.cs +++ b/src/Ng/Jobs/Catalog2PackageFixupJob.cs @@ -15,20 +15,20 @@ using Microsoft.WindowsAzure.Storage.Auth; using NuGet.Packaging.Core; using NuGet.Services.Configuration; +using NuGet.Services.Logging; using NuGet.Services.Metadata.Catalog; namespace Ng.Jobs { public class Catalog2PackageFixupJob : NgJob { - private const int MaximumPackageProcessingAttempts = 5; - private static readonly TimeSpan MaximumPackageProcessingTime = TimeSpan.FromMinutes(10); - private static readonly TimeSpan DonePollingInterval = TimeSpan.FromMinutes(1); - private IServiceProvider _serviceProvider; - public Catalog2PackageFixupJob(ITelemetryService telemetryService, ILoggerFactory loggerFactory) - : base(telemetryService, loggerFactory) + public Catalog2PackageFixupJob( + ILoggerFactory loggerFactory, + ITelemetryClient telemetryClient, + IDictionary telemetryGlobalDimensions) + : base(loggerFactory, telemetryClient, telemetryGlobalDimensions) { ThreadPool.SetMinThreads(MaxDegreeOfParallelism, 4); } @@ -54,7 +54,7 @@ protected override void Init(IDictionary arguments, Cancellation var services = new ServiceCollection(); - services.AddSingleton(TelemetryService); + services.AddSingleton(new TelemetryService(TelemetryClient, GlobalTelemetryDimensions)); services.AddSingleton(LoggerFactory); services.AddLogging(); @@ -65,7 +65,7 @@ protected override void Init(IDictionary arguments, Cancellation { AllowPipelining = true }); - + httpClient.DefaultRequestHeaders.Add("User-Agent", UserAgentUtility.GetUserAgent()); httpClient.Timeout = TimeSpan.FromMinutes(10); diff --git a/src/Ng/Jobs/Catalog2RegistrationJob.cs b/src/Ng/Jobs/Catalog2RegistrationJob.cs index a27cf3fcf..04fbd7e91 100644 --- a/src/Ng/Jobs/Catalog2RegistrationJob.cs +++ b/src/Ng/Jobs/Catalog2RegistrationJob.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using Microsoft.Extensions.Logging; using NuGet.Services.Configuration; +using NuGet.Services.Logging; using NuGet.Services.Metadata.Catalog; using NuGet.Services.Metadata.Catalog.Registration; @@ -19,8 +20,11 @@ public class Catalog2RegistrationJob : LoopingNgJob private ReadCursor _back; private Uri _destination; - public Catalog2RegistrationJob(ITelemetryService telemetryService, ILoggerFactory loggerFactory) - : base(telemetryService, loggerFactory) + public Catalog2RegistrationJob( + ILoggerFactory loggerFactory, + ITelemetryClient telemetryClient, + IDictionary telemetryGlobalDimensions) + : base(loggerFactory, telemetryClient, telemetryGlobalDimensions) { } diff --git a/src/Ng/Jobs/CheckLuceneJob.cs b/src/Ng/Jobs/CheckLuceneJob.cs index fad7e7b34..e3a3c2d37 100644 --- a/src/Ng/Jobs/CheckLuceneJob.cs +++ b/src/Ng/Jobs/CheckLuceneJob.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; using Lucene.Net.Index; using Microsoft.Extensions.Logging; -using NuGet.Services.Metadata.Catalog; +using NuGet.Services.Logging; namespace Ng.Jobs { @@ -14,7 +14,11 @@ public class CheckLuceneJob : NgJob { private Lucene.Net.Store.Directory _directory; - public CheckLuceneJob(ITelemetryService telemetryService, ILoggerFactory loggerFactory) : base(telemetryService, loggerFactory) + public CheckLuceneJob( + ILoggerFactory loggerFactory, + ITelemetryClient telemetryClient, + IDictionary telemetryGlobalDimensions) + : base(loggerFactory, telemetryClient, telemetryGlobalDimensions) { } diff --git a/src/Ng/Jobs/ClearLuceneJob.cs b/src/Ng/Jobs/ClearLuceneJob.cs index 5711169e6..28e898607 100644 --- a/src/Ng/Jobs/ClearLuceneJob.cs +++ b/src/Ng/Jobs/ClearLuceneJob.cs @@ -4,10 +4,10 @@ using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; -using Microsoft.Extensions.Logging; -using Lucene.Net.Index; using Lucene.Net.Analysis.Standard; -using NuGet.Services.Metadata.Catalog; +using Lucene.Net.Index; +using Microsoft.Extensions.Logging; +using NuGet.Services.Logging; namespace Ng.Jobs { @@ -15,7 +15,11 @@ public class ClearLuceneJob : NgJob { private Lucene.Net.Store.Directory _directory; - public ClearLuceneJob(ITelemetryService telemetryService, ILoggerFactory loggerFactory) : base(telemetryService, loggerFactory) + public ClearLuceneJob( + ILoggerFactory loggerFactory, + ITelemetryClient telemetryClient, + IDictionary telemetryGlobalDimensions) + : base(loggerFactory, telemetryClient, telemetryGlobalDimensions) { } @@ -34,7 +38,7 @@ protected override void Init(IDictionary arguments, Cancellation { _directory = CommandHelpers.GetLuceneDirectory(arguments); } - + protected override Task RunInternalAsync(CancellationToken cancellationToken) { if (IndexReader.IndexExists(_directory)) diff --git a/src/Ng/Jobs/CopyLuceneJob.cs b/src/Ng/Jobs/CopyLuceneJob.cs index 654fea358..2dbc52d38 100644 --- a/src/Ng/Jobs/CopyLuceneJob.cs +++ b/src/Ng/Jobs/CopyLuceneJob.cs @@ -6,9 +6,9 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; -using Microsoft.Extensions.Logging; using Lucene.Net.Store.Azure; -using NuGet.Services.Metadata.Catalog; +using Microsoft.Extensions.Logging; +using NuGet.Services.Logging; namespace Ng.Jobs { @@ -17,7 +17,11 @@ public class CopyLuceneJob : NgJob private Lucene.Net.Store.Directory _srcDirectory; private Lucene.Net.Store.Directory _destDirectory; - public CopyLuceneJob(ITelemetryService telemetryService, ILoggerFactory loggerFactory) : base(telemetryService, loggerFactory) + public CopyLuceneJob( + ILoggerFactory loggerFactory, + ITelemetryClient telemetryClient, + IDictionary telemetryGlobalDimensions) + : base(loggerFactory, telemetryClient, telemetryGlobalDimensions) { } @@ -47,7 +51,7 @@ protected override void Init(IDictionary arguments, Cancellation _srcDirectory = CommandHelpers.GetCopySrcLuceneDirectory(arguments); _destDirectory = CommandHelpers.GetCopyDestLuceneDirectory(arguments); } - + protected override Task RunInternalAsync(CancellationToken cancellationToken) { Lucene.Net.Store.Directory.Copy(_srcDirectory, _destDirectory, true); diff --git a/src/Ng/Jobs/Db2CatalogJob.cs b/src/Ng/Jobs/Db2CatalogJob.cs index a6d92fe89..9197426ac 100644 --- a/src/Ng/Jobs/Db2CatalogJob.cs +++ b/src/Ng/Jobs/Db2CatalogJob.cs @@ -12,6 +12,7 @@ using Ng.Helpers; using NuGet.Protocol; using NuGet.Services.Configuration; +using NuGet.Services.Logging; using NuGet.Services.Metadata.Catalog; using NuGet.Services.Metadata.Catalog.Helpers; using NuGet.Services.Metadata.Catalog.Persistence; @@ -35,8 +36,11 @@ public class Db2CatalogJob : LoopingNgJob protected Uri Destination; protected bool SkipCreatedPackagesProcessing; - public Db2CatalogJob(ITelemetryService telemetryService, ILoggerFactory loggerFactory) - : base(telemetryService, loggerFactory) + public Db2CatalogJob( + ILoggerFactory loggerFactory, + ITelemetryClient telemetryClient, + IDictionary telemetryGlobalDimensions) + : base(loggerFactory, telemetryClient, telemetryGlobalDimensions) { } diff --git a/src/Ng/Jobs/Db2LuceneJob.cs b/src/Ng/Jobs/Db2LuceneJob.cs index 8e4f896d3..c137c9452 100644 --- a/src/Ng/Jobs/Db2LuceneJob.cs +++ b/src/Ng/Jobs/Db2LuceneJob.cs @@ -8,7 +8,7 @@ using Microsoft.Extensions.Logging; using NuGet.Indexing; using NuGet.Services.Configuration; -using NuGet.Services.Metadata.Catalog; +using NuGet.Services.Logging; namespace Ng.Jobs { @@ -19,7 +19,11 @@ public class Db2LuceneJob : NgJob private string _source; private Uri _catalogIndexUrl; - public Db2LuceneJob(ITelemetryService telemetryService, ILoggerFactory loggerFactory) : base(telemetryService, loggerFactory) + public Db2LuceneJob( + ILoggerFactory loggerFactory, + ITelemetryClient telemetryClient, + IDictionary telemetryGlobalDimensions) + : base(loggerFactory, telemetryClient, telemetryGlobalDimensions) { } @@ -40,7 +44,7 @@ protected override void Init(IDictionary arguments, Cancellation _catalogIndexUrl = new Uri(_source); } - + protected override Task RunInternalAsync(CancellationToken cancellationToken) { Sql2Lucene.Export(_connectionString, _catalogIndexUrl, _path, LoggerFactory); diff --git a/src/Ng/Jobs/Db2MonitoringJob.cs b/src/Ng/Jobs/Db2MonitoringJob.cs index 38b4a1164..d026a9828 100644 --- a/src/Ng/Jobs/Db2MonitoringJob.cs +++ b/src/Ng/Jobs/Db2MonitoringJob.cs @@ -22,6 +22,7 @@ using CatalogStorageFactory = NuGet.Services.Metadata.Catalog.Persistence.StorageFactory; using CatalogStorage = NuGet.Services.Metadata.Catalog.Persistence.Storage; using Constants = NuGet.Services.Metadata.Catalog.Constants; +using NuGet.Services.Logging; namespace Ng.Jobs { @@ -53,8 +54,11 @@ public class Db2MonitoringJob : LoopingNgJob private CollectorHttpClient _client; - public Db2MonitoringJob(ITelemetryService telemetryService, ILoggerFactory loggerFactory) - : base(telemetryService, loggerFactory) + public Db2MonitoringJob( + ILoggerFactory loggerFactory, + ITelemetryClient telemetryClient, + IDictionary telemetryGlobalDimensions) + : base(loggerFactory, telemetryClient, telemetryGlobalDimensions) { } @@ -143,7 +147,7 @@ protected override async Task RunInternalAsync(CancellationToken cancellationTok } private async Task CheckPackages( - IReadOnlyCollection sources, + IReadOnlyCollection sources, CancellationToken cancellationToken) { Logger.LogInformation("Fetching packages to check status of."); diff --git a/src/Ng/Jobs/LightningJob.cs b/src/Ng/Jobs/LightningJob.cs index 4842d972e..3c901812d 100644 --- a/src/Ng/Jobs/LightningJob.cs +++ b/src/Ng/Jobs/LightningJob.cs @@ -11,7 +11,6 @@ using System.Threading.Tasks; using Autofac; using Autofac.Extensions.DependencyInjection; -using Microsoft.ApplicationInsights; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Logging; @@ -21,6 +20,7 @@ using NuGet.Protocol.Catalog; using NuGet.Services; using NuGet.Services.Configuration; +using NuGet.Services.Logging; using NuGet.Services.Metadata.Catalog; using NuGet.Services.Metadata.Catalog.Helpers; using NuGet.Services.Metadata.Catalog.Registration; @@ -33,8 +33,11 @@ public class LightningJob : NgJob private const string GraphDriver = "graph"; private const string JsonDriver = "json"; - public LightningJob(ITelemetryService telemetryService, ILoggerFactory loggerFactory) - : base(telemetryService, loggerFactory) + public LightningJob( + ILoggerFactory loggerFactory, + ITelemetryClient telemetryClient, + IDictionary telemetryGlobalDimensions) + : base(loggerFactory, telemetryClient, telemetryGlobalDimensions) { } @@ -522,7 +525,7 @@ private IServiceProvider GetServiceProvider() services.AddSingleton(LoggerFactory); services.AddLogging(); services.Add(ServiceDescriptor.Scoped(typeof(IOptionsSnapshot<>), typeof(NonCachingOptionsSnapshot<>))); - services.AddSingleton(new TelemetryClient()); + services.AddSingleton(TelemetryClient); services.Configure(config => { @@ -567,7 +570,7 @@ private IServiceProvider GetServiceProvider() config.EnsureSingleSnapshot = true; }); - services.AddCatalog2Registration(); + services.AddCatalog2Registration(GlobalTelemetryDimensions); var containerBuilder = new ContainerBuilder(); containerBuilder.AddCatalog2Registration(); diff --git a/src/Ng/Jobs/LoopingNgJob.cs b/src/Ng/Jobs/LoopingNgJob.cs index 8054251b0..477d5cf6a 100644 --- a/src/Ng/Jobs/LoopingNgJob.cs +++ b/src/Ng/Jobs/LoopingNgJob.cs @@ -7,14 +7,18 @@ using System.Threading.Tasks; using Microsoft.Extensions.Logging; using NuGet.Services.Configuration; +using NuGet.Services.Logging; using NuGet.Services.Metadata.Catalog; namespace Ng.Jobs { public abstract class LoopingNgJob : NgJob { - protected LoopingNgJob(ITelemetryService telemetryService, ILoggerFactory loggerFactory) - : base(telemetryService, loggerFactory) + protected LoopingNgJob( + ILoggerFactory loggerFactory, + ITelemetryClient telemetryClient, + IDictionary telemetryGlobalDimensions) + : base(loggerFactory, telemetryClient, telemetryGlobalDimensions) { } diff --git a/src/Ng/Jobs/Monitoring2MonitoringJob.cs b/src/Ng/Jobs/Monitoring2MonitoringJob.cs index c090a5cd9..a55c06efa 100644 --- a/src/Ng/Jobs/Monitoring2MonitoringJob.cs +++ b/src/Ng/Jobs/Monitoring2MonitoringJob.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; using Microsoft.Extensions.Logging; using NuGet.Services.Configuration; -using NuGet.Services.Metadata.Catalog; +using NuGet.Services.Logging; using NuGet.Services.Metadata.Catalog.Monitoring; using NuGet.Services.Storage; @@ -25,8 +25,11 @@ public class Monitoring2MonitoringJob : LoopingNgJob private const int DefaultMaxQueueSize = 100; private int _maxRequeueQueueSize; - public Monitoring2MonitoringJob(ITelemetryService telemetryService, ILoggerFactory loggerFactory) - : base(telemetryService, loggerFactory) + public Monitoring2MonitoringJob( + ILoggerFactory loggerFactory, + ITelemetryClient telemetryClient, + IDictionary telemetryGlobalDimensions) + : base(loggerFactory, telemetryClient, telemetryGlobalDimensions) { } @@ -50,7 +53,7 @@ protected override async Task RunInternalAsync(CancellationToken cancellationTok if (currentMessageCount > _maxRequeueQueueSize) { Logger.LogInformation( - "Can't requeue any invalid packages because the queue has too many messages ({CurrentMessageCount} > {MaxRequeueQueueSize})!", + "Can't requeue any invalid packages because the queue has too many messages ({CurrentMessageCount} > {MaxRequeueQueueSize})!", currentMessageCount, _maxRequeueQueueSize); return; } @@ -61,7 +64,7 @@ await Task.WhenAll(invalidPackages.Select(invalidPackage => { try { - Logger.LogInformation("Requeuing invalid package {PackageId} {PackageVersion}.", + Logger.LogInformation("Requeuing invalid package {PackageId} {PackageVersion}.", invalidPackage.Package.Id, invalidPackage.Package.Version); return _queue.AddAsync( @@ -70,7 +73,7 @@ await Task.WhenAll(invalidPackages.Select(invalidPackage => } catch (Exception e) { - Logger.LogError("Failed to requeue invalid package {PackageId} {PackageVersion}: {Exception}", + Logger.LogError("Failed to requeue invalid package {PackageId} {PackageVersion}: {Exception}", invalidPackage.Package.Id, invalidPackage.Package.Version, e); return Task.FromResult(0); diff --git a/src/Ng/Jobs/MonitoringProcessorJob.cs b/src/Ng/Jobs/MonitoringProcessorJob.cs index aa745119b..e51fc4929 100644 --- a/src/Ng/Jobs/MonitoringProcessorJob.cs +++ b/src/Ng/Jobs/MonitoringProcessorJob.cs @@ -12,6 +12,7 @@ using Ng.Helpers; using NuGet.Protocol.Core.Types; using NuGet.Services.Configuration; +using NuGet.Services.Logging; using NuGet.Services.Metadata.Catalog; using NuGet.Services.Metadata.Catalog.Helpers; using NuGet.Services.Metadata.Catalog.Monitoring; @@ -37,8 +38,11 @@ public class MonitoringProcessorJob : LoopingNgJob private TimeSpan _queueLoopDuration; private TimeSpan _queueDelay; - public MonitoringProcessorJob(ITelemetryService telemetryService, ILoggerFactory loggerFactory) - : base(telemetryService, loggerFactory) + public MonitoringProcessorJob( + ILoggerFactory loggerFactory, + ITelemetryClient telemetryClient, + IDictionary telemetryGlobalDimensions) + : base(loggerFactory, telemetryClient, telemetryGlobalDimensions) { } @@ -103,12 +107,12 @@ protected override void Init(IDictionary arguments, Cancellation _queueLoopDuration = TimeSpan.FromHours( arguments.GetOrDefault( - Arguments.QueueLoopDurationHours, + Arguments.QueueLoopDurationHours, DefaultQueueLoopDurationHours)); _queueDelay = TimeSpan.FromSeconds( arguments.GetOrDefault( - Arguments.QueueDelaySeconds, + Arguments.QueueDelaySeconds, DefaultQueueDelaySeconds)); SetUserAgentString(); @@ -147,7 +151,7 @@ await ParallelAsync.Repeat( /// The process will also stop dequeuing new messages because they wouldn't be processed anyway. /// private async Task ProcessPackagesAsync( - CancellationToken queueLoopCancellationToken, + CancellationToken queueLoopCancellationToken, CancellationToken queueMessageCancellationToken) { // We will never listen to cancellation of the queue loop token individually. @@ -155,7 +159,7 @@ private async Task ProcessPackagesAsync( // We combine the two tokens here so we don't have to call "IsCancellationRequested" multiple times. var combinedQueueLoopCancellationToken = CancellationTokenSource .CreateLinkedTokenSource( - queueLoopCancellationToken, + queueLoopCancellationToken, queueMessageCancellationToken) .Token; diff --git a/src/Ng/Jobs/NgJob.cs b/src/Ng/Jobs/NgJob.cs index 9d65c0a5d..47b448d28 100644 --- a/src/Ng/Jobs/NgJob.cs +++ b/src/Ng/Jobs/NgJob.cs @@ -7,22 +7,31 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; +using NuGet.Services.Logging; using NuGet.Services.Metadata.Catalog; namespace Ng.Jobs { public abstract class NgJob { - protected ITelemetryService TelemetryService; - protected ILoggerFactory LoggerFactory; - protected ILogger Logger; + protected readonly IDictionary GlobalTelemetryDimensions; + protected readonly ITelemetryClient TelemetryClient; + protected readonly ITelemetryService TelemetryService; + protected readonly ILoggerFactory LoggerFactory; + protected readonly ILogger Logger; protected int MaxDegreeOfParallelism { get; set; } - protected NgJob(ITelemetryService telemetryService, ILoggerFactory loggerFactory) + protected NgJob( + ILoggerFactory loggerFactory, + ITelemetryClient telemetryClient, + IDictionary telemetryGlobalDimensions) { - TelemetryService = telemetryService ?? throw new ArgumentNullException(nameof(telemetryService)); LoggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory)); + TelemetryClient = telemetryClient ?? throw new ArgumentNullException(nameof(telemetryClient)); + GlobalTelemetryDimensions = telemetryGlobalDimensions ?? throw new ArgumentNullException(nameof(telemetryGlobalDimensions)); + TelemetryService = new TelemetryService(telemetryClient, telemetryGlobalDimensions); + // We want to make a logger using the subclass of this job. // GetType returns the subclass of this instance which we can then use to create a logger. Logger = LoggerFactory.CreateLogger(GetType()); diff --git a/src/Ng/Ng.csproj b/src/Ng/Ng.csproj index 18afd1ad2..4da36abc2 100644 --- a/src/Ng/Ng.csproj +++ b/src/Ng/Ng.csproj @@ -66,7 +66,6 @@ - @@ -95,7 +94,6 @@ - @@ -180,16 +178,16 @@ all - 2.58.0 + 2.66.0 - 2.58.0 + 2.66.0 - 2.58.0 + 2.66.0 - 2.58.0 + 2.66.0 4.0.0 diff --git a/src/Ng/NgJobFactory.cs b/src/Ng/NgJobFactory.cs index ccaa007b9..83578750d 100644 --- a/src/Ng/NgJobFactory.cs +++ b/src/Ng/NgJobFactory.cs @@ -5,7 +5,7 @@ using System.Collections.Generic; using Microsoft.Extensions.Logging; using Ng.Jobs; -using NuGet.Services.Metadata.Catalog; +using NuGet.Services.Logging; namespace Ng { @@ -30,14 +30,18 @@ public static class NgJobFactory { "catalog2icon", typeof(Catalog2IconJob) }, }; - public static NgJob GetJob(string jobName, ITelemetryService telemetryService, ILoggerFactory loggerFactory) + public static NgJob GetJob( + string jobName, + ILoggerFactory loggerFactory, + ITelemetryClient telemetryClient, + IDictionary telemetryGlobalDimensions) { if (JobMap.ContainsKey(jobName)) { return (NgJob) - JobMap[jobName].GetConstructor(new[] { typeof(ITelemetryService), typeof(ILoggerFactory) }) - .Invoke(new object[] { telemetryService, loggerFactory }); + JobMap[jobName].GetConstructor(new[] { typeof(ILoggerFactory), typeof(ITelemetryClient), typeof(IDictionary) }) + .Invoke(new object[] { loggerFactory, telemetryClient, telemetryGlobalDimensions }); } throw new ArgumentException("Missing or invalid job name!"); diff --git a/src/Ng/Program.cs b/src/Ng/Program.cs index ed82eb34a..99c2528dc 100644 --- a/src/Ng/Program.cs +++ b/src/Ng/Program.cs @@ -9,12 +9,10 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.ApplicationInsights; -using Microsoft.ApplicationInsights.Extensibility; using Microsoft.Extensions.Logging; using Ng.Jobs; using NuGet.Services.Configuration; using NuGet.Services.Logging; -using NuGet.Services.Metadata.Catalog; using Serilog; using Serilog.Events; @@ -22,6 +20,8 @@ namespace Ng { public class Program { + private const string HeartbeatProperty_JobLoopExitCode = "JobLoopExitCode"; + private static Microsoft.Extensions.Logging.ILogger _logger; public static void Main(string[] args) @@ -38,6 +38,8 @@ public static async Task MainAsync(string[] args) } NgJob job = null; + ApplicationInsightsConfiguration applicationInsightsConfiguration = null; + int exitCode = 0; try { @@ -56,62 +58,107 @@ public static async Task MainAsync(string[] args) var jobName = args[0]; var instanceName = arguments.GetOrDefault(Arguments.InstanceName, jobName); - TelemetryConfiguration.Active.TelemetryInitializers.Add(new JobNameTelemetryInitializer(jobName, instanceName)); - - // Configure ApplicationInsights var instrumentationKey = arguments.GetOrDefault(Arguments.InstrumentationKey); var heartbeatIntervalSeconds = arguments.GetOrDefault(Arguments.HeartbeatIntervalSeconds); - if (heartbeatIntervalSeconds == 0) - { - ApplicationInsights.Initialize(instrumentationKey); - } - else - { - ApplicationInsights.Initialize( - instrumentationKey, - TimeSpan.FromSeconds(heartbeatIntervalSeconds)); - } + applicationInsightsConfiguration = ConfigureApplicationInsights( + instrumentationKey, + heartbeatIntervalSeconds, + jobName, + instanceName, + out var telemetryClient, + out var telemetryGlobalDimensions); - // Create an ILoggerFactory - var loggerConfiguration = LoggingSetup.CreateDefaultLoggerConfiguration(withConsoleLogger: true); - loggerConfiguration.WriteTo.File("Log.txt", retainedFileCountLimit: 3, fileSizeLimitBytes: 1000000, rollOnFileSizeLimit: true); + var loggerFactory = ConfigureLoggerFactory(applicationInsightsConfiguration); - var loggerFactory = LoggingSetup.CreateLoggerFactory(loggerConfiguration, LogEventLevel.Debug); + job = NgJobFactory.GetJob(jobName, loggerFactory, telemetryClient, telemetryGlobalDimensions); - // Create a logger that is scoped to this class (only) - _logger = loggerFactory.CreateLogger(); + // This tells Application Insights that, even though a heartbeat is reported, + // the state of the application is unhealthy when the exitcode is different from zero. + // The heartbeat metadata is enriched with the job loop exit code. + applicationInsightsConfiguration.DiagnosticsTelemetryModule?.AddOrSetHeartbeatProperty( + HeartbeatProperty_JobLoopExitCode, + exitCode.ToString(), + isHealthy: exitCode == 0); var cancellationTokenSource = new CancellationTokenSource(); - - // Create an ITelemetryService - var telemetryService = new TelemetryService(new TelemetryClient()); - - // Allow jobs to set global custom dimensions - TelemetryConfiguration.Active.TelemetryInitializers.Add(new JobPropertiesTelemetryInitializer(telemetryService)); - - job = NgJobFactory.GetJob(jobName, telemetryService, loggerFactory); await job.RunAsync(arguments, cancellationTokenSource.Token); + exitCode = 0; } catch (ArgumentException ae) { + exitCode = 1; _logger?.LogError("A required argument was not found or was malformed/invalid: {Exception}", ae); Console.WriteLine(job != null ? job.GetUsage() : NgJob.GetUsageBase()); } catch (KeyNotFoundException knfe) { + exitCode = 1; _logger?.LogError("An expected key was not found. One possible cause of this is required argument has not been provided: {Exception}", knfe); Console.WriteLine(job != null ? job.GetUsage() : NgJob.GetUsageBase()); } catch (Exception e) { + exitCode = 1; _logger?.LogCritical("A critical exception occured in ng.exe! {Exception}", e); } + applicationInsightsConfiguration.DiagnosticsTelemetryModule?.SetHeartbeatProperty( + HeartbeatProperty_JobLoopExitCode, + exitCode.ToString(), + isHealthy: exitCode == 0); + Trace.Close(); - TelemetryConfiguration.Active.TelemetryChannel.Flush(); + applicationInsightsConfiguration?.TelemetryConfiguration.TelemetryChannel.Flush(); + } + + private static ILoggerFactory ConfigureLoggerFactory(ApplicationInsightsConfiguration applicationInsightsConfiguration) + { + var loggerConfiguration = LoggingSetup.CreateDefaultLoggerConfiguration(withConsoleLogger: true); + loggerConfiguration.WriteTo.File("Log.txt", retainedFileCountLimit: 3, fileSizeLimitBytes: 1000000, rollOnFileSizeLimit: true); + + var loggerFactory = LoggingSetup.CreateLoggerFactory( + loggerConfiguration, + LogEventLevel.Debug, + applicationInsightsConfiguration.TelemetryConfiguration); + + // Create a logger that is scoped to this class (only) + _logger = loggerFactory.CreateLogger(); + return loggerFactory; + } + + private static ApplicationInsightsConfiguration ConfigureApplicationInsights( + string instrumentationKey, + int heartbeatIntervalSeconds, + string jobName, + string instanceName, + out ITelemetryClient telemetryClient, + out IDictionary telemetryGlobalDimensions) + { + ApplicationInsightsConfiguration applicationInsightsConfiguration; + if (heartbeatIntervalSeconds == 0) + { + applicationInsightsConfiguration = ApplicationInsights.Initialize(instrumentationKey); + } + else + { + applicationInsightsConfiguration = ApplicationInsights.Initialize( + instrumentationKey, + TimeSpan.FromSeconds(heartbeatIntervalSeconds)); + } + + telemetryClient = new TelemetryClientWrapper( + new TelemetryClient(applicationInsightsConfiguration.TelemetryConfiguration)); + + telemetryGlobalDimensions = new Dictionary(); + + // Enrich telemetry with job properties and global custom dimensions + applicationInsightsConfiguration.TelemetryConfiguration.TelemetryInitializers.Add( + new JobPropertiesTelemetryInitializer(jobName, instanceName, telemetryGlobalDimensions)); + + return applicationInsightsConfiguration; } } } diff --git a/src/Ng/Telemetry/JobNameTelemetryInitializer.cs b/src/Ng/Telemetry/JobNameTelemetryInitializer.cs deleted file mode 100644 index b166006a8..000000000 --- a/src/Ng/Telemetry/JobNameTelemetryInitializer.cs +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.ApplicationInsights.Channel; -using Microsoft.ApplicationInsights.DataContracts; -using Microsoft.ApplicationInsights.Extensibility; - -namespace Ng -{ - public class JobNameTelemetryInitializer : ITelemetryInitializer - { - private const string JobNameKey = "JobName"; - private const string InstanceNameKey = "InstanceName"; - - private readonly string _jobName; - private readonly string _instanceName; - - public JobNameTelemetryInitializer(string jobName, string instanceName) - { - _jobName = jobName ?? throw new ArgumentNullException(nameof(jobName)); - _instanceName = instanceName ?? throw new ArgumentNullException(nameof(instanceName)); - } - - public void Initialize(ITelemetry telemetry) - { - // Note that telemetry initializers can be called multiple times for the same telemetry item, so - // these operations need to not fail if called again. In this particular case, Dictionary.Add - // cannot be used since it will fail if the key already exists. - // https://github.com/microsoft/ApplicationInsights-dotnet-server/issues/977 - - // We need to cast to ISupportProperties to avoid using the deprecated telemetry.Context.Properties API. - // https://github.com/Microsoft/ApplicationInsights-Home/issues/300 - - var supportProperties = (ISupportProperties)telemetry; - - supportProperties.Properties[JobNameKey] = _jobName; - supportProperties.Properties[InstanceNameKey] = _instanceName; - } - } -} \ No newline at end of file diff --git a/src/Ng/Telemetry/JobPropertiesTelemetryInitializer.cs b/src/Ng/Telemetry/JobPropertiesTelemetryInitializer.cs deleted file mode 100644 index b2dc2ee41..000000000 --- a/src/Ng/Telemetry/JobPropertiesTelemetryInitializer.cs +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.ApplicationInsights.Channel; -using Microsoft.ApplicationInsights.DataContracts; -using Microsoft.ApplicationInsights.Extensibility; -using NuGet.Services.Metadata.Catalog; - -namespace Ng -{ - public class JobPropertiesTelemetryInitializer : ITelemetryInitializer - { - private readonly ITelemetryService _telemetryService; - - public JobPropertiesTelemetryInitializer(ITelemetryService telemetryService) - { - _telemetryService = telemetryService ?? throw new ArgumentNullException(nameof(telemetryService)); - } - - public void Initialize(ITelemetry telemetry) - { - if (_telemetryService.GlobalDimensions != null) - { - var supportProperties = (ISupportProperties)telemetry; - - foreach (var dimension in _telemetryService.GlobalDimensions) - { - supportProperties.Properties[dimension.Key] = dimension.Value; - } - } - } - } -} \ No newline at end of file diff --git a/src/NuGet.ApplicationInsights.Owin/MachineNameTelemetryInitializer.cs b/src/NuGet.ApplicationInsights.Owin/MachineNameTelemetryInitializer.cs deleted file mode 100644 index 9f50fe16c..000000000 --- a/src/NuGet.ApplicationInsights.Owin/MachineNameTelemetryInitializer.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using Microsoft.ApplicationInsights.Channel; -using Microsoft.ApplicationInsights.Extensibility; - -namespace NuGet.ApplicationInsights.Owin -{ - public class MachineNameTelemetryInitializer : ITelemetryInitializer - { - const string MachineNameProperty = "MachineName"; - - private string _machineName; - - public MachineNameTelemetryInitializer() - { - try - { - _machineName = Environment.MachineName; - } - catch - { - } - } - - public void Initialize(ITelemetry telemetry) - { - telemetry.Context.Properties[MachineNameProperty] = _machineName; - } - } -} diff --git a/src/NuGet.ApplicationInsights.Owin/NuGet.ApplicationInsights.Owin.csproj b/src/NuGet.ApplicationInsights.Owin/NuGet.ApplicationInsights.Owin.csproj index 3b628dd45..a10b489bf 100644 --- a/src/NuGet.ApplicationInsights.Owin/NuGet.ApplicationInsights.Owin.csproj +++ b/src/NuGet.ApplicationInsights.Owin/NuGet.ApplicationInsights.Owin.csproj @@ -59,7 +59,6 @@ - @@ -74,7 +73,7 @@ all - 2.2.0 + 2.12.0 3.0.1 diff --git a/src/NuGet.Indexing/NuGet.Indexing.csproj b/src/NuGet.Indexing/NuGet.Indexing.csproj index f8d781cdd..7803eaa63 100644 --- a/src/NuGet.Indexing/NuGet.Indexing.csproj +++ b/src/NuGet.Indexing/NuGet.Indexing.csproj @@ -182,7 +182,7 @@ all - 2.58.0 + 2.66.0 4.4.5-dev-3213233 diff --git a/src/NuGet.Jobs.Catalog2Registration/DependencyInjectionExtensions.cs b/src/NuGet.Jobs.Catalog2Registration/DependencyInjectionExtensions.cs index 1c19c4211..9c7795426 100644 --- a/src/NuGet.Jobs.Catalog2Registration/DependencyInjectionExtensions.cs +++ b/src/NuGet.Jobs.Catalog2Registration/DependencyInjectionExtensions.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Collections.Generic; using System.Net.Http; using Autofac; using Microsoft.Extensions.DependencyInjection; @@ -76,9 +77,11 @@ private static void RegisterCursorStorage(ContainerBuilder containerBuilder) .Keyed(CursorBindingKey); } - public static IServiceCollection AddCatalog2Registration(this IServiceCollection services) + public static IServiceCollection AddCatalog2Registration( + this IServiceCollection services, + IDictionary telemetryGlobalDimensions) { - services.AddV3(); + services.AddV3(telemetryGlobalDimensions); services.AddTransient(); services.AddTransient(); diff --git a/src/NuGet.Jobs.Catalog2Registration/Job.cs b/src/NuGet.Jobs.Catalog2Registration/Job.cs index a29e57340..f485c231b 100644 --- a/src/NuGet.Jobs.Catalog2Registration/Job.cs +++ b/src/NuGet.Jobs.Catalog2Registration/Job.cs @@ -30,7 +30,7 @@ protected override void ConfigureAutofacServices(ContainerBuilder containerBuild protected override void ConfigureJobServices(IServiceCollection services, IConfigurationRoot configurationRoot) { - services.AddCatalog2Registration(); + services.AddCatalog2Registration(GlobalTelemetryDimensions); services.Configure(configurationRoot.GetSection(ConfigurationSectionName)); services.Configure(configurationRoot.GetSection(ConfigurationSectionName)); diff --git a/src/NuGet.Jobs.RegistrationComparer/Job.cs b/src/NuGet.Jobs.RegistrationComparer/Job.cs index 9f7f978da..bffce9688 100644 --- a/src/NuGet.Jobs.RegistrationComparer/Job.cs +++ b/src/NuGet.Jobs.RegistrationComparer/Job.cs @@ -80,7 +80,7 @@ protected override void ConfigureAutofacServices(ContainerBuilder containerBuild protected override void ConfigureJobServices(IServiceCollection services, IConfigurationRoot configurationRoot) { - services.AddV3(); + services.AddV3(GlobalTelemetryDimensions); switch (_mode) { diff --git a/src/NuGet.Services.AzureSearch/AzureSearchJob.cs b/src/NuGet.Services.AzureSearch/AzureSearchJob.cs index 18fba0f08..bd90ee19d 100644 --- a/src/NuGet.Services.AzureSearch/AzureSearchJob.cs +++ b/src/NuGet.Services.AzureSearch/AzureSearchJob.cs @@ -39,7 +39,7 @@ protected override void ConfigureAutofacServices(ContainerBuilder containerBuild protected override void ConfigureJobServices(IServiceCollection services, IConfigurationRoot configurationRoot) { - services.AddAzureSearch(); + services.AddAzureSearch(GlobalTelemetryDimensions); } } } diff --git a/src/NuGet.Services.AzureSearch/DependencyInjectionExtensions.cs b/src/NuGet.Services.AzureSearch/DependencyInjectionExtensions.cs index 318b1cc49..6669d9732 100644 --- a/src/NuGet.Services.AzureSearch/DependencyInjectionExtensions.cs +++ b/src/NuGet.Services.AzureSearch/DependencyInjectionExtensions.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Collections.Generic; using System.Net.Http; using Autofac; using Microsoft.Azure.Search; @@ -32,7 +33,7 @@ public static class DependencyInjectionExtensions public static ContainerBuilder AddAzureSearch(this ContainerBuilder containerBuilder) { /// Here, we register services that depend on an interface that there are multiple implementations. - + /// There are multiple implementations of . RegisterIndexServices(containerBuilder, "SearchIndex", "HijackIndex"); @@ -212,9 +213,11 @@ private static void RegisterAuxiliaryDataStorageServices(ContainerBuilder contai c.Resolve>())); } - public static IServiceCollection AddAzureSearch(this IServiceCollection services) + public static IServiceCollection AddAzureSearch( + this IServiceCollection services, + IDictionary telemetryGlobalDimensions) { - services.AddV3(); + services.AddV3(telemetryGlobalDimensions); services .AddTransient(p => diff --git a/src/NuGet.Services.BasicSearch/ApplicationInsights.config b/src/NuGet.Services.BasicSearch/ApplicationInsights.config index dc6b4285c..cd22c26b1 100644 --- a/src/NuGet.Services.BasicSearch/ApplicationInsights.config +++ b/src/NuGet.Services.BasicSearch/ApplicationInsights.config @@ -28,81 +28,6 @@ - - - - - - - - - - - - - - - - - Microsoft.VisualStudio.Web.PageInspector.Runtime.Tracing.RequestDataHttpHandler - System.Web.StaticFileHandler - System.Web.Handlers.AssemblyResourceLoader - System.Web.Optimization.BundleHandler - System.Web.Script.Services.ScriptHandlerFactory - System.Web.Handlers.TraceHandler - System.Web.Services.Discovery.DiscoveryRequestHandler - System.Web.HttpDebugHandler - - - - - diff --git a/src/NuGet.Services.BasicSearch/NuGet.Services.BasicSearch.csproj b/src/NuGet.Services.BasicSearch/NuGet.Services.BasicSearch.csproj index 49760e029..8d14e8a95 100644 --- a/src/NuGet.Services.BasicSearch/NuGet.Services.BasicSearch.csproj +++ b/src/NuGet.Services.BasicSearch/NuGet.Services.BasicSearch.csproj @@ -133,14 +133,8 @@ 1.0.17-build01715 - - 0.12.0-build17386 - - - 0.12.0-build17386 - - 2.10.0 + 2.12.0 0.12.0-build02810 @@ -161,13 +155,13 @@ 3.0.1 - 2.58.0 + 2.66.0 - 2.58.0 + 2.66.0 - 2.0.9 + 5.0.52 4.3.3 diff --git a/src/NuGet.Services.BasicSearch/SearchTelemetryClient.cs b/src/NuGet.Services.BasicSearch/SearchTelemetryClient.cs index 9def2cbc2..97b013fbb 100644 --- a/src/NuGet.Services.BasicSearch/SearchTelemetryClient.cs +++ b/src/NuGet.Services.BasicSearch/SearchTelemetryClient.cs @@ -23,9 +23,9 @@ public static class MetricName public const string LuceneCommitTimestamp = "LuceneCommitTimestamp"; } - public SearchTelemetryClient() + public SearchTelemetryClient(TelemetryConfiguration telemetryConfiguration) { - TelemetryClient = new TelemetryClient(TelemetryConfiguration.Active); + TelemetryClient = new TelemetryClient(telemetryConfiguration); } public void TrackMetric(string name, double value, IDictionary properties = null) diff --git a/src/NuGet.Services.BasicSearch/Startup.cs b/src/NuGet.Services.BasicSearch/Startup.cs index a8ade702f..fd5553ae9 100644 --- a/src/NuGet.Services.BasicSearch/Startup.cs +++ b/src/NuGet.Services.BasicSearch/Startup.cs @@ -5,12 +5,19 @@ using System.Collections.Generic; using System.Diagnostics; using System.Globalization; +using System.Linq; using System.Net; using System.Threading; using System.Threading.Tasks; using System.Web.Cors; using Lucene.Net.Store; +using Microsoft.ApplicationInsights.DependencyCollector; using Microsoft.ApplicationInsights.Extensibility; +using Microsoft.ApplicationInsights.Extensibility.Implementation; +using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector; +using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse; +using Microsoft.ApplicationInsights.Web; +using Microsoft.ApplicationInsights.WindowsServer; using Microsoft.Azure.KeyVault; using Microsoft.Extensions.Logging; using Microsoft.IdentityModel.Clients.ActiveDirectory; @@ -51,36 +58,7 @@ public void Configuration( var config = GetConfiguration().Result; // Configure - if (!string.IsNullOrEmpty(config.ApplicationInsightsInstrumentationKey)) - { - Logging.ApplicationInsights.Initialize( - config.ApplicationInsightsInstrumentationKey, - TimeSpan.FromSeconds(config.ApplicationInsightsHeartbeatIntervalSeconds)); - } - - // Add telemetry initializers - TelemetryConfiguration.Active.TelemetryInitializers.Add(new MachineNameTelemetryInitializer()); - TelemetryConfiguration.Active.TelemetryInitializers.Add(new DeploymentIdTelemetryInitializer()); - - // Create telemetry sink - _searchTelemetryClient = new SearchTelemetryClient(); - - // Add telemetry processors - var processorChain = TelemetryConfiguration.Active.TelemetryProcessorChainBuilder; - - processorChain.Use(next => - { - var processor = new RequestTelemetryProcessor(next); - - processor.SuccessfulResponseCodes.Add(400); - processor.SuccessfulResponseCodes.Add(404); - - return processor; - }); - - processorChain.Use(next => new ExceptionTelemetryProcessor(next, _searchTelemetryClient.TelemetryClient)); - - processorChain.Build(); + var applicationInsightsConfiguration = InitializeApplicationInsights(config); // Create an ILoggerFactory var loggerConfiguration = LoggingSetup.CreateDefaultLoggerConfiguration(withConsoleLogger: false) @@ -91,7 +69,9 @@ public void Configuration( .Enrich.With() .Enrich.With(); - var loggerFactory = LoggingSetup.CreateLoggerFactory(loggerConfiguration); + var loggerFactory = LoggingSetup.CreateLoggerFactory( + loggerConfiguration, + telemetryConfiguration: applicationInsightsConfiguration.TelemetryConfiguration); // Create a logger that is scoped to this class (only) _logger = loggerFactory.CreateLogger(); @@ -135,10 +115,10 @@ public void Configuration( app.Use(typeof(CorrelationIdMiddleware)); // Add Application Insights - app.Use(typeof(RequestTrackingMiddleware)); + app.Use(typeof(RequestTrackingMiddleware), new[] { applicationInsightsConfiguration.TelemetryConfiguration }); // Set up exception logging - app.Use(typeof(ExceptionTrackingMiddleware)); + app.Use(typeof(ExceptionTrackingMiddleware), new[] { applicationInsightsConfiguration.TelemetryConfiguration }); // Enable HSTS app.Use(async (context, next) => @@ -198,6 +178,118 @@ public void Configuration( app.Run(InvokeAsync); } + private ApplicationInsightsConfiguration InitializeApplicationInsights(BasicSearchConfiguration config) + { + var applicationInsightsConfiguration = Logging.ApplicationInsights.Initialize( + config.ApplicationInsightsInstrumentationKey, + TimeSpan.FromSeconds(config.ApplicationInsightsHeartbeatIntervalSeconds)); + + RegisterApplicationInsightsTelemetryModules(applicationInsightsConfiguration.TelemetryConfiguration); + + // Add telemetry initializers + applicationInsightsConfiguration.TelemetryConfiguration.TelemetryInitializers.Add(new MachineNameTelemetryInitializer()); + applicationInsightsConfiguration.TelemetryConfiguration.TelemetryInitializers.Add(new DeploymentIdTelemetryInitializer()); + + // Create telemetry sink + _searchTelemetryClient = new SearchTelemetryClient(applicationInsightsConfiguration.TelemetryConfiguration); + + // Add telemetry processors + var processorChain = applicationInsightsConfiguration.TelemetryConfiguration.TelemetryProcessorChainBuilder; + + processorChain.Use(next => + { + var processor = new RequestTelemetryProcessor(next); + + processor.SuccessfulResponseCodes.Add(400); + processor.SuccessfulResponseCodes.Add(404); + + return processor; + }); + + processorChain.Use(next => new ExceptionTelemetryProcessor(next, _searchTelemetryClient.TelemetryClient)); + + processorChain.Build(); + + return applicationInsightsConfiguration; + } + + private static void RegisterApplicationInsightsTelemetryModules(TelemetryConfiguration configuration) + { + RegisterApplicationInsightsTelemetryModule( + new AppServicesHeartbeatTelemetryModule(), + configuration); + + RegisterApplicationInsightsTelemetryModule( + new AzureInstanceMetadataTelemetryModule(), + configuration); + + RegisterApplicationInsightsTelemetryModule( + new DeveloperModeWithDebuggerAttachedTelemetryModule(), + configuration); + + RegisterApplicationInsightsTelemetryModule( + new UnhandledExceptionTelemetryModule(), + configuration); + + RegisterApplicationInsightsTelemetryModule( + new UnobservedExceptionTelemetryModule(), + configuration); + + var requestTrackingModule = new RequestTrackingTelemetryModule(); + requestTrackingModule.Handlers.Add("Microsoft.VisualStudio.Web.PageInspector.Runtime.Tracing.RequestDataHttpHandler"); + requestTrackingModule.Handlers.Add("System.Web.StaticFileHandler"); + requestTrackingModule.Handlers.Add("System.Web.Handlers.AssemblyResourceLoader"); + requestTrackingModule.Handlers.Add("System.Web.Optimization.BundleHandler"); + requestTrackingModule.Handlers.Add("System.Web.Script.Services.ScriptHandlerFactory"); + requestTrackingModule.Handlers.Add("System.Web.Handlers.TraceHandler"); + requestTrackingModule.Handlers.Add("System.Web.Services.Discovery.DiscoveryRequestHandler"); + requestTrackingModule.Handlers.Add("System.Web.HttpDebugHandler"); + RegisterApplicationInsightsTelemetryModule( + requestTrackingModule, + configuration); + + RegisterApplicationInsightsTelemetryModule( + new ExceptionTrackingTelemetryModule(), + configuration); + + RegisterApplicationInsightsTelemetryModule( + new AspNetDiagnosticTelemetryModule(), + configuration); + + var dependencyTrackingModule = new DependencyTrackingTelemetryModule(); + dependencyTrackingModule.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("core.windows.net"); + dependencyTrackingModule.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("core.chinacloudapi.cn"); + dependencyTrackingModule.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("core.cloudapi.de"); + dependencyTrackingModule.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("core.usgovcloudapi.net"); + dependencyTrackingModule.IncludeDiagnosticSourceActivities.Add("Microsoft.Azure.EventHubs"); + dependencyTrackingModule.IncludeDiagnosticSourceActivities.Add("Microsoft.Azure.ServiceBus"); + RegisterApplicationInsightsTelemetryModule( + dependencyTrackingModule, + configuration); + + RegisterApplicationInsightsTelemetryModule( + new PerformanceCollectorModule(), + configuration); + + RegisterApplicationInsightsTelemetryModule( + new QuickPulseTelemetryModule(), + configuration); + } + + private static void RegisterApplicationInsightsTelemetryModule(ITelemetryModule telemetryModule, TelemetryConfiguration configuration) + { + var existingModule = TelemetryModules.Instance.Modules.SingleOrDefault(m => m.GetType().Equals(telemetryModule.GetType())); + if (existingModule != null) + { + TelemetryModules.Instance.Modules.Remove(existingModule); + } + + telemetryModule.Initialize(configuration); + + TelemetryModules.Instance.Modules.Add(telemetryModule); + } + + public void Configuration(IAppBuilder app) { ServicePointManager.DefaultConnectionLimit = Int32.MaxValue; diff --git a/src/NuGet.Services.BasicSearch/Web.config b/src/NuGet.Services.BasicSearch/Web.config index 288e1fe3e..1db40ada0 100644 --- a/src/NuGet.Services.BasicSearch/Web.config +++ b/src/NuGet.Services.BasicSearch/Web.config @@ -34,6 +34,7 @@ + @@ -75,20 +76,16 @@ - - + + - - + + - - - - - - + + @@ -124,7 +121,7 @@ - + diff --git a/src/NuGet.Services.Metadata.Catalog.Monitoring/NuGet.Services.Metadata.Catalog.Monitoring.csproj b/src/NuGet.Services.Metadata.Catalog.Monitoring/NuGet.Services.Metadata.Catalog.Monitoring.csproj index 51ed5ca43..c172d17c8 100644 --- a/src/NuGet.Services.Metadata.Catalog.Monitoring/NuGet.Services.Metadata.Catalog.Monitoring.csproj +++ b/src/NuGet.Services.Metadata.Catalog.Monitoring/NuGet.Services.Metadata.Catalog.Monitoring.csproj @@ -172,7 +172,7 @@ all - 2.58.0 + 2.66.0 diff --git a/src/NuGet.Services.SearchService/App_Start/AzureWebAppTelemetryInitializer.cs b/src/NuGet.Services.SearchService/App_Start/AzureWebAppTelemetryInitializer.cs deleted file mode 100644 index ea502efc8..000000000 --- a/src/NuGet.Services.SearchService/App_Start/AzureWebAppTelemetryInitializer.cs +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.ApplicationInsights.Channel; -using Microsoft.ApplicationInsights.Extensibility; - -namespace NuGet.Services.SearchService -{ - /// - /// Overrides the initialized telemetry context. This should be added last in - /// the Application Insights telemetry list. - /// See: https://github.com/microsoft/ApplicationInsights-dotnet-server/blob/e5a0edbe570e0938d3cb7a36a57b25d0db4d3c01/Src/WindowsServer/WindowsServer.Shared/AzureWebAppRoleEnvironmentTelemetryInitializer.cs#L12 - /// - public class AzureWebAppTelemetryInitializer : ITelemetryInitializer - { - private const string StagingSlotSuffix = "-staging"; - - public void Initialize(ITelemetry telemetry) - { - // Application Insight's Azure Web App Role Environment telemetry initializer uses - // the hostname for the "cloud_roleName" property, which unintentionally creates separate - // role names for our production/staging slots. - var roleName = telemetry.Context.Cloud.RoleName; - if (!string.IsNullOrEmpty(roleName) && roleName.EndsWith(StagingSlotSuffix, StringComparison.OrdinalIgnoreCase)) - { - telemetry.Context.Cloud.RoleName = roleName.Substring(0, roleName.Length - StagingSlotSuffix.Length); - } - } - } -} \ No newline at end of file diff --git a/src/NuGet.Services.SearchService/App_Start/WebApiConfig.cs b/src/NuGet.Services.SearchService/App_Start/WebApiConfig.cs index 6c441e23f..94ed96efe 100644 --- a/src/NuGet.Services.SearchService/App_Start/WebApiConfig.cs +++ b/src/NuGet.Services.SearchService/App_Start/WebApiConfig.cs @@ -2,7 +2,9 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Collections.Generic; using System.IO; +using System.Linq; using System.Reflection; using System.Threading; using System.Threading.Tasks; @@ -13,7 +15,13 @@ using Autofac.Extensions.DependencyInjection; using Autofac.Integration.WebApi; using Microsoft.ApplicationInsights; +using Microsoft.ApplicationInsights.DependencyCollector; using Microsoft.ApplicationInsights.Extensibility; +using Microsoft.ApplicationInsights.Extensibility.Implementation; +using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector; +using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse; +using Microsoft.ApplicationInsights.Web; +using Microsoft.ApplicationInsights.WindowsServer; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -129,24 +137,16 @@ private static AutofacWebApiDependencyResolver GetDependencyResolver(HttpConfigu { var configuration = GetConfiguration(); - var instrumentationKey = configuration.Root.GetValue("ApplicationInsights_InstrumentationKey"); - var heartbeatIntervalSeconds = configuration.Root.GetValue("ApplicationInsights_HeartbeatIntervalSeconds", 60); - if (!string.IsNullOrWhiteSpace(instrumentationKey)) - { - ApplicationInsights.Initialize( - instrumentationKey, - TimeSpan.FromSeconds(heartbeatIntervalSeconds)); - } - - TelemetryConfiguration.Active.TelemetryInitializers.Add(new AzureWebAppTelemetryInitializer()); + var globalTelemetryDimensions = new Dictionary(); + var applicationInsightsConfiguration = InitializeApplicationInsights(configuration); var services = new ServiceCollection(); services.AddSingleton(configuration.SecretReaderFactory); services.Add(ServiceDescriptor.Scoped(typeof(IOptionsSnapshot<>), typeof(NonCachingOptionsSnapshot<>))); services.Configure(configuration.Root.GetSection(ConfigurationSectionName)); services.Configure(configuration.Root.GetSection(ConfigurationSectionName)); - services.AddAzureSearch(); - services.AddSingleton(new TelemetryClient()); + services.AddAzureSearch(globalTelemetryDimensions); + services.AddSingleton(new TelemetryClient(applicationInsightsConfiguration.TelemetryConfiguration)); services.AddTransient(); var builder = new ContainerBuilder(); @@ -157,7 +157,10 @@ private static AutofacWebApiDependencyResolver GetDependencyResolver(HttpConfigu builder.AddAzureSearch(); var loggerConfiguration = LoggingSetup.CreateDefaultLoggerConfiguration(withConsoleLogger: false); - var loggerFactory = LoggingSetup.CreateLoggerFactory(loggerConfiguration); + var loggerFactory = LoggingSetup.CreateLoggerFactory( + loggerConfiguration, + telemetryConfiguration: applicationInsightsConfiguration.TelemetryConfiguration); + builder.RegisterInstance(loggerFactory).As(); builder.RegisterGeneric(typeof(Logger<>)).As(typeof(ILogger<>)); @@ -165,6 +168,98 @@ private static AutofacWebApiDependencyResolver GetDependencyResolver(HttpConfigu return new AutofacWebApiDependencyResolver(container); } + private static ApplicationInsightsConfiguration InitializeApplicationInsights(RefreshableConfiguration configuration) + { + var instrumentationKey = configuration.Root.GetValue("ApplicationInsights_InstrumentationKey"); + var heartbeatIntervalSeconds = configuration.Root.GetValue("ApplicationInsights_HeartbeatIntervalSeconds", 60); + + var applicationInsightsConfiguration = ApplicationInsights.Initialize( + instrumentationKey, + TimeSpan.FromSeconds(heartbeatIntervalSeconds)); + + applicationInsightsConfiguration.TelemetryConfiguration.TelemetryInitializers.Add(new AzureWebAppTelemetryInitializer()); + + RegisterApplicationInsightsTelemetryModules(applicationInsightsConfiguration.TelemetryConfiguration); + + return applicationInsightsConfiguration; + } + + private static void RegisterApplicationInsightsTelemetryModules(TelemetryConfiguration configuration) + { + RegisterApplicationInsightsTelemetryModule( + new AppServicesHeartbeatTelemetryModule(), + configuration); + + RegisterApplicationInsightsTelemetryModule( + new AzureInstanceMetadataTelemetryModule(), + configuration); + + RegisterApplicationInsightsTelemetryModule( + new DeveloperModeWithDebuggerAttachedTelemetryModule(), + configuration); + + RegisterApplicationInsightsTelemetryModule( + new UnhandledExceptionTelemetryModule(), + configuration); + + RegisterApplicationInsightsTelemetryModule( + new UnobservedExceptionTelemetryModule(), + configuration); + + var requestTrackingModule = new RequestTrackingTelemetryModule(); + requestTrackingModule.Handlers.Add("Microsoft.VisualStudio.Web.PageInspector.Runtime.Tracing.RequestDataHttpHandler"); + requestTrackingModule.Handlers.Add("System.Web.StaticFileHandler"); + requestTrackingModule.Handlers.Add("System.Web.Handlers.AssemblyResourceLoader"); + requestTrackingModule.Handlers.Add("System.Web.Optimization.BundleHandler"); + requestTrackingModule.Handlers.Add("System.Web.Script.Services.ScriptHandlerFactory"); + requestTrackingModule.Handlers.Add("System.Web.Handlers.TraceHandler"); + requestTrackingModule.Handlers.Add("System.Web.Services.Discovery.DiscoveryRequestHandler"); + requestTrackingModule.Handlers.Add("System.Web.HttpDebugHandler"); + RegisterApplicationInsightsTelemetryModule( + requestTrackingModule, + configuration); + + RegisterApplicationInsightsTelemetryModule( + new ExceptionTrackingTelemetryModule(), + configuration); + + RegisterApplicationInsightsTelemetryModule( + new AspNetDiagnosticTelemetryModule(), + configuration); + + var dependencyTrackingModule = new DependencyTrackingTelemetryModule(); + dependencyTrackingModule.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("core.windows.net"); + dependencyTrackingModule.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("core.chinacloudapi.cn"); + dependencyTrackingModule.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("core.cloudapi.de"); + dependencyTrackingModule.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("core.usgovcloudapi.net"); + dependencyTrackingModule.IncludeDiagnosticSourceActivities.Add("Microsoft.Azure.EventHubs"); + dependencyTrackingModule.IncludeDiagnosticSourceActivities.Add("Microsoft.Azure.ServiceBus"); + RegisterApplicationInsightsTelemetryModule( + dependencyTrackingModule, + configuration); + + RegisterApplicationInsightsTelemetryModule( + new PerformanceCollectorModule(), + configuration); + + RegisterApplicationInsightsTelemetryModule( + new QuickPulseTelemetryModule(), + configuration); + } + + private static void RegisterApplicationInsightsTelemetryModule(ITelemetryModule telemetryModule, TelemetryConfiguration configuration) + { + var existingModule = TelemetryModules.Instance.Modules.SingleOrDefault(m => m.GetType().Equals(telemetryModule.GetType())); + if (existingModule != null) + { + TelemetryModules.Instance.Modules.Remove(existingModule); + } + + telemetryModule.Initialize(configuration); + + TelemetryModules.Instance.Modules.Add(telemetryModule); + } + private static RefreshableConfiguration GetConfiguration() { const string prefix = "APPSETTING_"; diff --git a/src/NuGet.Services.SearchService/ApplicationInsights.config b/src/NuGet.Services.SearchService/ApplicationInsights.config index d695415d1..6975bcb8c 100644 --- a/src/NuGet.Services.SearchService/ApplicationInsights.config +++ b/src/NuGet.Services.SearchService/ApplicationInsights.config @@ -25,82 +25,6 @@ - - - - - - - - - - - - - - - - - - Microsoft.VisualStudio.Web.PageInspector.Runtime.Tracing.RequestDataHttpHandler - System.Web.StaticFileHandler - System.Web.Handlers.AssemblyResourceLoader - System.Web.Optimization.BundleHandler - System.Web.Script.Services.ScriptHandlerFactory - System.Web.Handlers.TraceHandler - System.Web.Services.Discovery.DiscoveryRequestHandler - System.Web.HttpDebugHandler - - - - - @@ -111,10 +35,4 @@ - \ No newline at end of file diff --git a/src/NuGet.Services.SearchService/NuGet.Services.SearchService.csproj b/src/NuGet.Services.SearchService/NuGet.Services.SearchService.csproj index af89d423b..e8d53d943 100644 --- a/src/NuGet.Services.SearchService/NuGet.Services.SearchService.csproj +++ b/src/NuGet.Services.SearchService/NuGet.Services.SearchService.csproj @@ -53,7 +53,6 @@ - @@ -85,10 +84,10 @@ all - 2.8.1 + 2.12.0 - 2.10.0 + 2.12.0 5.2.7 @@ -150,7 +149,7 @@ - + diff --git a/src/NuGet.Services.SearchService/Web.config b/src/NuGet.Services.SearchService/Web.config index e68aac17f..6c93e4e85 100644 --- a/src/NuGet.Services.SearchService/Web.config +++ b/src/NuGet.Services.SearchService/Web.config @@ -32,15 +32,19 @@ - - + + - - + + - + + + + + @@ -85,7 +89,7 @@ - + diff --git a/src/NuGet.Services.V3/DependencyInjectionExtensions.cs b/src/NuGet.Services.V3/DependencyInjectionExtensions.cs index 8ccf9cbba..aba36dd59 100644 --- a/src/NuGet.Services.V3/DependencyInjectionExtensions.cs +++ b/src/NuGet.Services.V3/DependencyInjectionExtensions.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System.Collections.Generic; using System.Net; using System.Net.Http; using Microsoft.Extensions.DependencyInjection; @@ -8,6 +9,7 @@ using NuGet.Jobs.Validation; using NuGet.Protocol.Catalog; using NuGet.Protocol.Registration; +using NuGet.Services.Logging; using NuGet.Services.Metadata.Catalog; using NuGetGallery.Diagnostics; @@ -15,7 +17,7 @@ namespace NuGet.Services.V3 { public static class DependencyInjectionExtensions { - public static IServiceCollection AddV3(this IServiceCollection services) + public static IServiceCollection AddV3(this IServiceCollection services, IDictionary telemetryGlobalDimensions) { services .AddTransient(p => new HttpClientHandler @@ -41,7 +43,9 @@ public static IServiceCollection AddV3(this IServiceCollection services) services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(p => new TelemetryService( + p.GetRequiredService(), + telemetryGlobalDimensions)); services.AddTransient(); return services; diff --git a/src/NuGet.Services.V3/NuGet.Services.V3.csproj b/src/NuGet.Services.V3/NuGet.Services.V3.csproj index ae7197834..18bf1dda2 100644 --- a/src/NuGet.Services.V3/NuGet.Services.V3.csproj +++ b/src/NuGet.Services.V3/NuGet.Services.V3.csproj @@ -72,7 +72,7 @@ all - 4.3.0-dev-3214091 + 4.3.0-dev-3391439 diff --git a/tests/CatalogTests/CatalogTests.csproj b/tests/CatalogTests/CatalogTests.csproj index 7f56d5e59..05da2a44a 100644 --- a/tests/CatalogTests/CatalogTests.csproj +++ b/tests/CatalogTests/CatalogTests.csproj @@ -337,7 +337,7 @@ 4.10.1 - 2.58.0 + 2.66.0 2.4.1 diff --git a/tests/CatalogTests/TelemetryHandlerTests.cs b/tests/CatalogTests/TelemetryHandlerTests.cs index 30bf05509..1e7c5e523 100644 --- a/tests/CatalogTests/TelemetryHandlerTests.cs +++ b/tests/CatalogTests/TelemetryHandlerTests.cs @@ -10,6 +10,7 @@ using System.Threading.Tasks; using Microsoft.ApplicationInsights; using Moq; +using NuGet.Services.Logging; using NuGet.Services.Metadata.Catalog; using Xunit; @@ -27,7 +28,7 @@ public class TelemetryHandlerTests public TelemetryHandlerTests() { - _telemetryService = new Mock(new TelemetryClient()); + _telemetryService = new Mock(new TelemetryClientWrapper(new TelemetryClient()), new Dictionary()); _telemetryService.Setup(x => x.TrackDuration(TelemetryConstants.HttpHeaderDurationSeconds, It.IsAny>())) .Callback((string name, IDictionary properties) => { _properties = properties; }).CallBase(); diff --git a/tests/NgTests/JobNameTelemetryInitializerTests.cs b/tests/NgTests/JobNameTelemetryInitializerTests.cs deleted file mode 100644 index 2d1f553ed..000000000 --- a/tests/NgTests/JobNameTelemetryInitializerTests.cs +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Ng; -using Xunit; - -namespace NgTests -{ - public class JobNameTelemetryInitializerTests - { - [Fact] - public void Constructor_WhenJobNameIsNull_Throws() - { - var exception = Assert.Throws( - () => new JobNameTelemetryInitializer(jobName: null, instanceName: "instanceName")); - - Assert.Equal("jobName", exception.ParamName); - } - - [Fact] - public void Constructor_WhenInstanceNameIsNull_Throws() - { - var exception = Assert.Throws( - () => new JobNameTelemetryInitializer(jobName: "jobName", instanceName: null)); - - Assert.Equal("instanceName", exception.ParamName); - } - - [Fact] - public void Initialize_WhenTelemetryIsNotNull_SetsJobNameAndInstanceName() - { - var telemetry = new TestableTelemetry(); - - var initializer = new JobNameTelemetryInitializer(jobName: "a", instanceName: "b"); - - initializer.Initialize(telemetry); - - Assert.Equal(2, telemetry.Properties.Count); - Assert.Equal("a", telemetry.Properties["JobName"]); - Assert.Equal("b", telemetry.Properties["InstanceName"]); - } - } -} \ No newline at end of file diff --git a/tests/NgTests/JobPropertiesTelemetryInitializerTests.cs b/tests/NgTests/JobPropertiesTelemetryInitializerTests.cs deleted file mode 100644 index 80b2e4e55..000000000 --- a/tests/NgTests/JobPropertiesTelemetryInitializerTests.cs +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using Microsoft.ApplicationInsights.Channel; -using Microsoft.ApplicationInsights.DataContracts; -using Moq; -using Ng; -using NuGet.Services.Metadata.Catalog; -using Xunit; - -namespace NgTests -{ - public class JobPropertiesTelemetryInitializerTests - { - private readonly Dictionary _globalDimensions; - private readonly TelemetryContext _telemetryContext; - private readonly Mock _telemetry; - private readonly Mock _telemetryService; - - public JobPropertiesTelemetryInitializerTests() - { - _globalDimensions = new Dictionary(); - _telemetryContext = new TelemetryContext(); - _telemetry = new Mock(); - _telemetryService = new Mock(); - - _telemetry.SetupGet(x => x.Context) - .Returns(_telemetryContext); - } - - [Fact] - public void Constructor_WhenTelemetryServiceIsNull_Throws() - { - var exception = Assert.Throws( - () => new JobPropertiesTelemetryInitializer(telemetryService: null)); - - Assert.Equal("telemetryService", exception.ParamName); - } - - [Fact] - public void Initialize_WhenGlobalDimensionsIsNull_DoesNothing() - { - _telemetryService.SetupGet(x => x.GlobalDimensions) - .Returns((IDictionary)null); - - var initializer = new JobPropertiesTelemetryInitializer(_telemetryService.Object); - - initializer.Initialize(_telemetry.Object); - - Assert.Empty(_telemetryContext.GlobalProperties); - } - - [Fact] - public void Initialize_WhenGlobalDimensionsIsEmpty_DoesNothing() - { - _telemetryService.SetupGet(x => x.GlobalDimensions) - .Returns(_globalDimensions); - - var telemetry = new TestableTelemetry(); - - var initializer = new JobPropertiesTelemetryInitializer(_telemetryService.Object); - - initializer.Initialize(telemetry); - - Assert.Empty(_telemetryContext.GlobalProperties); - } - - [Fact] - public void Initialize_WhenGlobalDimensionsIsNotEmpty_SetsTelemetry() - { - var globalDimensions = new Dictionary() - { - { "a", "b" }, - { "c", "d" } - }; - var telemetry = new TestableTelemetry(); - var telemetryService = new Mock(); - - telemetryService.SetupGet(x => x.GlobalDimensions) - .Returns(globalDimensions); - - var initializer = new JobPropertiesTelemetryInitializer(telemetryService.Object); - - initializer.Initialize(telemetry); - - Assert.Equal(2, telemetry.Properties.Count); - Assert.Equal("b", telemetry.Properties["a"]); - Assert.Equal("d", telemetry.Properties["c"]); - } - } -} \ No newline at end of file diff --git a/tests/NgTests/NgJobFactoryTests.cs b/tests/NgTests/NgJobFactoryTests.cs index 306530ea0..9008158d9 100644 --- a/tests/NgTests/NgJobFactoryTests.cs +++ b/tests/NgTests/NgJobFactoryTests.cs @@ -7,7 +7,7 @@ using Microsoft.Extensions.Logging; using Moq; using Ng; -using NuGet.Services.Metadata.Catalog; +using NuGet.Services.Logging; using Xunit; namespace NgTests @@ -19,14 +19,14 @@ public class NgJobFactoryTests public void CanActivateJobs(string name, Type type) { // Arrange - var telemetryService = new Mock(); + var telemetryClient = new Mock(); var loggerFactory = new Mock(); loggerFactory .Setup(x => x.CreateLogger(It.IsAny())) .Returns(() => new Mock().Object); // Act - var job = NgJobFactory.GetJob(name, telemetryService.Object, loggerFactory.Object); + var job = NgJobFactory.GetJob(name, loggerFactory.Object, telemetryClient.Object, new Dictionary()); // Assert Assert.NotNull(job); diff --git a/tests/NgTests/NgTests.csproj b/tests/NgTests/NgTests.csproj index 0c05b1cc1..7e4b6bcdc 100644 --- a/tests/NgTests/NgTests.csproj +++ b/tests/NgTests/NgTests.csproj @@ -73,8 +73,6 @@ - - @@ -93,7 +91,6 @@ - @@ -202,10 +199,10 @@ 4.10.1 - 2.58.0 + 2.66.0 - 2.58.0 + 2.66.0 2.4.1 diff --git a/tests/NgTests/TestableDb2CatalogJob.cs b/tests/NgTests/TestableDb2CatalogJob.cs index 54c7ba70e..42da0a428 100644 --- a/tests/NgTests/TestableDb2CatalogJob.cs +++ b/tests/NgTests/TestableDb2CatalogJob.cs @@ -2,12 +2,14 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Collections.Generic; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using Moq; using Ng.Jobs; using NgTests.Infrastructure; +using NuGet.Services.Logging; using NuGet.Services.Metadata.Catalog.Helpers; using NuGet.Services.Metadata.Catalog.Persistence; using Xunit.Abstractions; @@ -31,7 +33,7 @@ public TestableDb2CatalogJob( Mock galleryDatabaseMock, PackageContentUriBuilder packageContentUriBuilder, ITestOutputHelper testOutputHelper) - : base(new MockTelemetryService(), new TestLoggerFactory(testOutputHelper)) + : base(new TestLoggerFactory(testOutputHelper), new Mock().Object, new Dictionary()) { _handler = handler; diff --git a/tests/NgTests/TestableTelemetry.cs b/tests/NgTests/TestableTelemetry.cs deleted file mode 100644 index a43dc6122..000000000 --- a/tests/NgTests/TestableTelemetry.cs +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using Microsoft.ApplicationInsights.Channel; -using Microsoft.ApplicationInsights.DataContracts; -using Microsoft.ApplicationInsights.Extensibility; -using System; -using System.Collections.Generic; - -namespace NgTests -{ - public class TestableTelemetry : ITelemetry, ISupportProperties - { - private readonly IDictionary _properties = new Dictionary(); - - public DateTimeOffset Timestamp { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - - public TelemetryContext Context => throw new NotImplementedException(); - - public IExtension Extension { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - - public string Sequence { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - - public IDictionary Properties => _properties; - - public ITelemetry DeepClone() - { - throw new NotImplementedException(); - } - - public void Sanitize() - { - throw new NotImplementedException(); - } - - public void SerializeData(ISerializationWriter serializationWriter) - { - throw new NotImplementedException(); - } - } -} diff --git a/tests/NuGet.IndexingTests/NuGet.IndexingTests.csproj b/tests/NuGet.IndexingTests/NuGet.IndexingTests.csproj index 294bbd8a5..7f2bb3504 100644 --- a/tests/NuGet.IndexingTests/NuGet.IndexingTests.csproj +++ b/tests/NuGet.IndexingTests/NuGet.IndexingTests.csproj @@ -95,7 +95,7 @@ 4.10.1 - 2.58.0 + 2.66.0 2.4.1 diff --git a/tests/NuGet.Services.BasicSearchTests/NuGet.Services.BasicSearchTests.csproj b/tests/NuGet.Services.BasicSearchTests/NuGet.Services.BasicSearchTests.csproj index db16b1dfd..45f3eaf99 100644 --- a/tests/NuGet.Services.BasicSearchTests/NuGet.Services.BasicSearchTests.csproj +++ b/tests/NuGet.Services.BasicSearchTests/NuGet.Services.BasicSearchTests.csproj @@ -122,10 +122,10 @@ 3.0.1 - 2.58.0 + 2.66.0 - 2.58.0 + 2.66.0 2.4.1 diff --git a/tests/NuGet.Services.SearchService.Tests/App_Start/AzureWebAppTelemetryInitializerFacts.cs b/tests/NuGet.Services.SearchService.Tests/App_Start/AzureWebAppTelemetryInitializerFacts.cs deleted file mode 100644 index 5652148a7..000000000 --- a/tests/NuGet.Services.SearchService.Tests/App_Start/AzureWebAppTelemetryInitializerFacts.cs +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using Microsoft.ApplicationInsights.Channel; -using Microsoft.ApplicationInsights.DataContracts; -using Moq; -using Xunit; - -namespace NuGet.Services.SearchService -{ - public class AzureWebAppTelemetryInitializerFacts - { - [Theory] - [InlineData(null, null)] - [InlineData("hello", "hello")] - [InlineData("-staging-test", "-staging-test")] - [InlineData("hello-staging", "hello")] - [InlineData("hello-sTAGing", "hello")] - public void UpdatesRoleName(string input, string expected) - { - var telemetry = new Mock(); - var context = new TelemetryContext(); - - context.Cloud.RoleName = input; - - telemetry.Setup(t => t.Context).Returns(context); - - var target = new AzureWebAppTelemetryInitializer(); - target.Initialize(telemetry.Object); - - Assert.Equal(expected, context.Cloud.RoleName); - } - } -} diff --git a/tests/NuGet.Services.SearchService.Tests/NuGet.Services.SearchService.Tests.csproj b/tests/NuGet.Services.SearchService.Tests/NuGet.Services.SearchService.Tests.csproj index f70294e1f..58f5c8d6e 100644 --- a/tests/NuGet.Services.SearchService.Tests/NuGet.Services.SearchService.Tests.csproj +++ b/tests/NuGet.Services.SearchService.Tests/NuGet.Services.SearchService.Tests.csproj @@ -38,7 +38,6 @@ -