-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphQLTests.cs
222 lines (181 loc) · 7.73 KB
/
GraphQLTests.cs
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#if NET
using System.Net;
using System.Text;
using IntegrationTests.Helpers;
using OpenTelemetry.Proto.Trace.V1;
using Xunit.Abstractions;
namespace IntegrationTests;
public class GraphQLTests : TestHelper
{
public GraphQLTests(ITestOutputHelper output)
: base("GraphQL", output)
{
}
public static TheoryData<string, bool> GetData()
{
var theoryData = new TheoryData<string, bool>();
foreach (var version in LibraryVersion.GraphQL)
{
theoryData.Add(version, true);
theoryData.Add(version, false);
}
return theoryData;
}
[Theory]
[Trait("Category", "EndToEnd")]
[MemberData(nameof(GetData))]
public async Task SubmitsTraces(string packageVersion, bool setDocument)
{
var requests = new List<RequestInfo>();
using var collector = new MockSpansCollector(Output);
SetExporter(collector);
// SUCCESS: query using GET
Request(requests, id: 1, method: "GET", url: "/graphql?query=" + WebUtility.UrlEncode("query{hero{name appearsIn}}"));
Expect(collector, id: 1, spanName: "query", setDocument: setDocument);
// SUCCESS: query using POST (default)
Request(requests, id: 2, body: @"{""query"":""query HeroQuery{hero{name appearsIn}}"",""operationName"": ""HeroQuery""}");
Expect(collector, id: 2, spanName: "query HeroQuery", setDocument: setDocument);
// SUCCESS: mutation
Request(requests, id: 3, body: @"{""query"":""mutation AddBobaFett($human:HumanInput!){createHuman(human: $human){id name}}"",""variables"":{""human"":{""name"": ""Boba Fett""}}}");
Expect(collector, id: 3, spanName: "mutation AddBobaFett", setDocument: setDocument);
// SUCCESS: subscription
Request(requests, id: 4, body: @"{ ""query"":""subscription HumanAddedSub{humanAdded{name}}""}");
Expect(collector, id: 4, spanName: "subscription HumanAddedSub", setDocument: setDocument);
// TODO: re-enable if exceptions are supported again.
// FAILURE: query fails 'execute' step
// Request(requests, id: 5, body: @"{""query"":""subscription NotImplementedSub{throwNotImplementedException{name}}""}");
// Expect(collector, id: 5, spanName: "subscription NotImplementedSub", setDocument: setDocument, verifyFailure: VerifyNotImplementedException);
SetEnvironmentVariable("OTEL_DOTNET_AUTO_GRAPHQL_SET_DOCUMENT", setDocument.ToString());
SetEnvironmentVariable("OTEL_DOTNET_AUTO_TRACES_INSTRUMENTATION_ENABLED", "false");
SetEnvironmentVariable("OTEL_DOTNET_AUTO_METRICS_INSTRUMENTATION_ENABLED", "false"); // disable metrics to disable side effect of AspNetCore - working propagation on .NET
SetEnvironmentVariable("OTEL_DOTNET_AUTO_TRACES_GRAPHQL_INSTRUMENTATION_ENABLED", "true");
SetEnvironmentVariable("OTEL_DOTNET_AUTO_TRACES_ASPNETCORE_INSTRUMENTATION_ENABLED", "true"); // AspNetCore Instrumentation enables propagation used in this test
SetEnvironmentVariable("OTEL_TRACES_SAMPLER", "always_on");
SetEnvironmentVariable("OTEL_DOTNET_AUTO_NETFX_REDIRECT_ENABLED", "false");
int aspNetCorePort = TcpPortProvider.GetOpenPort();
SetEnvironmentVariable("ASPNETCORE_URLS", $"http://127.0.0.1:{aspNetCorePort}/");
using var process = StartTestApplication(new TestSettings { PackageVersion = packageVersion });
using var helper = new ProcessHelper(process);
try
{
await HealthzHelper.TestAsync($"http://localhost:{aspNetCorePort}/alive-check", Output);
await SubmitRequestsAsync(aspNetCorePort, requests);
collector.AssertExpectations();
}
finally
{
if (process != null && !process.HasExited)
{
process.Kill();
process.WaitForExit();
Output.WriteLine("Exit Code: " + process.ExitCode);
}
Output.WriteResult(helper);
}
}
private static void Request(List<RequestInfo> requests, byte id, string method = "POST", string url = "/graphql", string? body = null)
{
requests.Add(new RequestInfo
{
Id = GetTraceIdHex(id),
Url = url,
HttpMethod = method,
RequestBody = body
});
}
private static byte[] GetTraceIdBytes(byte id)
{
var traceId = new byte[16];
traceId[^1] = id;
return traceId;
}
private static string GetTraceIdHex(byte id)
{
return id.ToString("x32");
}
private void Expect(
MockSpansCollector collector,
string spanName,
byte id,
bool setDocument)
{
var traceIdBytes = GetTraceIdBytes(id);
bool Predicate(Span span)
{
var traceId = span.TraceId.ToByteArray();
if (!traceId.SequenceEqual(traceIdBytes))
{
return false;
}
if (setDocument && !span.Attributes.Any(attr => attr.Key == "graphql.document" && !string.IsNullOrWhiteSpace(attr.Value?.StringValue)))
{
return false;
}
if (!setDocument && span.Attributes.Any(attr => attr.Key == "graphql.document"))
{
return false;
}
return true;
}
collector.Expect("GraphQL", Predicate, spanName);
}
private async Task SubmitRequestsAsync(int aspNetCorePort, IEnumerable<RequestInfo> requests)
{
var client = new HttpClient();
foreach (var requestInfo in requests)
{
await SubmitRequestAsync(client, aspNetCorePort, requestInfo);
}
}
private async Task SubmitRequestAsync(HttpClient client, int aspNetCorePort, RequestInfo requestInfo, bool printResponseText = true)
{
try
{
var url = $"http://localhost:{aspNetCorePort}{requestInfo.Url}";
var method = requestInfo.HttpMethod;
var w3c = $"00-{requestInfo.Id}-0000000000000001-01";
HttpResponseMessage response;
if (method == "GET")
{
using var requestMessage = new HttpRequestMessage(HttpMethod.Get, url);
requestMessage.Headers.Add("traceparent", w3c);
response = await client.SendAsync(requestMessage);
}
else if (method == "POST")
{
if (requestInfo.RequestBody == null)
{
throw new NotSupportedException("RequestBody cannot be null when you are using POST method");
}
using var requestMessage = new HttpRequestMessage(HttpMethod.Post, url);
requestMessage.Content = new StringContent(requestInfo.RequestBody, Encoding.UTF8, "application/json");
requestMessage.Headers.Add("traceparent", w3c);
response = await client.SendAsync(requestMessage);
}
else
{
// If additional logic is needed, implement it here.
throw new NotImplementedException($"{method} is not supported.");
}
if (printResponseText)
{
var content = await response.Content.ReadAsStringAsync();
Output.WriteLine($"[http] {response.StatusCode} {content}");
}
}
catch (HttpRequestException ex)
{
Output.WriteLine($"[http] exception: {ex}");
}
}
private class RequestInfo
{
public string? Url { get; set; }
public string? HttpMethod { get; set; }
public string? RequestBody { get; set; }
public string? Id { get; set; }
}
}
#endif