Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
/ NuGet.Jobs Public archive

Commit

Permalink
Add additional telemetry for SQL queries invoked by Db2Catalog (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierdecoster authored May 29, 2019
1 parent 5eaffe8 commit 62c7cad
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 34 deletions.
34 changes: 34 additions & 0 deletions src/Catalog/Helpers/Db2CatalogCursor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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.Data.SqlTypes;

namespace NuGet.Services.Metadata.Catalog.Helpers
{
public sealed class Db2CatalogCursor
{
public const string ColumnNameCreated = "Created";
public const string ColumnNameLastEdited = "LastEdited";

private Db2CatalogCursor(string columnName, DateTime cursorValue, int top)
{
ColumnName = columnName ?? throw new ArgumentNullException(nameof(columnName));
CursorValue = cursorValue < SqlDateTime.MinValue.Value ? SqlDateTime.MinValue.Value : cursorValue;

if (top <= 0)
{
throw new ArgumentOutOfRangeException("Argument value must be a positive non-zero integer.", nameof(top));
}

Top = top;
}

public string ColumnName { get; }
public DateTime CursorValue { get; }
public int Top { get; }

public static Db2CatalogCursor ByCreated(DateTime since, int top) => new Db2CatalogCursor(ColumnNameCreated, since, top);
public static Db2CatalogCursor ByLastEdited(DateTime since, int top) => new Db2CatalogCursor(ColumnNameLastEdited, since, top);
}
}
39 changes: 9 additions & 30 deletions src/Catalog/Helpers/GalleryDatabaseQueryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Threading.Tasks;
using NuGet.Services.Entities;
using NuGet.Services.Sql;
Expand All @@ -20,14 +19,17 @@ public class GalleryDatabaseQueryService : IGalleryDatabaseQueryService

private readonly ISqlConnectionFactory _connectionFactory;
private readonly Db2CatalogProjection _db2catalogProjection;
private readonly ITelemetryService _telemetryService;
private readonly int _commandTimeout;

public GalleryDatabaseQueryService(
ISqlConnectionFactory connectionFactory,
PackageContentUriBuilder packageContentUriBuilder,
ITelemetryService telemetryService,
int commandTimeout)
{
_connectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory));
_telemetryService = telemetryService ?? throw new ArgumentNullException(nameof(telemetryService));
_db2catalogProjection = new Db2CatalogProjection(packageContentUriBuilder);
_commandTimeout = commandTimeout;
}
Expand Down Expand Up @@ -153,42 +155,19 @@ private async Task<IReadOnlyCollection<FeedPackageDetails>> GetPackageDetailsAsy
{
packagesCommand.Parameters.AddWithValue(CursorParameterName, cursor.CursorValue);

using (var packagesReader = await packagesCommand.ExecuteReaderAsync())
using (_telemetryService.TrackGetPackageDetailsQueryDuration(cursor))
{
while (await packagesReader.ReadAsync())
using (var packagesReader = await packagesCommand.ExecuteReaderAsync())
{
packages.Add(_db2catalogProjection.FromDataRecord(packagesReader));
while (await packagesReader.ReadAsync())
{
packages.Add(_db2catalogProjection.FromDataRecord(packagesReader));
}
}
}
}

return packages;
}

public sealed class Db2CatalogCursor
{
public const string ColumnNameCreated = "Created";
public const string ColumnNameLastEdited = "LastEdited";

private Db2CatalogCursor(string columnName, DateTime cursorValue, int top)
{
ColumnName = columnName ?? throw new ArgumentNullException(nameof(columnName));
CursorValue = cursorValue < SqlDateTime.MinValue.Value ? SqlDateTime.MinValue.Value : cursorValue;

if (top <= 0)
{
throw new ArgumentOutOfRangeException("Argument value must be a positive non-zero integer.", nameof(top));
}

Top = top;
}

public string ColumnName { get; }
public DateTime CursorValue { get; }
public int Top { get; }

public static Db2CatalogCursor ByCreated(DateTime since, int top) => new Db2CatalogCursor(ColumnNameCreated, since, top);
public static Db2CatalogCursor ByLastEdited(DateTime since, int top) => new Db2CatalogCursor(ColumnNameLastEdited, since, top);
}
}
}
1 change: 1 addition & 0 deletions src/Catalog/NuGet.Services.Metadata.Catalog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
<Compile Include="Dnx\DnxEntry.cs" />
<Compile Include="GetCatalogCommitItemKey.cs" />
<Compile Include="Helpers\AsyncExtensions.cs" />
<Compile Include="Helpers\Db2CatalogCursor.cs" />
<Compile Include="Helpers\Db2CatalogProjection.cs" />
<Compile Include="Extensions\IDataRecordExtensions.cs" />
<Compile Include="Helpers\GalleryDatabaseQueryService.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/Catalog/Telemetry/ITelemetryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using NuGet.Services.Logging;
using NuGet.Services.Metadata.Catalog.Helpers;
using NuGet.Versioning;

