Skip to content

Commit 47c1187

Browse files
authored
chore: drop dependencies on axios and form-data (#112)
1 parent 039ca9c commit 47c1187

File tree

3 files changed

+30
-64
lines changed

3 files changed

+30
-64
lines changed

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@
4343
},
4444
"dependencies": {
4545
"@datadog/pprof": "^5.4.1",
46-
"axios": ">=0.29.0 <1.x",
4746
"debug": "^4.3.3",
48-
"form-data": "^4.0.0",
4947
"p-limit": "^3.1.0",
5048
"regenerator-runtime": "^0.13.11",
5149
"source-map": "^0.7.3"

src/pyroscope-api-exporter.ts

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { URL } from 'node:url';
22

33
import { encode } from '@datadog/pprof';
4-
import axios, { AxiosBasicCredentials, AxiosError } from 'axios';
5-
import FormData, { Headers } from 'form-data';
64
import { Profile } from 'pprof-format';
75
import { ProfileExport, ProfileExporter } from './profile-exporter.js';
86
import { dateToUnixTimestamp } from './utils/date-to-unix-timestamp.js';
@@ -58,15 +56,22 @@ export class PyroscopeApiExporter implements ProfileExporter {
5856
return endpointUrl;
5957
}
6058

61-
private buildRequestHeaders(formData: FormData): Headers {
62-
const headers: Headers = formData.getHeaders();
59+
private buildRequestHeaders(): Headers {
60+
const headers: Headers = new Headers();
6361

6462
if (this.authToken !== undefined) {
65-
headers['authorization'] = `Bearer ${this.authToken}`;
63+
headers.set('authorization', `Bearer ${this.authToken}`);
64+
} else if (this.config.basicAuthUser && this.config.basicAuthPassword) {
65+
headers.set(
66+
'authorization',
67+
Buffer.from(
68+
`${this.config.basicAuthUser}:${this.config.basicAuthPassword}`
69+
).toString('base64')
70+
);
6671
}
6772

6873
if (this.config.tenantID) {
69-
headers['X-Scope-OrgID'] = this.config.tenantID;
74+
headers.set('X-Scope-OrgID', this.config.tenantID);
7075
}
7176

7277
return headers;
@@ -81,54 +86,36 @@ export class PyroscopeApiExporter implements ProfileExporter {
8186

8287
const formData: FormData = new FormData();
8388

84-
formData.append('profile', profileBuffer, {
85-
contentType: 'text/json',
86-
filename: 'profile',
87-
knownLength: profileBuffer.byteLength,
88-
});
89+
formData.append('profile', new Blob([profileBuffer]), 'profile');
8990

9091
return formData;
9192
}
9293

93-
private handleAxiosError(error: AxiosError): void {
94-
if (error.response !== undefined) {
95-
// The request was made and the server responded with a status code
96-
// that falls out of the range of 2xx
97-
log('Pyroscope received error while ingesting data to server');
98-
log(error.response.data);
99-
} else if (error.request !== undefined) {
100-
// The request was made but no response was received
101-
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
102-
// http.ClientRequest in node.js
103-
log('Error when ingesting data to server:', error.message);
104-
} else {
105-
// Something happened in setting up the request that triggered an Error
106-
log('Error', error.message);
107-
}
108-
}
109-
11094
private async uploadProfile(profileExport: ProfileExport): Promise<void> {
11195
const formData: FormData = await this.buildUploadProfileFormData(
11296
profileExport.profile
11397
);
11498

115-
const auth: AxiosBasicCredentials | undefined =
116-
this.config.basicAuthUser && this.config.basicAuthPassword
117-
? {
118-
username: this.config.basicAuthUser,
119-
password: this.config.basicAuthPassword,
120-
}
121-
: undefined;
122-
12399
try {
124-
await axios(this.buildEndpointUrl(profileExport).toString(), {
125-
data: formData,
126-
headers: this.buildRequestHeaders(formData),
127-
method: 'POST',
128-
auth: auth,
129-
});
100+
const response = await fetch(
101+
this.buildEndpointUrl(profileExport).toString(),
102+
{
103+
body: formData,
104+
headers: this.buildRequestHeaders(),
105+
method: 'POST',
106+
}
107+
);
108+
109+
if (!response.ok) {
110+
log('Server rejected data ingest: HTTP %d', response.status);
111+
log(await response.text());
112+
}
130113
} catch (error: unknown) {
131-
this.handleAxiosError(error as AxiosError);
114+
if (error instanceof Error) {
115+
log('Error sending data ingest: %s', error.message);
116+
} else {
117+
log('Unknown error sending data ingest');
118+
}
132119
}
133120
}
134121
}

yarn.lock

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -748,15 +748,6 @@ asynckit@^0.4.0:
748748
resolved "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz"
749749
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==
750750

751-
"axios@>=0.29.0 <1.x":
752-
version "0.29.0"
753-
resolved "https://registry.npmjs.org/axios/-/axios-0.29.0.tgz#5eed1a0bc4c0ffe060624eb7900aff66b7881eeb"
754-
integrity sha512-Kjsq1xisgO5DjjNQwZFsy0gpcU1P2j36dZeQDXVhpIU26GVgkDUnROaHLSuluhMqtDE7aKA2hbKXG5yu5DN8Tg==
755-
dependencies:
756-
follow-redirects "^1.15.4"
757-
form-data "^4.0.0"
758-
proxy-from-env "^1.1.0"
759-
760751
balanced-match@^1.0.0:
761752
version "1.0.2"
762753
resolved "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz"
@@ -1319,11 +1310,6 @@ flatted@^3.2.9:
13191310
resolved "https://registry.npmjs.org/flatted/-/flatted-3.2.9.tgz"
13201311
integrity sha512-36yxDn5H7OFZQla0/jFJmbIKTdZAQHngCedGxiMmpNfEZM0sdEeT+WczLQrjK6D7o2aiyLYDnkw0R3JK0Qv1RQ==
13211312

1322-
follow-redirects@^1.15.4:
1323-
version "1.15.9"
1324-
resolved "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.9.tgz#a604fa10e443bf98ca94228d9eebcc2e8a2c8ee1"
1325-
integrity sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==
1326-
13271313
foreground-child@^3.1.0:
13281314
version "3.3.0"
13291315
resolved "https://registry.yarnpkg.com/foreground-child/-/foreground-child-3.3.0.tgz#0ac8644c06e431439f8561db8ecf29a7b5519c77"
@@ -2030,11 +2016,6 @@ proxy-addr@~2.0.7:
20302016
forwarded "0.2.0"
20312017
ipaddr.js "1.9.1"
20322018

2033-
proxy-from-env@^1.1.0:
2034-
version "1.1.0"
2035-
resolved "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz"
2036-
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==
2037-
20382019
punycode@^2.1.0:
20392020
version "2.3.1"
20402021
resolved "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz"

0 commit comments

Comments
 (0)