Skip to content

Commit

Permalink
Refactor BearerTokenAuthorization and Telemetry classes
Browse files Browse the repository at this point in the history
  • Loading branch information
simlarsen committed Feb 22, 2024
1 parent b9afe17 commit 2ff93a1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
10 changes: 4 additions & 6 deletions CommonServer/Tests/Middleware/BearerTokenAuthorization.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import BearerTokenAuthorization from '../../Middleware/BearerTokenAuthorization';
import { OneUptimeRequest, ExpressResponse } from '../../Utils/Express';
import ObjectID from 'Common/Types/ObjectID';
import JSONWebToken from '../../Utils/JsonWebToken';
import { JSONObject } from 'Common/Types/JSON';

Expand All @@ -9,7 +8,6 @@ describe('BearerTokenAuthorization', () => {
it('adds decoded token data to request', () => {
const jsonObj: JSONObject = { test: 'test' };
const req: OneUptimeRequest = {
id: ObjectID.generate(),
headers: {
authorization: `Bearer ${JSONWebToken.signJsonPayload(
jsonObj,
Expand All @@ -30,7 +28,7 @@ describe('BearerTokenAuthorization', () => {
it('calls next without arguments if token is valid', () => {
const jsonObj: JSONObject = { test: 'test' };
const req: OneUptimeRequest = {
id: ObjectID.generate(),

headers: {
authorization: `Bearer ${JSONWebToken.signJsonPayload(
jsonObj,
Expand All @@ -49,7 +47,7 @@ describe('BearerTokenAuthorization', () => {
});
it('calls next with exception if token is empty', () => {
const req: OneUptimeRequest = {
id: ObjectID.generate(),

headers: {
authorization: '',
},
Expand All @@ -67,7 +65,7 @@ describe('BearerTokenAuthorization', () => {
});
it('calls next with exception if token is invalid', () => {
const req: OneUptimeRequest = {
id: ObjectID.generate(),

headers: {
authorization: 'Bearer ',
},
Expand All @@ -85,7 +83,7 @@ describe('BearerTokenAuthorization', () => {
});
it('calls next with exception if token header is not present', () => {
const req: OneUptimeRequest = {
id: ObjectID.generate(),

} as OneUptimeRequest;
const res: ExpressResponse = {} as ExpressResponse;
const next: jest.Mock = jest.fn();
Expand Down
25 changes: 15 additions & 10 deletions CommonServer/Utils/Telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,9 @@ export default class OneUptimeTelemetry {
if (!this.sdk) {
const headers: Dictionary<string> = this.getHeaders();

let traceExporter: SpanExporter = new ConsoleSpanExporter();
let traceExporter: SpanExporter | undefined = undefined;

let metricReader: PeriodicExportingMetricReader =
new PeriodicExportingMetricReader({
exporter: new ConsoleMetricExporter(),
});
let metricReader: PeriodicExportingMetricReader | undefined = undefined;

if (this.getOltpTracesEndpoint()) {
traceExporter = new OTLPTraceExporter({
Expand Down Expand Up @@ -147,19 +144,27 @@ export default class OneUptimeTelemetry {

this.logger = logs.getLogger('default');

const sdk: opentelemetry.NodeSDK = new opentelemetry.NodeSDK({
const nodeSdkConfiguration: Partial<opentelemetry.NodeSDKConfiguration> = {
idGenerator: new AWSXRayIdGenerator(),
traceExporter: traceExporter,
metricReader: metricReader as any,
logRecordProcessor: loggerProvider as any,
instrumentations: [
new HttpInstrumentation(),
new ExpressInstrumentation(),
],
resource: this.getResource({
serviceName: data.serviceName,
}),
});
logRecordProcessor: loggerProvider as any,
};

if(traceExporter) {
nodeSdkConfiguration.traceExporter = traceExporter;
}

if(metricReader) {
nodeSdkConfiguration.metricReader = metricReader as any;
}

const sdk: opentelemetry.NodeSDK = new opentelemetry.NodeSDK(nodeSdkConfiguration);

process.on('SIGTERM', () => {
sdk.shutdown().finally(() => {
Expand Down

0 comments on commit 2ff93a1

Please sign in to comment.