Skip to content

Commit

Permalink
refactor(otlp-exporter-base): don't create blob before sending xhr (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pichlermarc authored Nov 25, 2024
1 parent ef80821 commit 1e312c2
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 80 deletions.
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ All notable changes to experimental packages in this project will be documented
### :rocket: (Enhancement)

* feat(otlp-exporter-base): internally accept a http header provider function only [#5179](https://github.com/open-telemetry/opentelemetry-js/pull/5179) @pichlermarc
* refactor(otlp-exporter-base): don't create blob before sending xhr [#5193](https://github.com/open-telemetry/opentelemetry-js/pull/5193) @pichlermarc
* improves compatibility with some unsupported runtimes

### :bug: (Bug Fix)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,9 @@ describe('OTLPTraceExporter - web', () => {
assert.strictEqual(request.method, 'POST');
assert.strictEqual(request.url, 'http://foo.bar.com');

const body = request.requestBody as Blob;
const decoder = new TextDecoder();
const body = request.requestBody as Uint8Array;
const json = JSON.parse(
decoder.decode(await body.arrayBuffer())
new TextDecoder().decode(body)
) as IExportTraceServiceRequest;
const span1 = json.resourceSpans?.[0].scopeSpans?.[0].spans?.[0];

Expand Down Expand Up @@ -340,14 +339,18 @@ describe('OTLPTraceExporter - web', () => {
collectorTraceExporter.export(spans, () => {});

queueMicrotask(() => {
const [{ requestHeaders }] = server.requests;

ensureHeadersContain(requestHeaders, customHeaders);
assert.strictEqual(stubBeacon.callCount, 0);
assert.strictEqual(stubOpen.callCount, 0);
try {
const [{ requestHeaders: requestHeaders }] = server.requests;

clock.restore();
done();
ensureHeadersContain(requestHeaders, customHeaders);
assert.strictEqual(stubBeacon.callCount, 0);
assert.strictEqual(stubOpen.callCount, 0);
done();
} catch (e) {
done(e);
} finally {
clock.restore();
}
});
});
it('should log the timeout request error message', done => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,77 +240,84 @@ describe('OTLPMetricExporter - web', () => {
collectorExporter.export(metrics, () => {});

queueMicrotask(async () => {
const request = server.requests[0];
assert.strictEqual(request.method, 'POST');
assert.strictEqual(request.url, 'http://foo.bar.com');

const body = request.requestBody;
const decoder = new TextDecoder();
const json = JSON.parse(
decoder.decode(await body.arrayBuffer())
) as IExportMetricsServiceRequest;
// The order of the metrics is not guaranteed.
const counterIndex = metrics.scopeMetrics[0].metrics.findIndex(
it => it.descriptor.name === 'int-counter'
);
const observableIndex = metrics.scopeMetrics[0].metrics.findIndex(
it => it.descriptor.name === 'double-observable-gauge2'
);
const histogramIndex = metrics.scopeMetrics[0].metrics.findIndex(
it => it.descriptor.name === 'int-histogram'
);

const metric1 =
json.resourceMetrics[0].scopeMetrics[0].metrics[counterIndex];
const metric2 =
json.resourceMetrics[0].scopeMetrics[0].metrics[observableIndex];
const metric3 =
json.resourceMetrics[0].scopeMetrics[0].metrics[histogramIndex];
try {
const request = server.requests[0];
assert.strictEqual(request.method, 'POST');
assert.strictEqual(request.url, 'http://foo.bar.com');

const body = request.requestBody;
const json = JSON.parse(
new TextDecoder().decode(body)
) as IExportMetricsServiceRequest;
// The order of the metrics is not guaranteed.
const counterIndex = metrics.scopeMetrics[0].metrics.findIndex(
it => it.descriptor.name === 'int-counter'
);
const observableIndex = metrics.scopeMetrics[0].metrics.findIndex(
it => it.descriptor.name === 'double-observable-gauge2'
);
const histogramIndex = metrics.scopeMetrics[0].metrics.findIndex(
it => it.descriptor.name === 'int-histogram'
);

assert.ok(typeof metric1 !== 'undefined', "metric doesn't exist");
ensureCounterIsCorrect(
metric1,
metrics.scopeMetrics[0].metrics[counterIndex].dataPoints[0].endTime,
metrics.scopeMetrics[0].metrics[counterIndex].dataPoints[0]
.startTime
);
const metric1 =
json.resourceMetrics[0].scopeMetrics[0].metrics[counterIndex];
const metric2 =
json.resourceMetrics[0].scopeMetrics[0].metrics[observableIndex];
const metric3 =
json.resourceMetrics[0].scopeMetrics[0].metrics[histogramIndex];

assert.ok(typeof metric1 !== 'undefined', "metric doesn't exist");
ensureCounterIsCorrect(
metric1,
metrics.scopeMetrics[0].metrics[counterIndex].dataPoints[0]
.endTime,
metrics.scopeMetrics[0].metrics[counterIndex].dataPoints[0]
.startTime
);

assert.ok(
typeof metric2 !== 'undefined',
"second metric doesn't exist"
);
ensureObservableGaugeIsCorrect(
metric2,
metrics.scopeMetrics[0].metrics[observableIndex].dataPoints[0]
.endTime,
metrics.scopeMetrics[0].metrics[observableIndex].dataPoints[0]
.startTime,
6,
'double-observable-gauge2'
);
assert.ok(
typeof metric2 !== 'undefined',
"second metric doesn't exist"
);
ensureObservableGaugeIsCorrect(
metric2,
metrics.scopeMetrics[0].metrics[observableIndex].dataPoints[0]
.endTime,
metrics.scopeMetrics[0].metrics[observableIndex].dataPoints[0]
.startTime,
6,
'double-observable-gauge2'
);

assert.ok(
typeof metric3 !== 'undefined',
"third metric doesn't exist"
);
ensureHistogramIsCorrect(
metric3,
metrics.scopeMetrics[0].metrics[histogramIndex].dataPoints[0]
.endTime,
metrics.scopeMetrics[0].metrics[histogramIndex].dataPoints[0]
.startTime,
[0, 100],
[0, 2, 0]
);
assert.ok(
typeof metric3 !== 'undefined',
"third metric doesn't exist"
);
ensureHistogramIsCorrect(
metric3,
metrics.scopeMetrics[0].metrics[histogramIndex].dataPoints[0]
.endTime,
metrics.scopeMetrics[0].metrics[histogramIndex].dataPoints[0]
.startTime,
[0, 100],
[0, 2, 0]
);

const resource = json.resourceMetrics[0].resource;
assert.ok(typeof resource !== 'undefined', "resource doesn't exist");
ensureWebResourceIsCorrect(resource);
const resource = json.resourceMetrics[0].resource;
assert.ok(
typeof resource !== 'undefined',
"resource doesn't exist"
);
ensureWebResourceIsCorrect(resource);

assert.strictEqual(stubBeacon.callCount, 0);
ensureExportMetricsServiceRequestIsSet(json);
assert.strictEqual(stubBeacon.callCount, 0);
ensureExportMetricsServiceRequestIsSet(json);

done();
done();
} catch (e) {
done(e);
}
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class XhrTransport implements IExporterTransport {
});
};

xhr.send(new Blob([data], { type: headers['Content-Type'] }));
xhr.send(data);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ describe('XhrTransport', function () {
undefined
);
assert.strictEqual(request.url, testTransportParameters.url);
assert.strictEqual(
(request.requestBody as unknown as Blob).type,
'application/json'
);
assert.strictEqual(request.requestBody, testPayload);
ensureHeadersContain(request.requestHeaders, {
foo: 'foo-value',
bar: 'bar-value',
Expand Down

0 comments on commit 1e312c2

Please sign in to comment.