Skip to content

Add OpenTelemetry support #85

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

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6965d67
add docker-compose
Iffern Nov 29, 2022
1621f53
working version
Iffern Dec 28, 2022
1788cba
Use opentelemetry-collector
lwronski Jan 1, 2023
24a4081
Merge pull request #1 from lwronski/opentelemetry
Iffern Jan 1, 2023
b5f9bd2
move require tracing higher
Iffern Jan 14, 2023
81d3201
Downgrade package version
lwronski Jan 14, 2023
703014a
Propagate trace_id and span_id to job-executor
lwronski Jan 14, 2023
e9a4ac2
Merge pull request #2 from lwronski/opentelemetry
iczajowska Jan 15, 2023
43ce38b
Use Provider abd Processor from node SDK
lwronski Jan 15, 2023
2a3e595
Merge pull request #3 from lwronski/opentelemetry
Iffern Jan 16, 2023
56804aa
add tracing to kubernetes
Iffern Jan 18, 2023
240af20
node auto instrumentation
iczajowska Jan 18, 2023
9ea7108
update method call
Iffern Jan 18, 2023
15f011b
Merge pull request #4 from hyperflow-lsc/opentelemetry-instr
Iffern Jan 22, 2023
c87b5bd
Merge branch 'opentelemetry' into kubernetes
Iffern Jan 22, 2023
36b1766
add tracing to .dockerignore
Iffern Jan 22, 2023
3129713
cleanup package.json
Iffern Jan 22, 2023
f188ad8
cleanup
Iffern Jan 23, 2023
6407fd5
delete docker-compose.yaml
Iffern Jan 23, 2023
d81c756
add span.end()
Iffern Jan 23, 2023
e6789a3
add span.end()
Iffern Jan 23, 2023
0f36425
update async spans
Iffern Jan 23, 2023
5d116b6
Merge pull request #6 from hyperflow-lsc/attibutes_cleanup
Iffern Jan 23, 2023
8f147cc
update package-lock.json
Iffern Jan 23, 2023
844e4e3
remove unnecessary changes
Iffern Jan 23, 2023
ff6ae59
move configuration to env variables
Iffern Jan 25, 2023
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
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
!tests
!utils
!wflib
!tracing.js
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ FROM node:12-alpine
#ENV PATH $PATH:/node_modules/.bin

