Skip to content

Commit 8178e16

Browse files
committed
Add support for alternate instrumentation system in browser (#7971)
Progress on NuGet/Engineering#3020
1 parent 7322a06 commit 8178e16

10 files changed

Lines changed: 68 additions & 19 deletions

File tree

src/AccountDeleter/Configuration/GalleryConfiguration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,6 @@ public string SiteRoot
112112
public int? MaxWorkerThreads { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
113113
public int? MinIoThreads { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
114114
public int? MaxIoThreads { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
115+
public string InternalMicrosoftTenantKey { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
115116
}
116117
}

src/NuGetGallery.Services/Configuration/AppConfiguration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,5 +413,6 @@ public string ExternalBrandingMessage
413413
public int? MinIoThreads { get; set; }
414414
[DefaultValue(null)]
415415
public int? MaxIoThreads { get; set; }
416+
public string InternalMicrosoftTenantKey { get; set; }
416417
}
417418
}

src/NuGetGallery.Services/Configuration/IAppConfiguration.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ public interface IAppConfiguration : IMessageServiceConfiguration
210210
/// </summary>
211211
int AppInsightsHeartbeatIntervalSeconds { get; set; }
212212

213+
/// <summary>
214+
/// The tenant key used for Microsoft's internal instrumentation system similar to Application Insights.
215+
/// Because it is internal, this setting is not useful if used outside of Microsoft.
216+
/// </summary>
217+
string InternalMicrosoftTenantKey { get; set; }
218+
213219
/// <summary>
214220
/// Gets the protocol-independent site root
215221
/// </summary>

src/NuGetGallery/App_Code/ViewHelpers.cshtml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,32 @@
223223

224224
@helper InstrumentationScript()
225225
{
226-
// Get instrumentation key
226+
// Get instrumentation keys
227227
var config = DependencyResolver.Current.GetService<IGalleryConfigurationService>();
228228
var iKey = config == null ? string.Empty : config.Current.AppInsightsInstrumentationKey;
229229
var samplingPct = config == null ? 100 : config.Current.AppInsightsSamplingPercentage;
230+
var tenantKey = config == null ? string.Empty : config.Current.InternalMicrosoftTenantKey;
230231

231-
if (!string.IsNullOrEmpty(iKey))
232+
if (!string.IsNullOrEmpty(iKey) || !string.IsNullOrEmpty(tenantKey))
232233
{
233234
var cookieService = DependencyResolver.Current.GetService<ICookieComplianceService>();
234235
if (cookieService.CanWriteNonEssentialCookies(Request))
235236
{
236-
<!-- Telemetry -->
237+
if (!string.IsNullOrEmpty(tenantKey))
238+
{
239+
@System.Web.Optimization.Scripts.Render("~/Scripts/gallery/instrumentation.min.js")
240+
<script type="text/javascript">
241+
if (window.initializeNuGetInstrumentation) {
242+
window.NuGetInstrumentation = window.initializeNuGetInstrumentation({
243+
appInsightsInstrumentationKey: "@iKey",
244+
appInsightsSamplingPercentage: @samplingPct,
245+
tenantKey: "@tenantKey",
246+
});
247+
}
248+
</script>
249+
}
250+
else
251+
{
237252
<script type="text/javascript">
238253
var appInsights = window.appInsights || function (config) {
239254
function s(config) {
@@ -257,6 +272,7 @@
257272
window.appInsights = appInsights;
258273
appInsights.trackPageView();
259274
</script>
275+
}
260276
}
261277
}
262278
}

src/NuGetGallery/App_Start/AppActivator.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ private static void BundlingPostStart()
124124
.Include("~/Content/gallery/css/fabric.css");
125125
BundleTable.Bundles.Add(newStyleBundle);
126126

127+
var instrumentationBundle = new ScriptBundle("~/Scripts/gallery/instrumentation.min.js")
128+
.Include("~/Scripts/gallery/instrumentation.js");
129+
BundleTable.Bundles.Add(instrumentationBundle);
130+
127131
var scriptBundle = new ScriptBundle("~/Scripts/gallery/site.min.js")
128132
.Include("~/Scripts/gallery/jquery-3.4.1.js")
129133
.Include("~/Scripts/gallery/jquery.validate-1.16.0.js")

src/NuGetGallery/NuGetGallery.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,6 +1635,7 @@
16351635
</Content>
16361636
<Content Include="Areas\Admin\Views\Revalidation\Index.cshtml" />
16371637
<Content Include="App_Data\Files\Content\Symbols-Configuration.json" />
1638+
<Content Include="Scripts\gallery\instrumentation.js" />
16381639
<Content Include="Views\Shared\SiteMenu.cshtml">
16391640
<SubType>Code</SubType>
16401641
</Content>

src/NuGetGallery/Scripts/gallery/common.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@
340340
return typeof window.appInsights === 'object';
341341
};
342342

343+
nuget.isInstrumentationAvailable = function () {
344+
return typeof window.NuGetInstrumentation === 'object';
345+
};
346+
343347
nuget.getDateFormats = function (input) {
344348
var datetime = moment.utc(input);
345349

@@ -444,8 +448,16 @@
444448
}
445449
};
446450

447-
nuget.sendAiMetric = function (name, value, properties) {
448-
if (window.nuget.isAiAvailable()) {
451+
nuget.sendMetric = function (name, value, properties) {
452+
if (window.nuget.isInstrumentationAvailable()) {
453+
window.NuGetInstrumentation.trackMetric({
454+
name: name,
455+
average: value,
456+
sampleCount: 1,
457+
min: value,
458+
max: value
459+
}, properties);
460+
} else if (window.nuget.isAiAvailable()) {
449461
window.appInsights.trackMetric(name, value, 1, value, value, properties);
450462
}
451463
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
window["initializeNuGetInstrumentation"] = function (config) {
2+
var instrumentation = {
3+
initialize: function () {
4+
console.log("NuGet instrumentation shim: initialized %o", config);
5+
},
6+
trackMetric: function (metric, customProperties) {
7+
console.log("NuGet instrumentation shim: metric %o %o", metric, customProperties);
8+
}
9+
};
10+
instrumentation.initialize();
11+
return instrumentation;
12+
};

src/NuGetGallery/Scripts/gallery/page-home.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ $(function () {
4949
data: obj,
5050
success: function (data) {
5151
if (data.success) {
52-
emitAIMetric("Enable2FAModalProvidedFeedback");
52+
emitMetric("Enable2FAModalProvidedFeedback");
5353
viewModel.dismissModalOrGetFeedback(false);
5454
} else {
5555
viewModel.message(data.message);
@@ -65,7 +65,7 @@ $(function () {
6565
viewModel.setupFeedbackView();
6666
}
6767
else {
68-
emitAIMetric('Enable2FAModalDismissed');
68+
emitMetric('Enable2FAModalDismissed');
6969
$("#popUp2FAModal").modal('hide');
7070
}
7171
},
@@ -109,15 +109,15 @@ $(function () {
109109

110110
function show2FAModal() {
111111
viewModel.setupEnable2FAView();
112-
emitAIMetric("Enable2FAModalShown");
112+
emitMetric("Enable2FAModalShown");
113113
$("#popUp2FAModal").modal({
114114
show: true,
115115
focus: true
116116
});
117117
}
118118

119-
function emitAIMetric(metricName) {
120-
window.nuget.sendAiMetric(metricName, 1, {});
119+
function emitMetric(metricName) {
120+
window.nuget.sendMetric(metricName, 1, {});
121121
}
122122

123123
function updateStats() {

src/NuGetGallery/Views/Shared/ListPackages.cshtml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
// event to be correlated in Google Analytics.
9292
<text>
9393
window.nuget.sendAnalyticsEvent('@category', '@action', @Html.Raw(Json.Encode(Model.SearchTerm)), @Model.PageIndex);
94-
window.nuget.sendAiMetric('BrowserSearchPage', @Model.PageIndex, {
94+
window.nuget.sendMetric('BrowserSearchPage', @Model.PageIndex, {
9595
SearchId: '@searchId',
9696
SearchTerm: @Html.Raw(Json.Encode(Model.SearchTerm)),
9797
IncludePrerelease: '@Model.IncludePrerelease',
@@ -103,15 +103,11 @@
103103
}
104104
105105
$(function () {
106-
var emitAiClickEvent = function () {
107-
if (!window.nuget.isAiAvailable()) {
108-
return;
109-
}
110-
106+
var emitClickEvent = function () {
111107
var $this = $(this);
112108
var data = $this.data();
113109
if ($this.attr('href') && data.track) {
114-
window.nuget.sendAiMetric('BrowserSearchSelection', data.trackValue, {
110+
window.nuget.sendMetric('BrowserSearchSelection', data.trackValue, {
115111
SearchId: '@searchId',
116112
SearchTerm: @Html.Raw(Json.Encode(Model.SearchTerm)),
117113
IncludePrerelease: '@Model.IncludePrerelease',
@@ -132,11 +128,11 @@
132128
$.each($('a[data-track]'), function () {
133129
$(this).on('mouseup', function (e) {
134130
if (e.which === 2) { // Middle-mouse click
135-
emitAiClickEvent.call(this, e);
131+
emitClickEvent.call(this, e);
136132
}
137133
});
138134
$(this).on('click', function (e) {
139-
emitAiClickEvent.call(this, e);
135+
emitClickEvent.call(this, e);
140136
});
141137
});
142138
});

0 commit comments

Comments
 (0)