Skip to content

Commit

Permalink
More complete Agent.cs tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaffinito committed Oct 11, 2024
1 parent c63e02a commit 9bf58a2
Showing 1 changed file with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ public void SetUp()
_attribDefSvc = new AttributeDefinitionService((f) => new AttributeDefinitions(f));
_labelsService = Mock.Create<ILabelsService>();

var labels = new List<Label> { new Label("key", "value") };
Mock.Arrange(() => _labelsService.Labels).Returns(labels);

var scheduler = Mock.Create<IScheduler>();
Mock.Arrange(() => scheduler.ExecuteEvery(Arg.IsAny<Action>(), Arg.IsAny<TimeSpan>(), Arg.IsAny<TimeSpan?>()))
.DoInstead<Action, TimeSpan, TimeSpan?>((action, harvestCycle, __) => { _harvestAction = action; _harvestCycle = harvestCycle; });
Expand Down Expand Up @@ -1393,6 +1396,8 @@ public void RecordLogMessage_NoTransaction_Success()
.Returns(true);
Mock.Arrange(() => _configurationService.Configuration.ContextDataEnabled)
.Returns(true);
Mock.Arrange(() => _configurationService.Configuration.IncludeLabelsEnabled)
.Returns(true);

var timestamp = DateTime.Now;
var timestampUnix = timestamp.ToUnixTimeMilliseconds();
Expand Down Expand Up @@ -1428,6 +1433,7 @@ public void RecordLogMessage_NoTransaction_Success()
Assert.That(logEvent.SpanId, Is.EqualTo(spanId));
Assert.That(logEvent.TraceId, Is.EqualTo(traceId));
Assert.That(logEvent.ContextData, Is.EqualTo(contextData));
Assert.That(logEvent.Labels, Is.EqualTo(_labelsService.Labels));
});

Mock.Assert(() => _agentHealthReporter.IncrementLogLinesCount(Arg.AnyString), Occurs.Once());
Expand Down Expand Up @@ -1573,6 +1579,8 @@ public void RecordLogMessage_WithTransaction_Success()
.Returns(true);
Mock.Arrange(() => _configurationService.Configuration.ContextDataEnabled)
.Returns(true);
Mock.Arrange(() => _configurationService.Configuration.IncludeLabelsEnabled)
.Returns(true);

var timestamp = DateTime.Now;
var timestampUnix = timestamp.ToUnixTimeMilliseconds();
Expand Down Expand Up @@ -1613,6 +1621,7 @@ public void RecordLogMessage_WithTransaction_Success()
Assert.That(logEvent.TraceId, Is.EqualTo(traceId));
Assert.That(logEvent.ContextData, Is.EqualTo(contextData));
Assert.That(logEvent.Priority, Is.EqualTo(priority));
Assert.That(logEvent.Labels, Is.EqualTo(_labelsService.Labels));
});
}

Expand Down Expand Up @@ -2070,6 +2079,59 @@ public void RecordLogMessage_WithDenyList_DropsMessageAndIncrementsDeniedCount()

}

[Test]
public void RecordLogMessage_WithTransaction_IncludeLabels_Disabled_NoLabels()
{
Mock.Arrange(() => _configurationService.Configuration.LogEventCollectorEnabled)
.Returns(true);
Mock.Arrange(() => _configurationService.Configuration.ContextDataEnabled)
.Returns(true);
Mock.Arrange(() => _configurationService.Configuration.IncludeLabelsEnabled)
.Returns(false);

var timestamp = DateTime.Now;
var timestampUnix = timestamp.ToUnixTimeMilliseconds();
var level = "DEBUG";
var message = "message";
var contextData = new Dictionary<string, object>() { { "key1", "value1" }, { "key2", 1 } };

Func<object, string> getLevelFunc = (l) => level;
Func<object, DateTime> getTimestampFunc = (l) => timestamp;
Func<object, string> getMessageFunc = (l) => message;
Func<object, Exception> getLogExceptionFunc = (l) => null;
Func<object, Dictionary<string, object>> getContextDataFunc = (l) => contextData;

var spanId = "spanid";
var traceId = "traceid";
var loggingFramework = "testFramework";

SetupTransaction();
var transaction = _transactionService.GetCurrentInternalTransaction();
var priority = transaction.Priority;

var xapi = _agent as IAgentExperimental;
xapi.RecordLogMessage(loggingFramework, new object(), getTimestampFunc, getLevelFunc, getMessageFunc, getLogExceptionFunc, getContextDataFunc, spanId, traceId);

var harvestedLogEvents = transaction.HarvestLogEvents();
var logEvent = harvestedLogEvents.FirstOrDefault();
Assert.Multiple(() =>
{
Assert.That(harvestedLogEvents, Has.Count.EqualTo(1));
Assert.That(logEvent, Is.Not.Null);
});
Assert.Multiple(() =>
{
Assert.That(logEvent.TimeStamp, Is.EqualTo(timestampUnix));
Assert.That(logEvent.Level, Is.EqualTo(level));
Assert.That(logEvent.Message, Is.EqualTo(message));
Assert.That(logEvent.SpanId, Is.EqualTo(spanId));
Assert.That(logEvent.TraceId, Is.EqualTo(traceId));
Assert.That(logEvent.ContextData, Is.EqualTo(contextData));
Assert.That(logEvent.Priority, Is.EqualTo(priority));
Assert.That(logEvent.Labels, Is.Null);
});
}

#endregion

#region RecordLlmEvent
Expand Down

0 comments on commit 9bf58a2

Please sign in to comment.