public async Task InvokeAsync_SetsActivityProperties(int statusCode, TimedScopeResult result) { string scheme = "https"; int localPort = 8080; Task RequestDelegate(HttpContext context) { context.Response.StatusCode = statusCode; context.Request.Scheme = scheme; context.Connection.LocalPort = localPort; return(Task.CompletedTask); } HttpContext context = new DefaultHttpContext(); IMiddleware middleware = new ActivityEnrichmentMiddleware(); Activity activity = new Activity("TestName").Start(); await middleware.InvokeAsync(context, RequestDelegate).ConfigureAwait(false); activity.Stop(); activity.AssertResult(result); activity.AssertTag(ActivityTagKeys.SubType, $"{scheme}:{localPort}"); activity.AssertTag(ActivityTagKeys.Metadata, statusCode.ToString()); }
public void LogEvent( Category logCategory, string name, string subtype, string metadata, string userHash, string serviceName, TimedScopeResult result, string correlationId, double durationMs) { string nameAsString = SanitizeString(name, nameof(name), name, logCategory); string subTypeAsString = SanitizeString(subtype, nameof(subtype), name, logCategory); string metaDataAsString = SanitizeString(metadata, nameof(metadata), name, logCategory); string userHashAsString = SanitizeString(userHash, nameof(userHash), name, logCategory); string serviceNameAsString = SanitizeString(serviceName, nameof(serviceName), name, logCategory); string correlationIdAsString = SanitizeString(correlationId, nameof(correlationId), name, logCategory); WriteTimedScopeEvent( nameAsString, subTypeAsString, metaDataAsString, userHashAsString, serviceNameAsString, logCategory.Name, result.ToString(), correlationIdAsString, Convert.ToInt64(durationMs, CultureInfo.InvariantCulture)); }
/// <summary> /// Constructor /// </summary> /// <param name="scopeName">Strongy typed cope name</param> /// <param name="classification">Failure classification</param> public TimedScopeInstanceName(TimedScopeName scopeName, TimedScopeResult classification) { Code.ExpectsArgument(scopeName, nameof(scopeName), TaggingUtilities.ReserveTag(0x23817705 /* tag_96x2f */)); CompleteScopeName = scopeName; Classification = classification; }
private void TestExecution( string scopeName, Action <Task <bool>, ITimedScopeProvider, TimedScopeDefinition> createTask, Action <TaskCompletionSource <bool> > finishTask, TimedScopeResult expectedResult) { Mock <ITimedScopeProvider> providerMock = new Mock <ITimedScopeProvider>(); TimedScopeDefinition timedScopeDefinition = new TimedScopeDefinition(scopeName); Activity activity = new Activity(scopeName); TimedScope scope = new TimedScope(activity, TimedScopeResult.SystemError); providerMock.Setup(p => p.CreateAndStart(timedScopeDefinition, TimedScopeResult.SystemError)).Returns(scope); TaskCompletionSource <bool> taskCompletionSource = new TaskCompletionSource <bool>(); createTask(taskCompletionSource.Task, providerMock.Object, timedScopeDefinition); providerMock.Verify(p => p.CreateAndStart(timedScopeDefinition, TimedScopeResult.SystemError), Times.Once); scope.Activity.AssertResult(TimedScopeResult.SystemError); finishTask(taskCompletionSource); scope.Activity.AssertResult(expectedResult); }
public void CreateAndStart_TimedScopeCreated() { TimedScopeResult result = TimedScopeResult.ExpectedError; TimedScopeDefinition definition = new TimedScopeDefinition(nameof(CreateAndStart_TimedScopeCreated)); SimpleScopeProvider provider = new SimpleScopeProvider(); TimedScope scope = provider.CreateAndStart(definition, result); Assert.IsNotNull(scope); }
public void SetResult_SetsValue(TimedScopeResult result) { Activity activity1 = new Activity("SetResultTest1"); Activity activity2 = new Activity("SetResultTest2"); activity1.SetResult(result); activity1.AssertResult(result); Assert.IsNull(activity2.GetTag(ActivityTagKeys.Result)); }
public void SetResult_SetsValue(TimedScopeResult result) { TimedScope scope1 = CreateScope("SetResultTest1"); TimedScope scope2 = CreateScope("SetResultTest2"); scope1.SetResult(result); scope1.Activity.AssertResult(result); scope2.Activity.AssertResult(TimedScopeResult.SystemError); }
/// <summary> /// Create a timed scope /// </summary> /// <param name="scopeDefinition">Timed scope definition</param> /// <param name="initialResult">The default result for the scope</param> /// <param name="customLogger">Use a custom logger for the timed scope</param> /// <param name="replayEventConfigurator">Replay event configurator</param> /// <param name="timedScopeStackManager">Timed scope stack manager</param> /// <param name="correlationData">Correlation data</param> /// <param name="machineInformation">Machine Information</param> /// <returns>Newly created scope</returns> public static TimedScope Create(TimedScopeDefinition scopeDefinition, CorrelationData correlationData, IMachineInformation machineInformation, ITimedScopeLogger customLogger, IReplayEventConfigurator replayEventConfigurator, ITimedScopeStackManager timedScopeStackManager, TimedScopeResult initialResult = default(TimedScopeResult)) { return(new TimedScope(scopeDefinition, correlationData, customLogger, replayEventConfigurator, machineInformation, timedScopeStackManager) { TimedScopeData = correlationData, RunningTransaction = TransactionMonitor.RunningTransaction(correlationData), Result = initialResult, }); }
/// <summary> /// Decides whether we should replay events for scopes with given result /// </summary> /// <param name="result">The result</param> /// <returns>true if we should replay events for this result; false otherwise</returns> public static bool ShouldReplayEvents(this TimedScopeResult result) { switch (result) { case TimedScopeResult.SystemError: return(true); default: return(false); } }
public void DefaultTimedScopeResult_IsSeparateValue() { // Default value should never be used explicitly and is marked as Obsolete for that reason, so it can't be used. // But we need a separate default value to support the "not yet set" state. Verify the default is actually separate // and does not map to any of the real result values. TimedScopeResult result = default(TimedScopeResult); Assert.NotEqual(TimedScopeResult.Success, result); Assert.NotEqual(TimedScopeResult.SystemError, result); Assert.NotEqual(TimedScopeResult.ExpectedError, result); }
private TimedScope CreateTimedScope() { Activity activity = new Activity("TestName"); TimedScopeResult result = TimedScopeResult.Success; TimedScope scope = new TimedScope(activity, result); Assert.AreEqual(activity, scope.Activity); scope.Activity.AssertResult(result); return(scope); }
/// <summary> /// Creates a scope (and starts by default) /// </summary> /// <param name="correlationData">Correlation Data</param> /// <param name="machineInformation">Machine Information</param> /// <param name="initialResult">Initial result to use</param> /// <param name="startScope">Should the scope be automatically started (for use in e.g. 'using' statement)</param> /// <param name="customLogger">Optional custom timed scope logger</param> /// <param name="replayEventConfigurator">Optional replay event configurator</param> /// <param name="timedScopeStackManager">Timed scope stack manager</param> /// <returns>A timed scope</returns> public TimedScope Create(CorrelationData correlationData, IMachineInformation machineInformation, ITimedScopeLogger customLogger, IReplayEventConfigurator replayEventConfigurator, ITimedScopeStackManager timedScopeStackManager, TimedScopeResult initialResult = default(TimedScopeResult), bool startScope = true) { TimedScope scope = TimedScope.Create(this, correlationData, machineInformation, customLogger, replayEventConfigurator, timedScopeStackManager, initialResult); if (startScope) { scope.Start(); } return(scope); }
/// <summary> /// Constructor /// </summary> /// <param name="name">Scope name</param> /// <param name="subtype">Subtype</param> /// <param name="metadata">Metadata</param> /// <param name="result">Scope result</param> /// <param name="failureDescription">Failure description</param> /// <param name="userHash">User hash</param> /// <param name="duration">Duration</param> public TimedScopeLogEvent(string name, string subtype, string metadata, TimedScopeResult result, Enum failureDescription, string userHash, TimeSpan duration) { Name = name; SubType = subtype; Result = result; MetaData = metadata; FailureDescriptionEnum = failureDescription; UserHash = userHash; Duration = duration; }
private void CreateAndValidateActivity(string activityName) { TimedScopeResult result = TimedScopeResult.ExpectedError; Mock <IActivityProvider> activityProviderMock = new Mock <IActivityProvider>(); Mock <Activity> activityMock = new Mock <Activity>(activityName); TimedScopeDefinition definition = new TimedScopeDefinition(activityName); activityProviderMock.Setup(p => p.Create(definition)).Returns(activityMock.Object); TimedScopeProvider provider = new TimedScopeProvider(activityProviderMock.Object); TimedScope scope = provider.CreateAndStart(definition, result); Assert.IsNotNull(scope); }
/// <summary> /// Decides whether the result is success /// </summary> /// <param name="result">The result</param> /// <returns>Success flag (null if we don't know)</returns> public static bool?IsSuccessful(this TimedScopeResult result) { switch (result) { case default(TimedScopeResult): return(null); case TimedScopeResult.Success: return(true); case TimedScopeResult.ExpectedError: case TimedScopeResult.SystemError: return(false); default: ULSLogging.LogTraceTag(0x23817701 /* tag_96x2b */, Categories.TimingGeneral, Levels.Error, "IsSuccessful status unknown for timed scope result {0}", result); return(false); } }
public void OnStop_UsingReplaySettingsAndResult_CallesLogReplayIfNeeded(TimedScopeResult result, bool replayLog, bool shouldBeCalled) { Activity activity = new Activity(nameof(OnStop_UsingReplaySettingsAndResult_CallesLogReplayIfNeeded)); TimedScope timedScope = new TimedScope(activity, result); IOptions <OmexLoggingOptions> options = Options.Create(new OmexLoggingOptions { ReplayLogsInCaseOfError = replayLog }); LogEventReplayerMock replayerMock = new LogEventReplayerMock(); ReplayableActivityStopObserver observer = new ReplayableActivityStopObserver(replayerMock, options); observer.OnStop(activity, null); if (shouldBeCalled) { Assert.AreEqual(activity, replayerMock.Activity); } else { Assert.IsNull(replayerMock.Activity); } }
/// <summary> /// Set result /// </summary> /// <remarks>This property won't be transferred to child activity or via web requests</remarks> public static Activity SetResult(this Activity activity, TimedScopeResult result) => activity.SetTag(ActivityTagKeys.Result, ActivityResultStrings.ResultToString(result));
/// <summary> /// Creates a scope (and starts by default) /// </summary> /// <param name="definition">TimeScope definition</param> /// <param name="initialResult">Initial result to use</param> /// <param name="startScope">Should the scope be automatically started (for use in e.g. 'using' statement)</param> public TimedScope Create(TimedScopeDefinition definition, TimedScopeResult initialResult, bool startScope = true) => definition.Create(m_correlation.CurrentCorrelation, m_machineInformation, m_scopeLogger, m_replayEventConfigurator, m_timedScopeStackManager, initialResult, startScope);
/// <summary> /// Returns corresponding to enum string value with creating new string /// </summary> public static string ResultToString(TimedScopeResult result) => result switch {
/// <summary> /// Set result /// </summary> /// <remarks>This property won't be transfered to child activity or via web requests</remarks> public static TimedScope SetResult(this TimedScope timedScope, TimedScopeResult result) { timedScope.Activity.SetResult(result); return(timedScope); }
public static void AssertResult(this Activity activity, TimedScopeResult expectedResult) => Assert.AreEqual(ActivityResultStrings.ResultToString(expectedResult), activity.GetTag(ActivityTagKeys.Result));
/// <summary> /// Creates TimedScope instance /// </summary> /// <param name="activity">activity connected to this timedscope</param> /// <param name="result">TimedScope initial result</param> public TimedScope(Activity activity, TimedScopeResult result) { Activity = activity; this.SetResult(result); }
/// <summary> /// Constructor /// </summary> /// <param name="scope">Scope name</param> /// <param name="classification">Failure classification</param> /// <param name="subType">SubType</param> /// <param name="metaData">MetaData</param> public TimedScopeInstanceName(string scope, TimedScopeResult classification, string subType = null, string metaData = null) : this(new TimedScopeName(scope, subType, metaData), classification) { }
/// <inheritdoc /> public TimedScope Create(TimedScopeDefinition name, TimedScopeResult result = TimedScopeResult.SystemError) => new TimedScope(new System.Diagnostics.Activity(name.Name), result);
/// <inheritdoc /> public TimedScope CreateAndStart(TimedScopeDefinition name, TimedScopeResult result = TimedScopeResult.SystemError) => Create(name, result).Start();
/// <summary> /// Starts a scope /// </summary> /// <param name="correlationData">Correlation Data</param> /// <param name="machineInformation">Machine Information</param> /// <param name="initialResult">Initial result to use</param> /// <param name="customLogger">Optional custom timed scope logger</param> /// <param name="replayEventConfigurator">Optional replay event configurator</param> /// <param name="timedScopeStackManager">Timed scope stack manager</param> /// <returns>A timed scope</returns> public TimedScope Start(CorrelationData correlationData, IMachineInformation machineInformation, ITimedScopeLogger customLogger, IReplayEventConfigurator replayEventConfigurator, ITimedScopeStackManager timedScopeStackManager, TimedScopeResult initialResult = default(TimedScopeResult)) => Create(correlationData, machineInformation, customLogger, replayEventConfigurator, timedScopeStackManager, initialResult: initialResult, startScope: true);
/// <summary> /// Deprecated - Create a timed scope /// </summary> /// <remarks>Please use TimedScopeDefinition for creating timed scopes</remarks> /// <param name="correlationData">Correlation data</param> /// <param name="machineInformation">Machine Information</param> /// <param name="scopeName">The name of the timed scope</param> /// <param name="description">The description of the timed scope</param> /// <param name="initialResult">The default result for the scope</param> /// <param name="customLogger">Use a custom logger for the timed scope</param> /// <param name="replayEventConfigurator">Replay event configurator</param> /// <param name="timedScopeStackManager">Timed scope stack manager</param> /// <returns>newly created scope</returns> public static TimedScope Create(CorrelationData correlationData, IMachineInformation machineInformation, string scopeName, string description, ITimedScopeLogger customLogger, IReplayEventConfigurator replayEventConfigurator, ITimedScopeStackManager timedScopeStackManager, TimedScopeResult initialResult = default(TimedScopeResult)) => new TimedScopeDefinition(scopeName, description).Create(correlationData, machineInformation, customLogger, replayEventConfigurator, timedScopeStackManager, initialResult: initialResult, startScope: false);
/// <summary> /// Creates an instance name with provided scope result /// </summary> /// <param name="result">Scope result</param> /// <returns>Returns instance name</returns> public TimedScopeInstanceName CreateInstanceName(TimedScopeResult result) { return(new TimedScopeInstanceName(this, result)); }
public static TimedScope Create(this TimedScopeDefinition timedScopeDefinition, TimedScopeResult initialResult, bool startScope = true) { if (s_timedScopeProvider == null) { throw new OmexCompatibilityInitializationException(); } TimedScope scope = s_timedScopeProvider.Create(timedScopeDefinition, initialResult); if (startScope) { scope.Start(); } return(scope); }
/// <summary> /// Deprecated - Start a timed scope /// </summary> /// <remarks>Please use TimedScopeDefinition for creating timed scopes</remarks> /// <param name="correlationData">Correlation data</param> /// <param name="machineInformation">Machine Information</param> /// <param name="scopeName">The name of the timed scope</param> /// <param name="initialResult">The default result for the scope</param> /// <param name="customLogger">Use a custom logger for the timed scope</param> /// <param name="replayEventConfigurator">Replay event configurator</param> /// <param name="timedScopeStackManager">Timed scope stack manager</param> /// <returns>Newly created scope</returns> public static TimedScope Start(CorrelationData correlationData, IMachineInformation machineInformation, string scopeName, ITimedScopeLogger customLogger, IReplayEventConfigurator replayEventConfigurator, ITimedScopeStackManager timedScopeStackManager, TimedScopeResult initialResult = default(TimedScopeResult)) => new TimedScopeDefinition(scopeName).Start(correlationData, machineInformation, customLogger, replayEventConfigurator, timedScopeStackManager, initialResult);