@ -15,8 +15,10 @@ public class TelemetryApplicationInfoEnricher : ITelemetryActivityDataEnricher,
private readonly IEnumerable < ITelemetryApplicationInfoContributor > _ telemetryApplicationInfoContributors ;
private readonly ITelemetryActivityStorage _ telemetryActivityStorage ;
public TelemetryApplicationInfoEnricher ( ITelemetrySessionTypeProvider telemetrySessionTypeProvider ,
IEnumerable < ITelemetryApplicationInfoContributor > telemetryApplicationInfoContributors , ITelemetryActivityStorage telemetryActivityStorage )
public TelemetryApplicationInfoEnricher (
ITelemetrySessionTypeProvider telemetrySessionTypeProvider ,
IEnumerable < ITelemetryApplicationInfoContributor > telemetryApplicationInfoContributors ,
ITelemetryActivityStorage telemetryActivityStorage )
{
_ telemetrySessionTypeProvider = telemetrySessionTypeProvider ;
_ telemetryApplicationInfoContributors = telemetryApplicationInfoContributors ;
@ -25,27 +27,64 @@ public class TelemetryApplicationInfoEnricher : ITelemetryActivityDataEnricher,
public async Task EnrichAsync ( ActivityData activity )
{
if ( activity . ContainsKey ( ActivityPropertyNames . Assembly ) & & _ telemetrySessionTypeProvider . SessionType = = SessionType . ApplicationRuntime )
if ( ! ShouldEnrichActivity ( activity ) )
{
if ( ! activity . TryGetValue ( ActivityPropertyNames . ProjectId , out var projectIdObj ) | |
projectIdObj is not string projectIdStr | |
! Guid . TryParse ( projectIdStr , out var projectId ) )
return ;
}
var projectId = ExtractProjectId ( activity ) ;
if ( projectId = = null )
{
return ;
}
try
{
if ( ! await _ telemetryActivityStorage . ShouldAddApplicationInfoAsync ( projectId . Value ) )
{
return ;
}
if ( ! await _ telemetryActivityStorage . ShouldAddApplicationInfoAsync ( projectId ) )
await ContributeApplicationInfoAsync ( activity ) ;
activity . Remove ( ActivityPropertyNames . Assembly ) ;
await _ telemetryActivityStorage . MarkApplicationInfoAsAddedAsync ( projectId . Value ) ;
}
catch
{
// ignored
}
}
private bool ShouldEnrichActivity ( ActivityData activity )
{
return activity . ContainsKey ( ActivityPropertyNames . Assembly ) & &
_ telemetrySessionTypeProvider . SessionType = = SessionType . ApplicationRuntime ;
}
private static Guid ? ExtractProjectId ( ActivityData activity )
{
if ( ! activity . TryGetValue ( ActivityPropertyNames . ProjectId , out var projectIdObj ) | |
projectIdObj is not string projectIdStr | |
! Guid . TryParse ( projectIdStr , out var projectId ) )
{
return null ;
}
return projectId ;
}
private async Task ContributeApplicationInfoAsync ( ActivityData activity )
{
foreach ( var contributor in _ telemetryApplicationInfoContributors )
{
try
{
return ;
await contributor . ContributeAsync ( activity ) ;
}
foreach ( var contributor in _ telemetryApplicationInfoContributors )
catch
{
await contributor . ContributeAsync ( activity ) ;
// ignored
}
activity . Remove ( ActivityPropertyNames . Assembly ) ;
await _ telemetryActivityStorage . MarkApplicationInfoAsAddedAsync ( projectId ) ;
}
}
}