Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add telemetry for service entries #6047

Draft
wants to merge 8 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ public void Dispose()
public async Task SendTelemetryAsync()
{
var parentId = _parentId.ToString();
int NumberOfHTTPSSourcesWithHTTPResource = 0;
int NumberOfHTTPSSources = 0;
bool sendResourceTelemetry = false;

foreach (var kvp in _data)
{
Data data = kvp.Value;
Expand All @@ -209,13 +213,64 @@ public async Task SendTelemetryAsync()
sourceRepository = new SourceRepository(new PackageSource(source), Repository.Provider.GetCoreV3());
}

Uri sourceUri = null;
bool hasAnHttpResource = false;

try
{
sourceUri = new Uri(source);
}
catch (ArgumentException)
{

}
catch (UriFormatException)
{

}
finally
{
if (sourceUri != null)
{
if (sourceUri.Scheme == Uri.UriSchemeHttps)
{
NumberOfHTTPSSources++;
}
}
}


if (sourceRepository.GetServiceIndexV3FromCache(out ServiceIndexResourceV3 resource))
{
sendResourceTelemetry |= true;
var entries = resource.Entries;

foreach (var entry in entries)
{
var resourceUri = entry.Uri;

if (sourceUri != null && resourceUri.Scheme == Uri.UriSchemeHttp && resourceUri.Scheme != Uri.UriSchemeHttps)
{
hasAnHttpResource |= true;
}
}
}

NumberOfHTTPSSourcesWithHTTPResource += hasAnHttpResource ? 1 : 0;

var telemetry = await ToTelemetryAsync(data, sourceRepository, parentId, _actionName, _packageSourceMappingConfiguration);

if (telemetry != null)
{
TelemetryActivity.EmitTelemetryEvent(telemetry);
}
}

if (sendResourceTelemetry)
{
var resourceTelemetry = new ServiceIndexHttpResourcesTelemetry(NumberOfHTTPSSources, NumberOfHTTPSSourcesWithHTTPResource, "ServiceIndexHttpResourcesSummary");
TelemetryActivity.EmitTelemetryEvent(resourceTelemetry);
}
}

internal static async Task<TelemetryEvent> ToTelemetryAsync(Data data, SourceRepository sourceRepository, string parentId, string actionName, PackageSourceMapping packageSourceMappingConfiguration)
Expand Down Expand Up @@ -323,6 +378,15 @@ private static TelemetryEvent ToStatusCodeTelemetry(Dictionary<int, int> statusC
return subevent;
}

private class ServiceIndexHttpResourcesTelemetry : TelemetryEvent
{
public ServiceIndexHttpResourcesTelemetry(int numberOfSources, int numberOfHttpsSourcesWithHttpAnResource, string eventName) : base(eventName)
{
this["NumHTTPSSources"] = numberOfSources;
this["NumHTTPSSourcesWithAnHTTPResource"] = numberOfHttpsSourcesWithHttpAnResource;
}
}