namespace NuGet.Services.Metadata.Catalog
Expand All @@ -30,5 +31,6 @@ public interface ITelemetryService
void TrackMetric(string name, ulong metric, IDictionary<string, string> properties = null);
DurationMetric TrackDuration(string name, IDictionary<string, string> properties = null);
void TrackIconExtractionFailure(string packageId, string normalizedPackageVersion);
IDisposable TrackGetPackageDetailsQueryDuration(Db2CatalogCursor cursor);
}
}
5 changes: 3 additions & 2 deletions src/Catalog/Telemetry/TelemetryConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public static class TelemetryConstants
public const string Version = "Version";
public const string Handler = "Handler";
public const string IconExtractionFailed = "IconExtractionFailed";
public const string GetPackageDetailsSeconds = "GetPackageDetailsSeconds";
public const string CursorValue = "CursorValue";
}
}

}
13 changes: 13 additions & 0 deletions src/Catalog/Telemetry/TelemetryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using Microsoft.ApplicationInsights;
using NuGet.Services.Logging;
using NuGet.Services.Metadata.Catalog.Helpers;
using NuGet.Versioning;

namespace NuGet.Services.Metadata.Catalog
Expand Down Expand Up @@ -205,5 +206,17 @@ public void TrackIconExtractionFailure(string packageId, string normalizedPackag
{ TelemetryConstants.Version, normalizedPackageVersion }
});
}

public IDisposable TrackGetPackageDetailsQueryDuration(Db2CatalogCursor cursor)
{
var properties = new Dictionary<string, string>()
{
{ TelemetryConstants.Method, cursor.ColumnName },
{ TelemetryConstants.BatchItemCount, cursor.Top.ToString() },
{ TelemetryConstants.CursorValue, cursor.CursorValue.ToString("O") }
};

return _telemetryClient.TrackDuration(TelemetryConstants.GetPackageDetailsSeconds, properties);
}
}
}
1 change: 1 addition & 0 deletions src/Ng/Jobs/Db2CatalogJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ protected override void Init(IDictionary<string, string> arguments, Cancellation
GalleryDatabaseQueryService = new GalleryDatabaseQueryService(
GalleryDbConnection,
PackageContentUriBuilder,
TelemetryService,
timeoutInSeconds);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/CatalogTests/Helpers/Db2CatalogCursorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using NuGet.Services.Metadata.Catalog.Helpers;
using Xunit;
using Db2CatalogCursor = NuGet.Services.Metadata.Catalog.Helpers.GalleryDatabaseQueryService.Db2CatalogCursor;

namespace CatalogTests.Helpers
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using NuGet.Services.Metadata.Catalog.Helpers;
using Xunit;
using CatalogConstants = NuGet.Services.Metadata.Catalog.Constants;
using Db2CatalogCursor = NuGet.Services.Metadata.Catalog.Helpers.GalleryDatabaseQueryService.Db2CatalogCursor;

namespace CatalogTests.Helpers
{
Expand Down
13 changes: 13 additions & 0 deletions tests/NgTests/Infrastructure/MockTelemetryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Moq;
using NuGet.Services.Logging;
using NuGet.Services.Metadata.Catalog;
using NuGet.Services.Metadata.Catalog.Helpers;
using NuGet.Versioning;

namespace NgTests.Infrastructure
Expand Down Expand Up @@ -80,5 +81,17 @@ public void TrackMetric(string name, ulong metric, IDictionary<string, string> p
public void TrackIconExtractionFailure(string packageId, string normalizedPackageVersion)
{
}

public IDisposable TrackGetPackageDetailsQueryDuration(Db2CatalogCursor cursor)
{
var properties = new Dictionary<string, string>()
{
{ TelemetryConstants.Method, cursor.ColumnName },
{ TelemetryConstants.BatchItemCount, cursor.Top.ToString() },
{ TelemetryConstants.CursorValue, cursor.CursorValue.ToString("O") }
};

return TrackDuration(nameof(TrackGetPackageDetailsQueryDuration), properties);
}
}
}

0 comments on commit 62c7cad

Please sign in to comment.