-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathNpgsqlCommandTests.cs
More file actions
132 lines (116 loc) · 6.06 KB
/
Copy pathNpgsqlCommandTests.cs
File metadata and controls
132 lines (116 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// <copyright file="NpgsqlCommandTests.cs" company="Datadog">
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Datadog.Trace.ClrProfiler.IntegrationTests.Helpers;
using Datadog.Trace.Configuration;
using Datadog.Trace.TestHelpers;
using Datadog.Trace.TestHelpers.AutoInstrumentation.Containers;
using FluentAssertions;
using VerifyXunit;
using Xunit;
using Xunit.Abstractions;
namespace Datadog.Trace.ClrProfiler.IntegrationTests.AdoNet
{
[Trait("RequiresDockerDependency", "true")]
[Trait("DockerGroup", "1")]
[UsesVerify]
[Collection(PostgresCollection.Name)]
public class NpgsqlCommandTests : TracingIntegrationTest
{
private readonly PostgresFixture _postgresFixture;
public NpgsqlCommandTests(ITestOutputHelper output, PostgresFixture postgresFixture)
: base("Npgsql", output)
{
_postgresFixture = postgresFixture;
SetServiceVersion("1.0.0");
ConfigureContainers(postgresFixture);
}
public override Result ValidateIntegrationSpan(MockSpan span, string metadataSchemaVersion) => span.IsNpgsql(metadataSchemaVersion);
[SkippableTheory]
[CombinatorialOrPairwiseData]
[Trait("Category", "EndToEnd")]
public async Task SubmitsTraces(
[PackageVersionData(nameof(PackageVersions.Npgsql))] string packageVersion,
[MetadataSchemaVersionData] string metadataSchemaVersion,
[DbmPropagationModesData] string dbmPropagation)
{
SetEnvironmentVariable("DD_DBM_PROPAGATION_MODE", dbmPropagation);
// ALWAYS: 77 spans
// - NpgsqlCommand: 21 spans (3 groups * 7 spans)
// - DbCommand: 42 spans (6 groups * 7 spans)
// - IDbCommand: 14 spans (2 groups * 7 spans)
//
// NETSTANDARD: +56 spans
// - DbCommand-netstandard: 42 spans (6 groups * 7 spans)
// - IDbCommand-netstandard: 14 spans (2 groups * 7 spans)
//
// CALLTARGET: +7 spans
// - IDbCommandGenericConstrant<NpgsqlCommand>: 7 spans (1 group * 7 spans)
//
// NETSTANDARD + CALLTARGET: +7 spans
// - IDbCommandGenericConstrant<NpgsqlCommand>-netstandard: 7 spans (1 group * 7 spans)
const int expectedSpanCount = 147;
const string dbType = "postgres";
const string expectedOperationName = dbType + ".query";
SetEnvironmentVariable("DD_TRACE_SPAN_ATTRIBUTE_SCHEMA", metadataSchemaVersion);
var isExternalSpan = metadataSchemaVersion == "v0";
var clientSpanServiceName = isExternalSpan ? $"{EnvironmentHelper.FullSampleName}-{dbType}" : EnvironmentHelper.FullSampleName;
using var telemetry = this.ConfigureTelemetry();
using var agent = EnvironmentHelper.GetMockAgent();
using var process = await RunSampleAndWaitForExit(agent, packageVersion: packageVersion);
var spans = await agent.WaitForSpansAsync(expectedSpanCount, operationName: expectedOperationName);
int actualSpanCount = spans.Count(s => s.ParentId.HasValue); // Remove unexpected DB spans from the calculation
var filteredSpans = spans.Where(s => s.ParentId.HasValue).ToList();
// Assert an exact match once we can correctly instrument the generic constraint case
actualSpanCount.Should().Be(expectedSpanCount);
ValidateIntegrationSpans(spans, metadataSchemaVersion, expectedServiceName: clientSpanServiceName, isExternalSpan);
await telemetry.AssertIntegrationEnabledAsync(IntegrationId.Npgsql);
var settings = VerifyHelper.GetSpanVerifierSettings();
settings.AddRegexScrubber(new Regex("Npgsql-Test-[a-zA-Z0-9]{32}"), "Npgsql-Test-GUID");
settings.AddSimpleScrubber($"out.host: {_postgresFixture.Host}", "out.host: postgres");
settings.AddSimpleScrubber("out.host: localhost", "out.host: postgres");
settings.AddSimpleScrubber("out.host: postgres_arm64", "out.host: postgres");
var fileName = nameof(NpgsqlCommandTests);
#if NETFRAMEWORK
fileName = fileName + ".Net462";
#endif
fileName = fileName + (dbmPropagation switch
{
"full" => ".tagged",
_ => ".untagged",
});
await VerifyHelper.VerifySpans(filteredSpans, settings)
.DisableRequireUniquePrefix()
.UseFileName($"{fileName}.Schema{metadataSchemaVersion.ToUpper()}");
}
[SkippableFact]
[Trait("Category", "EndToEnd")]
public async Task IntegrationDisabled()
{
const int totalSpanCount = 21;
const string expectedOperationName = "postgres.query";
SetEnvironmentVariable($"DD_TRACE_{nameof(IntegrationId.Npgsql)}_ENABLED", "false");
string packageVersion = PackageVersions.Npgsql.First()[0] as string;
using var telemetry = this.ConfigureTelemetry();
using var agent = EnvironmentHelper.GetMockAgent();
try
{
using var process = await RunSampleAndWaitForExit(agent, packageVersion: packageVersion);
var spans = await agent.WaitForSpansAsync(totalSpanCount, returnAllOperations: true);
Assert.NotEmpty(spans);
spans.Where(s => s.Name.Equals(expectedOperationName)).Should().BeEmpty();
await telemetry.AssertIntegrationDisabledAsync(IntegrationId.Npgsql);
}
catch (ExitCodeException ex) when (ex.Message.Contains("Connection timed out"))
{
throw new SkipException("The PostgreSQL test dependency timed out.");
}
}
}
}