1+ // Copyright (c) .NET Foundation. All rights reserved.
2+ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+ using System ;
5+ using System . Collections . Generic ;
6+ using System . Web ;
7+ using Microsoft . ApplicationInsights . Channel ;
8+ using Microsoft . ApplicationInsights . DataContracts ;
9+ using Microsoft . ApplicationInsights . Extensibility ;
10+
11+ namespace NuGetGallery
12+ {
13+ public class CustomerResourceIdEnricher : ITelemetryInitializer
14+ {
15+ private const string CustomerResourceId = "CustomerResourceId" ;
16+ private const string Prefix = "/tenants/" ;
17+ private static readonly string Empty = Prefix + Guid . Empty . ToString ( ) ;
18+
19+ private static readonly HashSet < string > CustomMetricNames = new HashSet < string >
20+ {
21+ TelemetryService . Events . PackagePush ,
22+ TelemetryService . Events . PackagePushFailure ,
23+ } ;
24+
25+ public void Initialize ( ITelemetry telemetry )
26+ {
27+ var metric = telemetry as MetricTelemetry ;
28+ if ( metric == null )
29+ {
30+ return ;
31+ }
32+
33+ if ( ! CustomMetricNames . Contains ( metric . Name ) )
34+ {
35+ return ;
36+ }
37+
38+ var itemTelemetry = telemetry as ISupportProperties ;
39+ if ( itemTelemetry == null )
40+ {
41+ return ;
42+ }
43+
44+ var httpContext = GetHttpContext ( ) ;
45+ var tenantId = httpContext ? . User ? . Identity . GetTenantIdOrNull ( ) ;
46+ var customerResourceId = tenantId != null ? Prefix + tenantId : Empty ;
47+ itemTelemetry . Properties [ CustomerResourceId ] = customerResourceId ;
48+ }
49+
50+ protected virtual HttpContextBase GetHttpContext ( ) => HttpContextBaseExtensions . GetCurrent ( ) ;
51+ }
52+ }
0 commit comments