Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(otlp-exporter-*-proto): clean up tests #5199

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ All notable changes to experimental packages in this project will be documented

### :house: (Internal)

* chore(otlp-exporter-*-proto): clean up tests [#5196](https://github.com/open-telemetry/opentelemetry-js/pull/5199) @pichlermarc
pichlermarc marked this conversation as resolved.
Show resolved Hide resolved

## 0.55.0

### :boom: Breaking Change
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,82 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as assert from 'assert';
import * as sinon from 'sinon';
import { OTLPLogExporter } from '../../src/platform/browser/index';

describe('OTLPLogExporter - web', () => {
let collectorLogsExporter: OTLPLogExporter;
describe('constructor', () => {
beforeEach(() => {
const collectorExporterConfig = {
hostname: 'foo',
url: 'http://foo.bar.com',
};
collectorLogsExporter = new OTLPLogExporter(collectorExporterConfig);
});
afterEach(() => {
sinon.restore();

import { OTLPLogExporter } from '../../src/platform/browser';
import {
LoggerProvider,
SimpleLogRecordProcessor,
} from '@opentelemetry/sdk-logs';

/*
* NOTE: Tests here are not intended to test the underlying components directly. They are intended as a quick
* check if the correct components are used. Use the following packages to test details:
* - `@opentelemetry/oltp-exporter-base`: OTLP common exporter logic (handling of concurrent exports, ...), HTTP transport code
* - `@opentelemetry/otlp-transformer`: Everything regarding serialization and transforming internal representations to OTLP
*/

describe('OTLPLogExporter', function () {
afterEach(() => {
sinon.restore();
});

describe('export', function () {
describe('when sendBeacon is available', function () {
it('should successfully send data using sendBeacon', async function () {
// arrange
const stubBeacon = sinon.stub(navigator, 'sendBeacon');
const loggerProvider = new LoggerProvider();
loggerProvider.addLogRecordProcessor(
new SimpleLogRecordProcessor(new OTLPLogExporter())
);

// act
loggerProvider.getLogger('test-logger').emit({ body: 'test-body' });
await loggerProvider.shutdown();

// assert
const args = stubBeacon.args[0];
const blob: Blob = args[1] as unknown as Blob;
const body = await blob.text();
assert.throws(
() => JSON.parse(body),
'expected requestBody to be in protobuf format, but parsing as JSON succeeded'
);
});
});
it('should create an instance', () => {
assert.ok(typeof collectorLogsExporter !== 'undefined');

describe('when sendBeacon is not available', function () {
beforeEach(function () {
// fake sendBeacon not being available
(window.navigator as any).sendBeacon = false;
});

it('should successfully send data using XMLHttpRequest', async function () {
// arrange
const server = sinon.fakeServer.create();
const loggerProvider = new LoggerProvider();
loggerProvider.addLogRecordProcessor(
new SimpleLogRecordProcessor(new OTLPLogExporter())
);

// act
loggerProvider.getLogger('test-logger').emit({ body: 'test-body' });
queueMicrotask(() => {
// simulate success response
server.requests[0].respond(200, {}, '');
});
await loggerProvider.shutdown();

// assert
const request = server.requests[0];
const body = request.requestBody as unknown as Uint8Array;
assert.throws(
() => JSON.parse(new TextDecoder().decode(body)),
'expected requestBody to be in protobuf format, but parsing as JSON succeeded'
);
});
});
});
});
190 changes: 0 additions & 190 deletions experimental/packages/exporter-logs-otlp-proto/test/logHelper.ts

This file was deleted.

Loading