Skip to content

Commit 01c423d

Browse files
committed
Fix issue with missing context
1 parent 5503aa9 commit 01c423d

File tree

5 files changed

+53
-28
lines changed

5 files changed

+53
-28
lines changed

src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { createTerminus } from '@godaddy/terminus';
33
import app from './app';
44
import config from './config';
55
import logger from './logger';
6+
import services from './services';
67

78
const server = http.createServer(app);
89

@@ -28,6 +29,14 @@ createTerminus(server, {
2829

2930
async function start(): Promise<void> {
3031
try {
32+
const namespace = config.services.trace.daemonAddressNamespace;
33+
const name = config.services.trace.daemonAddressName;
34+
if (typeof namespace === 'string' && typeof name === 'string') {
35+
const address = await services.ServiceDiscovery.discoverInstance(namespace, name);
36+
37+
(services.Trace as any).setDaemonAddress(address);
38+
}
39+
3140
server.listen(config.port, () => {
3241
logger.info(`Server started at http://localhost:${ config.port }`);
3342
});

src/services/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import metadata from './metadata';
88
import job from './job';
99
import cache from './cache';
1010
import trace from './trace';
11+
import ServiceDiscovery from './serviceDiscovery';
1112
import config from '../config';
1213

1314
const container = {
@@ -62,6 +63,10 @@ const container = {
6263
default:
6364
return trace.CLSHooked;
6465
}
66+
},
67+
68+
get ServiceDiscovery() {
69+
return new ServiceDiscovery();
6570
}
6671
};
6772

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ServiceDiscoveryClient, DiscoverInstancesCommand, HealthStatusFilter } from '@aws-sdk/client-servicediscovery';
2+
3+
export class ServiceDiscovery {
4+
public async discoverInstance(namespace: string, name: string): Promise<string> {
5+
const client = new ServiceDiscoveryClient({ region: process.env.AWS_XRAY_REGION });
6+
7+
const input = {
8+
NamespaceName: namespace,
9+
ServiceName: name,
10+
MaxResults: 1,
11+
HealthStatus: HealthStatusFilter.HEALTHY_OR_ELSE_ALL,
12+
};
13+
14+
const command = new DiscoverInstancesCommand(input);
15+
16+
const result = await client.send(command);
17+
18+
const ip = result.Instances[0].Attributes.AWS_INSTANCE_IPV4;
19+
const port = result.Instances[0].Attributes.AWS_INSTANCE_PORT;
20+
21+
return `${ip}:${port}`;
22+
}
23+
}
24+
25+
export default ServiceDiscovery;

src/services/trace/index.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,11 @@
1-
import { ServiceDiscoveryClient, DiscoverInstancesCommand, HealthStatusFilter } from '@aws-sdk/client-servicediscovery';
21
import AWSXRay from './AWSXRay';
32
import CLSHooked from './CLSHooked';
43
import config from '../../config';
5-
import { nextTick } from 'process';
64

75
const container = {
86
get AWSXRay() {
97
if (typeof this._awsXRay === 'undefined') {
108
this._awsXRay = new AWSXRay(config.services.trace.plugins);
11-
12-
if (typeof config.services.trace.daemonAddressNamespace === 'string') {
13-
const setDaemonAddress = async () => {
14-
const client = new ServiceDiscoveryClient({ region: process.env.AWS_XRAY_REGION });
15-
16-
const input = {
17-
NamespaceName: config.services.trace.daemonAddressNamespace,
18-
ServiceName: config.services.trace.daemonAddressName,
19-
MaxResults: 1,
20-
HealthStatus: HealthStatusFilter.HEALTHY_OR_ELSE_ALL,
21-
};
22-
23-
const command = new DiscoverInstancesCommand(input);
24-
25-
const result = await client.send(command);
26-
27-
const ip = result.Instances[0].Attributes.AWS_INSTANCE_IPV4;
28-
const port = result.Instances[0].Attributes.AWS_INSTANCE_PORT;
29-
30-
this._awsXRay.setDaemonAddress(`${ip}:${port}`);
31-
};
32-
33-
nextTick(setDaemonAddress);
34-
}
359
}
3610

3711
return this._awsXRay;

src/worker.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ app.on('timeout_error', (err) => {
6969
logger.error('Timeout error!', { message: err.message, stack: err.stack });
7070
});
7171

72-
logger.info('Upload worker is running');
72+
async function start() {
73+
const namespace = config.services.trace.daemonAddressNamespace;
74+
const name = config.services.trace.daemonAddressName;
75+
if (typeof namespace === 'string' && typeof name === 'string') {
76+
const address = await services.ServiceDiscovery.discoverInstance(namespace, name);
7377

74-
app.start();
78+
(services.Trace as any).setDaemonAddress(address);
79+
}
80+
81+
logger.info('Upload worker is running');
82+
83+
app.start();
84+
}
85+
86+
start();

0 commit comments

Comments
 (0)