internal static string GetMsFeed(PackageSource source)
{
if (source.IsHttp)
Expand Down
15 changes: 14 additions & 1 deletion src/NuGet.Core/NuGet.Protocol/CachingSourceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,20 @@ public SourceRepository CreateRepository(PackageSource source, FeedType type)

public void AddSourceRepository(SourceRepository source)
{
_cachedSources.TryAdd(source.PackageSource.Source, source);
// Update or add to the dictionary
_cachedSources[source.PackageSource.Source] = source;

// Find the existing source in the list
var existingSource = _repositories.FirstOrDefault(r => r.PackageSource.Source == source.PackageSource.Source);

if (existingSource != null)
{
_repositories[_repositories.IndexOf(existingSource)] = source;
}
else
{
_repositories.Add(source);
}
}

public IPackageSourceProvider PackageSourceProvider
Expand Down
1 change: 0 additions & 1 deletion src/NuGet.Core/NuGet.Protocol/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'object SemanticVersionConverter.ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)', validate parameter 'reader' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Protocol.SemanticVersionConverter.ReadJson(Newtonsoft.Json.JsonReader,System.Type,System.Object,Newtonsoft.Json.JsonSerializer)~System.Object")]
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'void SemanticVersionConverter.WriteJson(JsonWriter writer, object value, JsonSerializer serializer)', validate parameter 'serializer' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Protocol.SemanticVersionConverter.WriteJson(Newtonsoft.Json.JsonWriter,System.Object,Newtonsoft.Json.JsonSerializer)")]
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'ServiceIndexResourceV3.ServiceIndexResourceV3(JObject index, DateTime requestTime)', validate parameter 'index' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Protocol.ServiceIndexResourceV3.#ctor(Newtonsoft.Json.Linq.JObject,System.DateTime)")]
[assembly: SuppressMessage("Build", "CA1822:Member ConsumeServiceIndexStreamAsync does not access instance data and can be marked as static (Shared in VisualBasic)", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Protocol.ServiceIndexResourceV3Provider.ConsumeServiceIndexStreamAsync(System.IO.Stream,System.DateTime,System.Threading.CancellationToken)~System.Threading.Tasks.Task{NuGet.Protocol.ServiceIndexResourceV3}")]
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'Task<Tuple<bool, INuGetResource>> ServiceIndexResourceV3Provider.TryCreate(SourceRepository source, CancellationToken token)', validate parameter 'source' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Protocol.ServiceIndexResourceV3Provider.TryCreate(NuGet.Protocol.Core.Types.SourceRepository,System.Threading.CancellationToken)~System.Threading.Tasks.Task{System.Tuple{System.Boolean,NuGet.Protocol.Core.Types.INuGetResource}}")]
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'Task StreamExtensions.CopyToAsync(Stream stream, Stream destination, CancellationToken token)', validate parameter 'stream' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Protocol.StreamExtensions.CopyToAsync(System.IO.Stream,System.IO.Stream,System.Threading.CancellationToken)~System.Threading.Tasks.Task")]
#if IS_DESKTOP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;

Expand Down Expand Up @@ -149,7 +150,7 @@ private async Task<ServiceIndexResourceV3> GetServiceIndexResourceV3(
},
async httpSourceResult =>
{
var result = await ConsumeServiceIndexStreamAsync(httpSourceResult.Stream, utcNow, token);
var result = await ConsumeServiceIndexStreamAsync(httpSourceResult.Stream, utcNow, source.PackageSource, token);

return result;
},
Expand Down Expand Up @@ -194,7 +195,7 @@ ex.InnerException is IOException &&
return null;
}

private async Task<ServiceIndexResourceV3> ConsumeServiceIndexStreamAsync(Stream stream, DateTime utcNow, CancellationToken token)
private static async Task<ServiceIndexResourceV3> ConsumeServiceIndexStreamAsync(Stream stream, DateTime utcNow, PackageSource source, CancellationToken token)
{
// Parse the JSON
JObject json = await stream.AsJObjectAsync(token);
Expand Down Expand Up @@ -226,6 +227,16 @@ private async Task<ServiceIndexResourceV3> ConsumeServiceIndexStreamAsync(Stream
}
}

internal ServiceIndexResourceV3 GetIndexFromCache(string sourceUri)
{
if (_cache.TryGetValue(sourceUri, out ServiceIndexCacheInfo cacheInfo))
{
return cacheInfo.Index;
}

return null;
}

protected class ServiceIndexCacheInfo
{
public ServiceIndexResourceV3 Index { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#nullable enable
~NuGet.Protocol.Core.Types.SourceRepository.GetServiceIndexV3FromCache(out NuGet.Protocol.ServiceIndexResourceV3 result) -> bool
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#nullable enable
~NuGet.Protocol.Core.Types.SourceRepository.GetServiceIndexV3FromCache(out NuGet.Protocol.ServiceIndexResourceV3 result) -> bool
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#nullable enable
~NuGet.Protocol.Core.Types.SourceRepository.GetServiceIndexV3FromCache(out NuGet.Protocol.ServiceIndexResourceV3 result) -> bool
19 changes: 19 additions & 0 deletions src/NuGet.Core/NuGet.Protocol/SourceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,25 @@ public virtual async Task<T> GetResourceAsync<T>(CancellationToken token) where
return null;
}

/// <summary>
/// Attempts to retrieve a cached <see cref="ServiceIndexResourceV3"/> object from the provider cache.
/// </summary>
public bool GetServiceIndexV3FromCache(out ServiceIndexResourceV3 result)
{
if (_providerCache.TryGetValue(typeof(ServiceIndexResourceV3), out INuGetResourceProvider[] providers))
{
if (providers.FirstOrDefault() is ServiceIndexResourceV3Provider provider)
{
result = provider.GetIndexFromCache(PackageSource.Source);
return result != null;
}
}

result = null;
return false;
}


/// <summary>
/// Initialize provider cache
/// </summary>
Expand Down
Loading
Loading