/// <summary> /// Validate the event name complies with data model event name schema. /// </summary> /// <param name="eventName"></param> private static void ValidateEventName(string eventName) { int num = 0; CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(eventName, "eventName"); if (eventName[0] == '/' || eventName[eventName.Length - 1] == '/') { throw new ArgumentException("Event name shouldn't start or end with slash.", "eventName"); } for (int i = 0; i < eventName.Length; i++) { if (eventName[i] == ' ') { throw new ArgumentException("Event name shouldn't have whitespaces.", "eventName"); } if (eventName[i] == '/') { num++; if (i > 0 && eventName[i - 1] == '/') { throw new ArgumentException("Event name shouldn't have successive slashes.", "eventName"); } } } if (num < 2) { throw new ArgumentException("Event name should have at least 3 parts separated by slash.", "eventName"); } }
/// <summary> /// Set new flight. /// </summary> /// <param name="flightName"></param> /// <param name="timeoutInMinutes"></param> public void SetFlight(string flightName, int timeoutInMinutes) { CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(flightName, "flightName"); if (timeoutInMinutes < 0) { throw new ArgumentException("Flight expiration timeout can't be negative", "timeoutInMinutes"); } flightName = flightName.ToLowerInvariant(); DateTimeOffset expiration = DateTimeOffset.UtcNow.AddMinutes(timeoutInMinutes); lock (lockObject) { IEnumerable <string> rawFlights = GetRawFlights(); foreach (string item in rawFlights) { FlightInformation flightInformation = FlightInformation.Parse(item); if (flightInformation != null && flightInformation.Flight == flightName) { return; } } SetRawFlights(rawFlights.Union(new string[1] { new FlightInformation(flightName, expiration).ToString() })); } }
/// <summary> /// Register correlation from a given asset id with specified asset type. /// </summary> /// <param name="assetTypeName">Asset type name. It is defined by asset provider.</param> /// <param name="assetId"> /// Used to identify the asset. The id should be immutable in the asset life cycle, even if the status or content changes over time. /// E.g., project guid is generated during project creation and will never change. This makes it a good candidate for asset id of Project asset. /// </param> /// <param name="correlation">correlation of the asset.</param> /// <remarks> /// Used by Asset Provider. /// </remarks> public void RegisterCorrelation(string assetTypeName, string assetId, TelemetryEventCorrelation correlation) { CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(assetTypeName, "assetTypeName"); CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(assetId, "assetId"); CacheKey key = new CacheKey(assetTypeName, assetId); registeredCorrelations[key] = correlation; }
/// <summary> /// Unregister correlation from this service. /// </summary> /// <param name="assetTypeName">Asset type name. It is defined by asset provider.</param> /// <param name="assetId"> /// Used to identify the asset. The id should be immutable in the asset life cycle, even if the status or content changes over time. /// E.g., project guid is generated during project creation and will never change. This makes it a good candidate for asset id of Project asset. /// </param> /// <remarks> /// Used by Asset Provider. /// Call this method when previous registered asset correlation is stale. /// </remarks> public void UnregisterCorrelation(string assetTypeName, string assetId) { CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(assetTypeName, "assetTypeName"); CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(assetId, "assetId"); CacheKey key = new CacheKey(assetTypeName, assetId); registeredCorrelations.TryRemove(key, out TelemetryEventCorrelation _); }
/// <summary> /// Set shared property for the session means set shared property for the default context /// You may want to consider <see cref="T:Coding4Fun.VisualStudio.Telemetry.TelemetrySettingProperty" /> or <see cref="T:Coding4Fun.VisualStudio.Telemetry.TelemetryMetricProperty" /> /// These will enable a richer telemetry experience with additional insights provided by Visual Studio Data Model. /// If you have any questions regarding VS Data Model, please email VS Data Model Crew ([email protected]). /// </summary> /// <param name="propertyName"></param> /// <param name="propertyValue"></param> public void SetSharedProperty(string propertyName, object propertyValue) { if (!base.IsDisposed) { CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(propertyName, "propertyName"); defaultContext.SharedProperties[propertyName] = propertyValue; } }
/// <summary> /// Remove shared property for the session means remove shared property from the default context /// </summary> /// <param name="propertyName"></param> public void RemoveSharedProperty(string propertyName) { if (!base.IsDisposed) { CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(propertyName, "propertyName"); defaultContext.SharedProperties.Remove(propertyName); } }
/// <summary> /// Returns default properties that should be on each TelemetryEvent /// </summary> /// <param name="eventTime">A time when the event happend</param> /// <param name="processStartTime">A time when the session started</param> /// <param name="sessionId"></param> /// <returns></returns> protected virtual IEnumerable <KeyValuePair <string, object> > GetDefaultEventProperties(long eventTime, long processStartTime, string sessionId) { CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(sessionId, "sessionId"); yield return(new KeyValuePair <string, object>("TimeSinceSessionStart", Math.Round(new TimeSpan(eventTime - processStartTime).TotalMilliseconds))); yield return(new KeyValuePair <string, object>("EventId", eventId)); yield return(new KeyValuePair <string, object>("SessionId", sessionId)); }
/// <summary> /// Post context property. That property is posted to the backend immediately and not attached to the every event. /// Property could be reserved or regular. /// Reserved property will accomplished with prefix "Reserved." /// </summary> /// <param name="propertyName"></param> /// <param name="propertyValue"></param> /// <param name="isReserved">is property reserved</param> internal void PostProperty(string propertyName, object propertyValue, bool isReserved) { if (!base.IsDisposed) { CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(propertyName, "propertyName"); CodeContract.RequiresArgumentNotNull <object>(propertyValue, "propertyValue"); postedProperties.Enqueue(new PostPropertyEntry(propertyName, propertyValue, isReserved)); Action action = FlushPostedProperties; scheduler.ScheduleTimed(action); } }
/// <summary> /// Creates the new telemetry event instance with specific information. /// </summary> /// <param name="eventName">Event name that is unique, not null and not empty.</param> /// <param name="severity">Severity level of the event.</param> /// <param name="correlation">Correlation value for this event.</param> internal TelemetryEvent(string eventName, TelemetrySeverity severity, TelemetryEventCorrelation correlation) { CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(eventName, "eventName"); correlation.RequireNotEmpty("correlation"); TelemetryService.EnsureEtwProviderInitialized(); this.eventName = eventName.ToLower(CultureInfo.InvariantCulture); Severity = severity; Correlation = correlation; EventSchemaVersion = 5; InitDataModelBasicProperties(); }
/// <summary> /// Validate property name. Check whether property name prefix is not match to the reserved prefixes. /// </summary> /// <param name="propertyName"></param> internal static void ValidatePropertyName(string propertyName) { CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(propertyName, "propertyName"); if (IsPropertyNameReserved(propertyName)) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "property '{0}' has reserved prefix '{1}'", new object[2] { propertyName, "Context." })); } }
/// <summary> /// Remove persisted shared property for all sessions means remove shared property from this default context, /// and for any future sessions on the machine. /// </summary> /// <param name="propertyName"></param> public void RemovePersistedSharedProperty(string propertyName) { if (!base.IsDisposed) { CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(propertyName, "propertyName"); dynamic persistedSharedProperty = GetPersistedSharedProperty(propertyName); persistentSharedProperties.RemoveProperty(propertyName); defaultContext.SharedProperties.Remove(propertyName); if (persistedSharedProperty != null) { TelemetryEvent telemetryEvent = new TelemetryEvent("VS/TelemetryApi/PersistedSharedProperty/Remove"); telemetryEvent.Properties["VS.TelemetryApi.PersistedSharedProperty.Name"] = propertyName; PostEvent(telemetryEvent); } } }
private void SetPersistedSharedProperty(string propertyName, object propertyValue, Action addToBagAction) { if (!base.IsDisposed) { CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(propertyName, "propertyName"); dynamic persistedSharedProperty = GetPersistedSharedProperty(propertyName); addToBagAction(); defaultContext.SharedProperties[propertyName] = propertyValue; if (persistedSharedProperty == null || !persistedSharedProperty.Equals(propertyValue)) { TelemetryEvent telemetryEvent = new TelemetryEvent("VS/TelemetryApi/PersistedSharedProperty/Set"); telemetryEvent.Properties["VS.TelemetryApi.PersistedSharedProperty.Name"] = propertyName; telemetryEvent.Properties["VS.TelemetryApi.PersistedSharedProperty.IsChangedValue"] = (object)(persistedSharedProperty != null); PostEvent(telemetryEvent); } } }
/// <summary> /// Get correlation for a given asset type and asset id. /// </summary> /// <param name="assetTypeName">Asset type name. It is defined by asset provider.</param> /// <param name="assetId"> /// Used to identify the asset. The id should be immutable in the asset life cycle, even if the status or content changes over time. /// E.g., project guid is generated during project creation and will never change. This makes it a good candidate for asset id of Project asset. /// You can get more information from asset provider. /// </param> /// <returns>Asset correlation</returns> public TelemetryEventCorrelation GetCorrelation(string assetTypeName, string assetId) { CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(assetTypeName, "assetTypeName"); CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(assetId, "assetId"); IAssetProvider provider = null; CacheKey key = new CacheKey(assetTypeName, assetId); TelemetryEventCorrelation correlation = TelemetryEventCorrelation.Empty; lock (locker) { if (!registeredCorrelations.TryGetValue(key, out correlation)) { correlation = TelemetryEventCorrelation.Empty; if (registeredProviders.TryGetValue(assetTypeName, out provider)) { correlation = new TelemetryEventCorrelation(Guid.NewGuid(), DataModelEventType.Asset); RegisterCorrelation(assetTypeName, assetId, correlation); } } } if (provider != null) { ThreadScheduler.Schedule(delegate { bool flag = false; try { flag = provider.PostAsset(assetId, correlation); } finally { if (!flag) { UnregisterCorrelation(assetTypeName, assetId); } } }); } return(correlation); }
public void Initialize(string sessionId, string userId) { CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(sessionId, "sessionId"); TelemetryConfiguration telemetryConfiguration = TelemetryConfiguration.CreateDefault(); if (telemetryConfiguration.TelemetryChannel != null) { telemetryConfiguration.TelemetryChannel.Dispose(); telemetryConfiguration.TelemetryChannel = null; } telemetryConfiguration.TelemetryInitializers.Remove(telemetryConfiguration.TelemetryInitializers.FirstOrDefault((ITelemetryInitializer o) => o is TimestampPropertyInitializer)); appInsightsChannel = CreateAppInsightsChannel(telemetryConfiguration); TelemetryClient telemetryClient = new TelemetryClient(telemetryConfiguration); Coding4Fun.VisualStudio.ApplicationInsights.DataContracts.TelemetryContext context = telemetryClient.Context; context.InstrumentationKey = InstrumentationKey; context.Session.Id = sessionId; context.User.Id = userId; context.Device.Type = "0"; context.Device.Id = "0"; appInsightsClient = telemetryClient; }
public FlightsRemoteFileReaderFactoryBase(string configPath) { CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(configPath, "configPath"); this.configPath = configPath; }
public BaseAppInsightsClientWrapper(string instrumentationKey) { CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(instrumentationKey, "instrumentationKey"); this.instrumentationKey = instrumentationKey; }
/// <summary> /// Register asset provider which can send asset event and return correlation per consumers' request. /// </summary> /// <param name="assetTypeName">Asset type name. It is defined by asset provider.</param> /// <param name="assetProvider">Asset provider</param> public void RegisterProvider(string assetTypeName, IAssetProvider assetProvider) { CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(assetTypeName, "assetTypeName"); CodeContract.RequiresArgumentNotNull <IAssetProvider>(assetProvider, "assetProvider"); registeredProviders[assetTypeName] = assetProvider; }
/// <summary> /// Unregister asset provider. /// </summary> /// <param name="assetTypeName">Asset type name. It is defined by asset provider.</param> public void UnregisterProvider(string assetTypeName) { CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(assetTypeName, "assetTypeName"); registeredProviders.TryRemove(assetTypeName, out IAssetProvider _); }
/// <summary> /// Get correlation for a given asset type and asset id. /// </summary> /// <param name="assetTypeName">Asset type name. It is defined by asset provider.</param> /// <param name="assetId"> /// Used to identify the asset. The id should be immutable in the asset life cycle, even if the status or content changes over time. /// E.g., project guid is generated during project creation and will never change. This makes it a good candidate for asset id of Project asset. /// You can get more information from asset provider. /// </param> /// <returns>Asset correlation</returns> public TelemetryEventCorrelation GetCorrelation(string assetTypeName, Guid assetId) { CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(assetTypeName, "assetTypeName"); CodeContract.RequiresArgumentNotEmpty(assetId, "assetId"); return(GetCorrelation(assetTypeName, assetId.ToString("D"))); }
/// <summary> /// Deserialize a json string to <see cref="T:Coding4Fun.VisualStudio.Telemetry.TelemetryEventCorrelation" /> object. /// </summary> /// <param name="jsonString"> /// A json string to include id and event type properties. /// E.g., {"id":"d7149afe-632f-4278-b94e-3915a79ee60f","eventType":"Operation"} /// </param> /// <returns> /// A <see cref="T:Coding4Fun.VisualStudio.Telemetry.TelemetryEventCorrelation" /> object. /// It would throw <see cref="T:Newtonsoft.Json.JsonReaderException" /> if parameter jsonString is an invalid Json string. /// It would throw <see cref="T:Newtonsoft.Json.JsonSerializationException" /> if any of conditions below happen, /// 1. Miss properties "id" or "eventType" /// 2. Invalid property value. "id" value should be a guid string, and "eventType" should be enum name from <see cref="T:Coding4Fun.VisualStudio.Telemetry.DataModelEventType" />. /// </returns> public static TelemetryEventCorrelation Deserialize(string jsonString) { CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(jsonString, "jsonString"); return(JsonConvert.DeserializeObject <TelemetryEventCorrelation>(jsonString)); }
/// <summary> /// Register correlation from a given asset id with specified asset type. /// </summary> /// <param name="assetTypeName">Asset type name. It is defined by asset provider.</param> /// <param name="assetId"> /// Used to identify the asset. The id should be immutable in the asset life cycle, even if the status or content changes over time. /// E.g., project guid is generated during project creation and will never change. This makes it a good candidate for asset id of Project asset. /// </param> /// <param name="correlation">correlation of the asset.</param> /// <remarks> /// Used by Asset Provider. /// </remarks> public void RegisterCorrelation(string assetTypeName, Guid assetId, TelemetryEventCorrelation correlation) { CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(assetTypeName, assetTypeName); CodeContract.RequiresArgumentNotEmpty(assetId, "assetId"); RegisterCorrelation(assetTypeName, assetId.ToString("D"), correlation); }
/// <summary> /// Correlate this event with other event via <see cref="T:Coding4Fun.VisualStudio.Telemetry.TelemetryEventCorrelation" /> with description information. /// </summary> /// <param name="correlation">The property <see cref="P:Coding4Fun.VisualStudio.Telemetry.TelemetryEvent.Correlation" /> of correlated event.</param> /// <param name="description"> /// A description string for this correlation information, such as name, hint, tag, category. /// Please don't include comma which is a reserved char. /// </param> /// <remarks> /// This method is not thread-safe. /// </remarks> public void Correlate(TelemetryEventCorrelation correlation, string description) { CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(description, "description"); CorrelateWithDescription(correlation, description); }
/// <summary> /// Unregister correlation from this service. /// </summary> /// <param name="assetTypeName">Asset type name. It is defined by asset provider.</param> /// <param name="assetId"> /// Used to identify the asset. The id should be immutable in the asset life cycle, even if the status or content changes over time. /// E.g., project guid is generated during project creation and will never change. This makes it a good candidate for asset id of Project asset. /// </param> /// <remarks> /// Used by Asset Provider. /// Call this method when previous registered asset correlation is stale. /// </remarks> public void UnregisterCorrelation(string assetTypeName, Guid assetId) { CodeContract.RequiresArgumentNotNullAndNotWhiteSpace(assetTypeName, "assetTypeName"); CodeContract.RequiresArgumentNotEmpty(assetId, "assetId"); UnregisterCorrelation(assetTypeName, assetId.ToString("D")); }