Skip to content

Commit 3565b33

Browse files
authored
DRV-542: Add runtime environment/partner information to headers (#135)
1 parent 0ba4d66 commit 3565b33

File tree

9 files changed

+364
-16
lines changed

9 files changed

+364
-16
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ commands:
4141
dotnet --info
4242
mkdir results
4343
dotnet restore
44-
dotnet test --framework netcoreapp3.0 --logger "junit;LogFilePath=./results/results.xml" FaunaDB.Client.Test
44+
dotnet test --framework netcoreapp3.1 --logger "junit;LogFilePath=./results/results.xml" FaunaDB.Client.Test
4545
4646
- store_test_results:
4747
path: FaunaDB.Client.Test/results/

FaunaDB.Client.Test/ClientTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2101,7 +2101,7 @@ public async Task TestRequestMessageHeaders()
21012101
await faunaClient0.Query(Count(Databases()));
21022102

21032103
var headers = customHttp.LastMessage.Headers;
2104-
Assert.AreEqual("csharp", headers.GetValues("X-Fauna-Driver").First());
2104+
Assert.That(headers.GetValues("X-Driver-Env").First(), Does.Match("driver=csharp-.*; runtime=.*; env=.*; os=.*"));
21052105
Assert.AreEqual("4", headers.GetValues("X-FaunaDB-API-Version").First());
21062106
Assert.IsFalse(headers.Contains("X-Last-Seen-Txn"));
21072107

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
using System.Collections.Generic;
2+
using FaunaDB.Client;
3+
using NUnit.Framework;
4+
5+
namespace Test
6+
{
7+
[TestFixture]
8+
public class EnvironmentHeaderTest
9+
{
10+
private IEnvironmentEditor environmentEditor;
11+
12+
[OneTimeSetUp]
13+
public void SetUp()
14+
{
15+
environmentEditor = new EnvironmentEditorMock();
16+
}
17+
18+
[SetUp]
19+
public void DestroyRuntimeEnvironmentHeaderInstance()
20+
{
21+
RuntimeEnvironmentHeader.Destroy();
22+
}
23+
24+
[Test]
25+
public void TestRuntimeEnvironmentHeaderFormat()
26+
{
27+
string actual = RuntimeEnvironmentHeader.Construct(environmentEditor);
28+
Assert.That(actual, Does.Match("driver=csharp-.*; runtime=.*; env=.*; os=.*"));
29+
}
30+
31+
[Test]
32+
public void TestNetlifyEnvironment()
33+
{
34+
environmentEditor.SetVariable("NETLIFY_IMAGES_CDN_DOMAIN", "some_value");
35+
var actual = RuntimeEnvironmentHeader.Construct(environmentEditor);
36+
Assert.That(actual, Does.Contain("Netlify"));
37+
}
38+
39+
[Test]
40+
public void TestVercelEnvironment()
41+
{
42+
environmentEditor.SetVariable("VERCEL", "some_value");
43+
var actual = RuntimeEnvironmentHeader.Construct(environmentEditor);
44+
Assert.That(actual, Does.Contain("Vercel"));
45+
}
46+
47+
[Test]
48+
public void TestHerokuEnvironment()
49+
{
50+
environmentEditor.SetVariable("PATH", "heroku");
51+
var actual = RuntimeEnvironmentHeader.Construct(environmentEditor);
52+
Assert.That(actual, Does.Contain("Heroku"));
53+
}
54+
55+
[Test]
56+
public void TestUnknownEnvironmentWithPathVariable()
57+
{
58+
environmentEditor.SetVariable("PATH", "some_value");
59+
var actual = RuntimeEnvironmentHeader.Construct(environmentEditor);
60+
Assert.That(actual, Does.Contain("Unknown"));
61+
}
62+
63+
[Test]
64+
public void TestAwsLambdaEnvironment()
65+
{
66+
environmentEditor.SetVariable("AWS_LAMBDA_FUNCTION_VERSION", "some_value");
67+
var actual = RuntimeEnvironmentHeader.Construct(environmentEditor);
68+
Assert.That(actual, Does.Contain("AWS Lambda"));
69+
}
70+
71+
[Test]
72+
public void TestGoogleFunctionsEnvironment()
73+
{
74+
environmentEditor.SetVariable("_", "google");
75+
var actual = RuntimeEnvironmentHeader.Construct(environmentEditor);
76+
Assert.That(actual, Does.Contain("GCP Cloud Functions"));
77+
RuntimeEnvironmentHeader.Destroy();
78+
}
79+
80+
[Test]
81+
public void TestUnknownEnvironmentWithUnderscoreVariable()
82+
{
83+
environmentEditor.SetVariable("_", "some_value");
84+
var actual = RuntimeEnvironmentHeader.Construct(environmentEditor);
85+
Assert.That(actual, Does.Contain("Unknown"));
86+
}
87+
88+
[Test]
89+
public void TestGoogleCloudEnvironment()
90+
{
91+
environmentEditor.SetVariable("GOOGLE_CLOUD_PROJECT", "some_value");
92+
var actual = RuntimeEnvironmentHeader.Construct(environmentEditor);
93+
Assert.That(actual, Does.Contain("GCP Compute Instances"));
94+
}
95+
96+
[Test]
97+
public void TestAzureEnvironment()
98+
{
99+
environmentEditor.SetVariable("ORYX_ENV_TYPE", "AppService");
100+
environmentEditor.SetVariable("WEBSITE_INSTANCE_ID", "some_value");
101+
var actual = RuntimeEnvironmentHeader.Construct(environmentEditor);
102+
Assert.That(actual, Does.Contain("Azure Compute"));
103+
}
104+
105+
[Test]
106+
public void TestUnknownEnvironmentWithOryx()
107+
{
108+
environmentEditor.SetVariable("ORYX_ENV_TYPE", "some_value");
109+
environmentEditor.SetVariable("WEBSITE_INSTANCE_ID", "some_value");
110+
var actual = RuntimeEnvironmentHeader.Construct(environmentEditor);
111+
Assert.That(actual, Does.Contain("Unknown"));
112+
}
113+
114+
[Test]
115+
public void TestUnknownEnvironmentWithOryxWithoutWebsiteInstanceId()
116+
{
117+
environmentEditor.SetVariable("ORYX_ENV_TYPE", "AppService");
118+
var actual = RuntimeEnvironmentHeader.Construct(environmentEditor);
119+
Assert.That(actual, Does.Contain("Unknown"));
120+
}
121+
122+
[TearDown]
123+
public void RemoveEnvironmentVariables()
124+
{
125+
environmentEditor.RemoveVariable("NETLIFY_IMAGES_CDN_DOMAIN");
126+
environmentEditor.RemoveVariable("VERCEL");
127+
environmentEditor.RemoveVariable("PATH");
128+
environmentEditor.RemoveVariable("AWS_LAMBDA_FUNCTION_VERSION");
129+
environmentEditor.RemoveVariable("_");
130+
environmentEditor.RemoveVariable("GOOGLE_CLOUD_PROJECT");
131+
environmentEditor.RemoveVariable("ORYX_ENV_TYPE");
132+
environmentEditor.RemoveVariable("WEBSITE_INSTANCE_ID");
133+
}
134+
}
135+
136+
class EnvironmentEditorMock : IEnvironmentEditor
137+
{
138+
private Dictionary<string, string> mockEnvironment;
139+
140+
public EnvironmentEditorMock()
141+
{
142+
mockEnvironment = new Dictionary<string, string>();
143+
}
144+
145+
public string GetVariable(string variableName)
146+
{
147+
if (!mockEnvironment.ContainsKey(variableName)) return null;
148+
return mockEnvironment[variableName];
149+
}
150+
151+
public void SetVariable(string variableName, string variableValue)
152+
{
153+
mockEnvironment[variableName] = variableValue;
154+
}
155+
156+
public void RemoveVariable(string variableName)
157+
{
158+
mockEnvironment.Remove(variableName);
159+
}
160+
}
161+
}

FaunaDB.Client.Test/FaunaDB.Client.Test.csproj

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
https://docs.microsoft.com/en-us/dotnet/standard/net-standard#net-implementation-support
66
https://github.com/dotnet/standard/blob/master/docs/versions.md
77
8-
netcoreapp1.0 => netstandard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6
9-
netcoreapp1.1 => netstandard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6
10-
netcoreapp2.0 => netstandard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0
11-
net45 => netstandard 1.0, 1.1
12-
net451 => netstandard 1.0, 1.1, 1.2
13-
net46 => netstandard 1.0, 1.1, 1.2, 1.3
14-
net461 => netstandard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0
8+
netcoreapp1.1 => netstandard 1.5 FuanaDB.Client build will be used
9+
netcoreapp2.1 => netstandard 2.0 FuanaDB.Client build will be used
10+
netcoreapp3.1 => netstandard 2.1 FuanaDB.Client build will be used
11+
net5.0 => netstandard 2.1 FuanaDB.Client build will be used (should be changed to 6.0 after the release)
12+
net45 => applicable for .net framework version up to 4.8, means if you target
13+
.net frameworks net45 till net48, FaunaDB.Client net45 build will be used
1514
-->
16-
<TargetFrameworks>net45;netcoreapp1.0;netcoreapp1.1;netcoreapp2.0;netcoreapp3.0;net451;net46;net461;net47</TargetFrameworks>
15+
<TargetFrameworks>net45;netcoreapp1.1;netcoreapp2.1;netcoreapp3.1;net5.0</TargetFrameworks>
1716
</PropertyGroup>
1817

1918
<ItemGroup>

FaunaDB.Client.Test/TestCase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ async Task SetUpAsync()
4949
var port = Env("FAUNA_PORT", "8443");
5050

5151
faunaSecret = Env("FAUNA_ROOT_KEY", "secret");
52-
faunaEndpoint = $"{scheme}://{domain}:{port}/";
52+
faunaEndpoint = port != "443" ? $"{scheme}://{domain}:{port}/" : $"{scheme}://{domain}/";
5353
rootClient = new FaunaClient(secret: faunaSecret, endpoint: faunaEndpoint);
5454

5555
DbRef = Database(testDbName);

FaunaDB.Client/Client/DefaultClientIO.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using System.Net;
77
using System.Net.Http;
88
using System.Net.Http.Headers;
9+
using System.Runtime.InteropServices;
10+
using System.Reflection;
911
using System.Text;
1012
using System.Threading;
1113
using System.Threading.Tasks;
@@ -66,7 +68,7 @@ async Task<RequestResult> DoRequestAsync(HttpMethodKind method, string path, str
6668
message.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
6769
message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
6870
message.Headers.Add("X-FaunaDB-API-Version", "4");
69-
message.Headers.Add("X-Fauna-Driver", "csharp");
71+
message.Headers.Add("X-Driver-Env", RuntimeEnvironmentHeader.Construct(EnvironmentEditor.Create()));
7072

7173
var last = lastSeen.Txn;
7274
if (last.HasValue)
@@ -116,7 +118,7 @@ async Task<StreamingRequestResult> DoStreamingRequestAsync(string data, IReadOnl
116118
message.Content = dataString;
117119
message.Headers.Authorization = authHeader;
118120
message.Headers.Add("X-FaunaDB-API-Version", "4");
119-
message.Headers.Add("X-Fauna-Driver", "csharp");
121+
message.Headers.Add("X-Driver-Env", RuntimeEnvironmentHeader.Construct(EnvironmentEditor.Create()));
120122

121123
var last = lastSeen.Txn;
122124
if (last.HasValue)

0 commit comments

Comments
 (0)