COPY . /hyperflow
RUN npm install -g /hyperflow
RUN npm install -g /hyperflow
110 changes: 68 additions & 42 deletions examples/RemoteJobs/functions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const tracer = process.env.HF_VAR_ENABLE_TRACING === "1" ? require("../../tracing.js")("hyperflow-service") : undefined;
var spawn = require('child_process').spawn;
var log4js = require('log4js');
var createJobMessage = require('../../common/jobMessage.js').createJobMessage;
Expand All @@ -6,64 +7,89 @@ var createJobMessage = require('../../common/jobMessage.js').createJobMessage;
// Spawns a job "node handler.js" and waits for the notification of its
// completion using the Redis job status notification mechanism
async function submitRemoteJob(ins, outs, context, cb) {
let fname='wftrace-' + context.hfId + '-' + context.appId + '.log';

let jobMessage = JSON.stringify(createJobMessage(ins, outs, context));

let fname = 'wftrace-' + context.hfId + '-' + context.appId + '.log';

log4js.configure({
appenders: { hftrace: { type: 'file', filename: fname } },
categories: { default: { appenders: ['hftrace'], level: 'error' } }
appenders: {hftrace: {type: 'file', filename: fname}},
categories: {default: {appenders: ['hftrace'], level: 'error'}}
});

var logger = log4js.getLogger();

logger.level = 'debug';
console.log("Spawning process...");

//console.log(ins.map(i => i));

var input_dir = context.executor.input_dir,
function runJob(trace_id = undefined, parent_id = undefined, span = undefined) {
var input_dir = context.executor.input_dir,
work_dir = context.executor.work_dir,
output_dir = context.executor.output_dir;

let jobMessage = JSON.stringify(createJobMessage(ins, outs, context));

var cmd;

// if 'container' is present, run through Docker, mounting all directories if necessary
if (context.container) {
cmd = 'docker run ';
if (input_dir) cmd += ' -v ' + input_dir + ':/input_dir ';
if (work_dir) cmd += ' -v ' + work_dir + ':/work_dir ';
if (output_dir) cmd += ' -v ' + output_dir + ':/output_dir ';
cmd += container + ' node';
} else cmd = 'node'

try {
if (work_dir) { process.chdir(work_dir); }
} catch (error) {
throw error;
var cmd;

// if 'container' is present, run through Docker, mounting all directories if necessary
if (context.container) {
cmd = 'docker run ';
if (input_dir) cmd += ' -v ' + input_dir + ':/input_dir ';
if (work_dir) cmd += ' -v ' + work_dir + ':/work_dir ';
if (output_dir) cmd += ' -v ' + output_dir + ':/output_dir ';
cmd += container + ' node';
} else cmd = 'node'

try {
if (work_dir) {
process.chdir(work_dir);
}
} catch (error) {
throw error;
}

// "submit" job (start the handler process)
var proc = spawn(cmd, ['../../../hyperflow-job-executor/jobexec.js', context.taskId, context.redis_url],
{shell: true, env: {...process.env, HF_VAR_OT_PARENT_ID: parent_id, HF_VAR_OT_TRACE_ID: trace_id,
HF_VAR_ENABLE_TRACING: process.env.HF_VAR_ENABLE_TRACING}});

if (span !== undefined && span.isRecording()) {
span.setAttributes({
'hfId': context.hfId, 'appId': context.appId, 'input_dir': input_dir,
'work_dir': work_dir, 'output_dir': output_dir
})
}

proc.stderr.on('data', function (data) {
logger.debug(data.toString());
console.log(data.toString());
});

proc.stdout.on('data', function (data) {
logger.debug(data.toString());
console.log(data.toString());
});

proc.on('exit', function (code) {
logger.debug('Process exited with code', code);
});
}

// "submit" job (start the handler process)
var proc = spawn(cmd, ['../../../hyperflow-job-executor/jobexec.js', context.taskId, context.redis_url], {shell: true});

proc.stderr.on('data', function(data) {
logger.debug(data.toString());
console.log(data.toString());
});

proc.stdout.on('data', function(data) {
logger.debug(data.toString());
console.log(data.toString());
});

proc.on('exit', function(code) {
logger.debug('Process exited with code', code);
});
if (process.env.HF_VAR_ENABLE_TRACING === "1") {
tracer.startActiveSpan('submitRemoteJob', span => {
//console.log(ins.map(i => i));
var trace_id = span.spanContext().traceId
var parent_id = span.spanContext().spanId
runJob(trace_id, parent_id, span)
span.end();
});
} else {
runJob()
}

// send message to the job (command to be executed)
try {
await context.sendMsgToJob(jobMessage, context.taskId);
logger.info('[' + context.taskId + '] job message sent');
} catch(err) {
} catch (err) {
console.error(err);
throw err;
}
Expand All @@ -74,10 +100,10 @@ async function submitRemoteJob(ins, outs, context, cb) {
logger.info('[' + context.taskId + '] job result received:', jobResult);
console.log('Received job result:', jobResult);
cb(null, outs);
} catch(err) {
} catch (err) {
console.error(err);
throw err;
}
}

exports.submitRemoteJob = submitRemoteJob;
exports.submitRemoteJob = submitRemoteJob;
1 change: 1 addition & 0 deletions examples/RemoteJobs/handler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Executor of 'jobs' using the Redis task status notification mechanism
process.env.HF_VAR_ENABLE_TRACING === "1" && require("../../tracing.js")("hyperflow-service");
const redis = require('redis');
const { spawn } = require('child_process');

Expand Down
4 changes: 2 additions & 2 deletions examples/RemoteJobs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions functions/k8sCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const k8s = require('@kubernetes/client-node');
const yaml = require('js-yaml');
var fs = require('fs');

async function k8sCommand(ins, outs, context, cb) {
async function k8sCommand(ins, outs, context, cb, parentId, traceId) {

let handlerStart = Date.now();
console.log("[DEBUG] K8sInvoke called.");
Expand All @@ -29,7 +29,7 @@ async function k8sCommand(ins, outs, context, cb) {

const k8sApi = kc.makeApiClient(k8s.BatchV1Api);
try {
var command = 'hflow-job-execute ' + context.taskId + ' ' + context.redis_url;
var command = 'hflow-job-execute ' + context.taskId + ' ' + context.redis_url + ' ' + parentId + ' ' + traceId;
var containerName = process.env.HF_VAR_WORKER_CONTAINER;
var volumePath = '/work_dir';
var jobName = Math.random().toString(36).substring(7) + '-' + context.name.replace(/_/g, '-') + "-" + context.procId;
Expand Down
32 changes: 27 additions & 5 deletions functions/kubernetes/k8sCommand.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Runs a job as a Pod (Kubernetes Job) in a Kubernetes cluster

const tracer = process.env.HF_VAR_ENABLE_TRACING === "1" ? require("../../tracing.js")("hyperflow-kubernetes") : undefined;
const k8s = require('@kubernetes/client-node');
var BufferManager = require('./buffer_manager.js').BufferManager;
var RestartCounter = require('./restart_counter.js').RestartCounter;
Expand Down Expand Up @@ -125,18 +126,39 @@ async function k8sCommandGroup(bufferItems) {
kubeconfig.loadFromDefault(); // loadFromString(JSON.stringify(kconfig))

let jobExitCodes = [];
try {

const executeJobs = async (span) => {
if (getExecutorType(context) === "WORKER_POOL") {
await amqpEnqueueJobs(jobArr, taskIdArr, contextArr, customParams)
} else {
await submitK8sJob(kubeconfig, jobArr, taskIdArr, contextArr, customParams)
var traceId = span && span.spanContext().traceId
var parentId = span && span.spanContext().spanId
await submitK8sJob(kubeconfig, jobArr, taskIdArr, contextArr, customParams, parentId, traceId)
}
jobExitCodes = await synchronizeJobs(jobArr, taskIdArr, contextArr, customParams, restartFn);
} catch (err) {
console.log("Error when submitting job:", err);
throw err;
}

if (process.env.HF_VAR_ENABLE_TRACING === "1" && tracer){
tracer.startActiveSpan('k8sCommands', async span => {
try {
await executeJobs();
} catch (err) {
console.log("Error when submitting job:", err);
throw err;
} finally {
span.end();
}
});
} else {
try {
await executeJobs();
} catch (err) {
console.log("Error when submitting job:", err);
throw err;
}
}


let endTime = Date.now();
console.log("Ending k8sCommandGroup function, time:", endTime, "exit codes:", jobExitCodes);

Expand Down
10 changes: 6 additions & 4 deletions functions/kubernetes/k8sJobSubmit.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function createK8sJobMessage(job, taskId, context) {
//
// Returns:
// - jobYaml: string with job YAML to create the k8s job
var createK8sJobYaml = (job, taskIds, context, jobYamlTemplate, customParams) => {
var createK8sJobYaml = (job, taskIds, context, jobYamlTemplate, customParams, parentId, traceId) => {
let quotedTaskIds = taskIds.map(x => '"' + x + '"');
var command = 'hflow-job-execute ' + context.redis_url + ' -a -- ' + quotedTaskIds.join(' ');
var containerName = job.image || process.env.HF_VAR_WORKER_CONTAINER;
Expand Down Expand Up @@ -81,7 +81,9 @@ var createK8sJobYaml = (job, taskIds, context, jobYamlTemplate, customParams) =>
restartPolicy: restartPolicy, backoffLimit: backoffLimit,
experimentId: context.hfId + ":" + context.appId,
workflowName: context.wfname, taskName: job.name,
appId: context.appId, workingDirPath: workingDirPath
appId: context.appId, workingDirPath: workingDirPath,
enableTracing: process.env.HF_VAR_ENABLE_TRACING, optParentId: parentId,
optTraceId: traceId
}

// Add/override custom parameters for the job
Expand All @@ -105,7 +107,7 @@ var createK8sJobYaml = (job, taskIds, context, jobYamlTemplate, customParams) =>
//
//
// Returns: job exit code
var submitK8sJob = async(kubeconfig, jobArr, taskIdArr, contextArr, customParams) => {
var submitK8sJob = async(kubeconfig, jobArr, taskIdArr, contextArr, customParams, parentId, traceId) => {

// Load definition of the the worker job pod
// File 'job-template.yaml' should be provided externally during deployment
Expand All @@ -116,7 +118,7 @@ var submitK8sJob = async(kubeconfig, jobArr, taskIdArr, contextArr, customParams
let context = contextArr[0];

// CAUTION: When creating job YAML first job details (requests, container) are used.
var jobYaml = createK8sJobYaml(jobArr[0], taskIdArr, contextArr[0], jobYamlTemplate, customParams);
var jobYaml = createK8sJobYaml(jobArr[0], taskIdArr, contextArr[0], jobYamlTemplate, customParams, parentId, traceId);
let jobMessages = [];
for (var i=0; i<jobArr.length; i++) {
let job = jobArr[i];
Expand Down
Loading