|
| 1 | +// Copyright 2020 New Relic, Inc. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using NewRelic.Agent.IntegrationTestHelpers; |
| 8 | +using NewRelic.Agent.IntegrationTestHelpers.RemoteServiceFixtures; |
| 9 | +using Xunit; |
| 10 | +using Xunit.Abstractions; |
| 11 | + |
| 12 | +namespace NewRelic.Agent.IntegrationTests.Api |
| 13 | +{ |
| 14 | + [NetFrameworkTest] |
| 15 | + public class RecordDatastoreSegment_Full_TestsFWLatest : RecordDatastoreSegmentTests<ConsoleDynamicMethodFixtureFWLatest> |
| 16 | + { |
| 17 | + public RecordDatastoreSegment_Full_TestsFWLatest(ConsoleDynamicMethodFixtureFWLatest fixture, ITestOutputHelper output) |
| 18 | + : base(fixture, output, true) |
| 19 | + { |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + [NetFrameworkTest] |
| 24 | + public class RecordDatastoreSegment_RequiredOnly_TestsFWLatest : RecordDatastoreSegmentTests<ConsoleDynamicMethodFixtureFWLatest> |
| 25 | + { |
| 26 | + public RecordDatastoreSegment_RequiredOnly_TestsFWLatest(ConsoleDynamicMethodFixtureFWLatest fixture, ITestOutputHelper output) |
| 27 | + : base(fixture, output, false) |
| 28 | + { |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + [NetCoreTest] |
| 33 | + public class RecordDatastoreSegment_Full_TestsCoreLatest : RecordDatastoreSegmentTests<ConsoleDynamicMethodFixtureCoreLatest> |
| 34 | + { |
| 35 | + public RecordDatastoreSegment_Full_TestsCoreLatest(ConsoleDynamicMethodFixtureCoreLatest fixture, ITestOutputHelper output) |
| 36 | + : base(fixture, output, true) |
| 37 | + { |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + [NetCoreTest] |
| 42 | + public class RecordDatastoreSegment_RequiredOnly_TestsCoreLatest : RecordDatastoreSegmentTests<ConsoleDynamicMethodFixtureCoreLatest> |
| 43 | + { |
| 44 | + public RecordDatastoreSegment_RequiredOnly_TestsCoreLatest(ConsoleDynamicMethodFixtureCoreLatest fixture, ITestOutputHelper output) |
| 45 | + : base(fixture, output, false) |
| 46 | + { |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public abstract class RecordDatastoreSegmentTests<TFixture> : NewRelicIntegrationTest<TFixture> where TFixture : ConsoleDynamicMethodFixture |
| 51 | + { |
| 52 | + protected readonly TFixture _fixture; |
| 53 | + |
| 54 | + private bool _allOptions; |
| 55 | + |
| 56 | + public RecordDatastoreSegmentTests(TFixture fixture, ITestOutputHelper output, bool allOptions) : base(fixture) |
| 57 | + { |
| 58 | + _fixture = fixture; |
| 59 | + _fixture.TestLogger = output; |
| 60 | + _allOptions = allOptions; |
| 61 | + |
| 62 | + if(_allOptions) |
| 63 | + { |
| 64 | + _fixture.AddCommand("ApiCalls TestFullRecordDatastoreSegment"); |
| 65 | + } |
| 66 | + else |
| 67 | + { |
| 68 | + _fixture.AddCommand("ApiCalls TestRequiredRecordDatastoreSegment"); |
| 69 | + } |
| 70 | + |
| 71 | + _fixture.Actions |
| 72 | + ( |
| 73 | + setupConfiguration: () => |
| 74 | + { |
| 75 | + var configModifier = new NewRelicConfigModifier(_fixture.DestinationNewRelicConfigFilePath); |
| 76 | + configModifier.SetOrDeleteDistributedTraceEnabled(true); |
| 77 | + configModifier.SetLogLevel("finest"); |
| 78 | + configModifier.DisableEventListenerSamplers(); // Required for .NET 8 to pass. |
| 79 | + configModifier.ConfigureFasterMetricsHarvestCycle(25); |
| 80 | + configModifier.ConfigureFasterSqlTracesHarvestCycle(30); |
| 81 | + } |
| 82 | + ); |
| 83 | + |
| 84 | + _fixture.AddActions |
| 85 | + ( |
| 86 | + exerciseApplication: () => |
| 87 | + { |
| 88 | + var threadProfileMatch = _fixture.AgentLog.WaitForLogLine(AgentLogFile.SqlTraceDataLogLineRegex, TimeSpan.FromMinutes(1)); |
| 89 | + } |
| 90 | + ); |
| 91 | + |
| 92 | + _fixture.Initialize(); |
| 93 | + } |
| 94 | + |
| 95 | + [Fact] |
| 96 | + public void Test() |
| 97 | + { |
| 98 | + var expectedMetrics = new List<Assertions.ExpectedMetric> |
| 99 | + { |
| 100 | + new Assertions.ExpectedMetric(){ callCount = 1, metricName = "Supportability/ApiInvocation/StartDatastoreSegment" }, |
| 101 | + new Assertions.ExpectedMetric(){ callCount = 1, metricName = "Datastore/statement/Other/MyModel/MyOperation" }, |
| 102 | + new Assertions.ExpectedMetric(){ callCount = 1, metricName = "Datastore/operation/Other/MyOperation" }, |
| 103 | + new Assertions.ExpectedMetric(){ callCount = 1, metricName = "Datastore/all" }, |
| 104 | + new Assertions.ExpectedMetric(){ callCount = 1, metricName = "Datastore/allOther" }, |
| 105 | + new Assertions.ExpectedMetric(){ callCount = 1, metricName = "Datastore/Other/all" }, |
| 106 | + new Assertions.ExpectedMetric(){ callCount = 1, metricName = "Datastore/Other/allOther" }, |
| 107 | + new Assertions.ExpectedMetric(){ callCount = 1, metricName = _allOptions ? "Datastore/instance/Other/MyHost/MyPath" : "Datastore/instance/Other/unknown/unknown" }, |
| 108 | + new Assertions.ExpectedMetric(){ callCount = 1, metricName = "Datastore/statement/Other/MyModel/MyOperation", metricScope = "OtherTransaction/Custom/MultiFunctionApplicationHelpers.Libraries.ApiCalls/RecordDatastoreSegment" }, |
| 109 | + }; |
| 110 | + |
| 111 | + // this will not exist if command text is missing. |
| 112 | + var expectedSqlTraces = new List<Assertions.ExpectedSqlTrace> |
| 113 | + { |
| 114 | + new Assertions.ExpectedSqlTrace() |
| 115 | + { |
| 116 | + Sql = "MyCommandText", |
| 117 | + DatastoreMetricName = "Datastore/statement/Other/MyModel/MyOperation", |
| 118 | + TransactionName = "OtherTransaction/Custom/MultiFunctionApplicationHelpers.Libraries.ApiCalls/RecordDatastoreSegment", |
| 119 | + HasExplainPlan = false |
| 120 | + } |
| 121 | + }; |
| 122 | + |
| 123 | + var actualMetrics = _fixture.AgentLog.GetMetrics().ToList(); |
| 124 | + |
| 125 | + var actualSqlTraces = _fixture.AgentLog.GetSqlTraces().ToList(); //0 |
| 126 | + |
| 127 | + Assertions.MetricsExist(expectedMetrics, actualMetrics); |
| 128 | + |
| 129 | + if (_allOptions) |
| 130 | + { |
| 131 | + Assertions.SqlTraceExists(expectedSqlTraces, actualSqlTraces); |
| 132 | + } |
| 133 | + else // RequiredOnly |
| 134 | + { |
| 135 | + Assert.True(actualSqlTraces.Count == 0); |
| 136 | + } |
| 137 | + |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments