Skip to content

Commit

Permalink
Refactor telemetry configuration and remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
simlarsen committed Feb 22, 2024
1 parent 2ff93a1 commit 31bff7f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ describe('BearerTokenAuthorization', () => {
it('calls next without arguments if token is valid', () => {
const jsonObj: JSONObject = { test: 'test' };
const req: OneUptimeRequest = {

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

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

headers: {
authorization: 'Bearer ',
},
Expand All @@ -82,9 +79,7 @@ describe('BearerTokenAuthorization', () => {
);
});
it('calls next with exception if token header is not present', () => {
const req: OneUptimeRequest = {

} as OneUptimeRequest;
const req: OneUptimeRequest = {} as OneUptimeRequest;
const res: ExpressResponse = {} as ExpressResponse;
const next: jest.Mock = jest.fn();
void BearerTokenAuthorization.isAuthorizedBearerToken(
Expand Down
46 changes: 22 additions & 24 deletions CommonServer/Utils/Telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import * as opentelemetry from '@opentelemetry/sdk-node';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-proto';
import {
ConsoleMetricExporter,
PeriodicExportingMetricReader,
} from '@opentelemetry/sdk-metrics';
import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
import Dictionary from 'Common/Types/Dictionary';
import { AWSXRayIdGenerator } from '@opentelemetry/id-generator-aws-xray';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
Expand All @@ -17,10 +14,7 @@ import {
SimpleLogRecordProcessor,
} from '@opentelemetry/sdk-logs';
import URL from 'Common/Types/API/URL';
import {
ConsoleSpanExporter,
SpanExporter,
} from '@opentelemetry/sdk-trace-node';
import { SpanExporter } from '@opentelemetry/sdk-trace-node';
import { Resource } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { Logger, logs } from '@opentelemetry/api-logs';
Expand Down Expand Up @@ -102,7 +96,8 @@ export default class OneUptimeTelemetry {

let traceExporter: SpanExporter | undefined = undefined;

let metricReader: PeriodicExportingMetricReader | undefined = undefined;
let metricReader: PeriodicExportingMetricReader | undefined =
undefined;

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

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

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

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

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

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

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

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

0 comments on commit 31bff7f

Please sign in to comment.