in BASE/src/Microsoft.ApplicationInsights/TelemetryClient.cs [493:591]
public void Initialize(ITelemetry telemetry)
{
if (telemetry == null)
{
throw new ArgumentNullException(nameof(telemetry));
}
ISupportAdvancedSampling telemetryWithSampling = telemetry as ISupportAdvancedSampling;
// Telemetry can be already sampled out if that decision was made before calling Track()
bool sampledOut = false;
if (telemetryWithSampling != null)
{
sampledOut = telemetryWithSampling.ProactiveSamplingDecision == SamplingDecision.SampledOut;
}
if (!sampledOut)
{
if (telemetry is ISupportProperties telemetryWithProperties)
{
if (this.configuration.TelemetryChannel?.DeveloperMode != null && this.configuration.TelemetryChannel.DeveloperMode.Value)
{
if (!telemetryWithProperties.Properties.ContainsKey("DeveloperMode"))
{
telemetryWithProperties.Properties.Add("DeveloperMode", "true");
}
}
}
// Properties set of TelemetryClient's Context are copied over to that of ITelemetry's Context
#pragma warning disable CS0618 // Type or member is obsolete
if (this.Context.PropertiesValue != null)
{
Utils.CopyDictionary(this.Context.Properties, telemetry.Context.Properties);
}
#pragma warning restore CS0618 // Type or member is obsolete
// This check avoids accessing the public accessor GlobalProperties
// unless needed, to avoid the penalty of ConcurrentDictionary instantiation.
if (this.Context.GlobalPropertiesValue != null)
{
Utils.CopyDictionary(this.Context.GlobalProperties, telemetry.Context.GlobalProperties);
}
string instrumentationKey = this.Context.InstrumentationKey;
if (string.IsNullOrEmpty(instrumentationKey))
{
instrumentationKey = this.configuration.InstrumentationKey;
}
telemetry.Context.Initialize(this.Context, instrumentationKey);
for (int index = 0; index < this.configuration.TelemetryInitializers.Count; index++)
{
try
{
this.configuration.TelemetryInitializers[index].Initialize(telemetry);
}
catch (Exception exception)
{
CoreEventSource.Log.LogError(string.Format(
CultureInfo.InvariantCulture,
"Exception while initializing {0}, exception message - {1}",
this.configuration.TelemetryInitializers[index].GetType().FullName,
exception));
}
}
if (telemetry.Timestamp == default(DateTimeOffset))
{
telemetry.Timestamp = PreciseTimestamp.GetUtcNow();
}
// Currently backend requires SDK version to comply "name: version"
if (string.IsNullOrEmpty(telemetry.Context.Internal.SdkVersion))
{
var version = this.sdkVersion ?? (this.sdkVersion = SdkVersionUtils.GetSdkVersion(VersionPrefix));
telemetry.Context.Internal.SdkVersion = version;
}
// set NodeName to the machine name if it's not initialized yet, if RoleInstance is also not set then we send only RoleInstance
if (string.IsNullOrEmpty(telemetry.Context.Internal.NodeName) && !string.IsNullOrEmpty(telemetry.Context.Cloud.RoleInstance))
{
telemetry.Context.Internal.NodeName = PlatformSingleton.Current.GetMachineName();
}
// set RoleInstance to the machine name if it's not initialized yet
if (string.IsNullOrEmpty(telemetry.Context.Cloud.RoleInstance))
{
telemetry.Context.Cloud.RoleInstance = PlatformSingleton.Current.GetMachineName();
}
}
else
{
CoreEventSource.Log.InitializationIsSkippedForSampledItem();
}
}