Skip to content

Commit 7abb9ce

Browse files
authored
[Client Telemetry] Enables reporting meta headers for client usage based telemetry (#38)
* enables reporting meta headers for client usage based telemetry * validate meta-header against spec regex * update the name of the describe for node context test
1 parent 2c07a2a commit 7abb9ce

File tree

5 files changed

+90
-1
lines changed

5 files changed

+90
-1
lines changed

.vscode/launch.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Jest tests",
9+
"type": "node",
10+
"request": "launch",
11+
"program": "${workspaceRoot}/node_modules/jest/bin/jest.js",
12+
"stopOnEntry": false,
13+
"args": ["--runInBand", "--forceExit", "--watch"],
14+
"cwd": "${workspaceRoot}",
15+
"preLaunchTask": null,
16+
"runtimeExecutable": null,
17+
"env": {
18+
"NODE_ENV": "test"
19+
},
20+
"console": "integratedTerminal",
21+
"sourceMaps": true
22+
}
23+
]
24+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"precommit": "lint-staged"
1717
},
1818
"jest": {
19-
"testURL": "http://localhost/"
19+
"testURL": "http://localhost/",
20+
"testEnvironment": "jsdom"
2021
},
2122
"lint-staged": {
2223
"*.js": [

src/request.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,14 @@ function _request(
4343
params,
4444
{ additionalHeaders } = {}
4545
) {
46+
const jsVersion = typeof window !== "undefined" ? "browser" : process.version;
47+
const metaHeader = `ent=${version}-legacy,js=${jsVersion},t=${version}-legacy,ft=universal`;
4648
const headers = new Headers({
4749
...(searchKey && { Authorization: `Bearer ${searchKey}` }),
4850
"Content-Type": "application/json",
4951
"X-Swiftype-Client": "elastic-app-search-javascript",
5052
"X-Swiftype-Client-Version": version,
53+
"x-elastic-client-meta": metaHeader,
5154
...additionalHeaders
5255
});
5356

tests/request.node.spec.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @jest-environment node
3+
*/
4+
5+
import { request } from "../src/request";
6+
import { Headers } from "node-fetch";
7+
import { version } from "../package.json";
8+
const nodeVersion = process.version;
9+
10+
describe("request - within node context", () => {
11+
const responseJson = {};
12+
const response = {
13+
json: () => Promise.resolve(responseJson)
14+
};
15+
16+
const searchKey = "api-12345";
17+
const endpoint = "http://www.example.com";
18+
const path = "/v1/search";
19+
const params = {
20+
a: "a"
21+
};
22+
23+
beforeEach(() => {
24+
global.Headers = Headers;
25+
jest.resetAllMocks();
26+
global.fetch = jest
27+
.fn()
28+
.mockImplementation(() => Promise.resolve(response));
29+
});
30+
31+
it("will have the correct node based meta headers when running in node context", async () => {
32+
expect(global.window).not.toBeDefined();
33+
const res = await request(searchKey, endpoint, path, params, false);
34+
expect(res.response).toBe(response);
35+
expect(global.fetch.mock.calls.length).toBe(1);
36+
var [_, options] = global.fetch.mock.calls[0];
37+
expect(options.headers.get("x-elastic-client-meta")).toEqual(
38+
`ent=${version}-legacy,js=${nodeVersion},t=${version}-legacy,ft=universal`
39+
);
40+
const validHeaderRegex = /^[a-z]{1,}=[a-z0-9\.\-]{1,}(?:,[a-z]{1,}=[a-z0-9\.\-]+)*$/;
41+
expect(options.headers.get("x-elastic-client-meta")).toMatch(
42+
validHeaderRegex
43+
);
44+
});
45+
});

tests/request.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { request } from "../src/request";
22
import { Headers } from "node-fetch";
3+
import { version } from "../package.json";
34

45
describe("request", () => {
56
const responseJson = {};
@@ -116,4 +117,19 @@ describe("request", () => {
116117
expect(res.response).toBe(response);
117118
expect(global.fetch.mock.calls.length).toBe(1);
118119
});
120+
121+
it("will have the correct browser based meta headers when running in browser context", async () => {
122+
expect(global.window).toBeDefined();
123+
const res = await request(searchKey, endpoint, path, params, false);
124+
expect(res.response).toBe(response);
125+
expect(global.fetch.mock.calls.length).toBe(1);
126+
var [_, options] = global.fetch.mock.calls[0];
127+
expect(options.headers.get("x-elastic-client-meta")).toEqual(
128+
`ent=${version}-legacy,js=browser,t=${version}-legacy,ft=universal`
129+
);
130+
const validHeaderRegex = /^[a-z]{1,}=[a-z0-9\.\-]{1,}(?:,[a-z]{1,}=[a-z0-9\.\-]+)*$/;
131+
expect(options.headers.get("x-elastic-client-meta")).toMatch(
132+
validHeaderRegex
133+
);
134+
});
119135
});

0 commit comments

Comments
 (0)