Skip to content

Commit

Permalink
address mathieu comments 4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
usmanmani1122 committed Oct 28, 2024
1 parent d432dea commit d10fccd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
38 changes: 38 additions & 0 deletions packages/telemetry/src/context-aware-slog-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* eslint-env node */

import { makeFsStreamWriter } from '@agoric/internal/src/node/fs-stream.js';
import { makeContextualSlogProcessor } from './context-aware-slog.js';
import { serializeSlogObj } from './serialize-slog-obj.js';

/**
* @param {import('./index.js').MakeSlogSenderOptions} options
*/
export const makeSlogSender = async options => {
const { CHAIN_ID, CONTEXTUAL_SLOGFILE } = options.env || {};
if (!CONTEXTUAL_SLOGFILE)
return console.warn(
'Ignoring invocation of slogger "context-aware-slog-file" without the presence of "CONTEXTUAL_SLOGFILE"',
);

const stream = await makeFsStreamWriter(CONTEXTUAL_SLOGFILE);

if (!stream)
return console.error(
`Couldn't create a write stream on file "${CONTEXTUAL_SLOGFILE}"`,
);

const contextualSlogProcessor = makeContextualSlogProcessor({
'chain-id': CHAIN_ID,
});

/**
* @param {import('./context-aware-slog.js').Slog} slog
*/
const slogSender = slog =>
stream.write(`${serializeSlogObj(contextualSlogProcessor(slog))}\n`);

return Object.assign(slogSender, {
forceFlush: () => stream.flush(),
shutdown: () => stream.close(),
});
};
14 changes: 5 additions & 9 deletions packages/telemetry/src/otel-context-aware-slog.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,14 @@ import { makeContextualSlogProcessor } from './context-aware-slog.js';
import { getResourceAttributes } from './index.js';
import { serializeSlogObj } from './serialize-slog-obj.js';

/**
* @typedef {import('./index.js').MakeSlogSenderOptions} Options
*/

const DEFAULT_CONTEXT_FILE = 'slog-context.json';
const FILE_ENCODING = 'utf8';

/**
* @param {string} filePath
*/
export const getContextFilePersistenceUtils = filePath => {
console.log(`Using file ${filePath} for slogger context`);
console.warn(`Using file ${filePath} for slogger context`);

return {
/**
Expand All @@ -32,7 +28,7 @@ export const getContextFilePersistenceUtils = filePath => {
try {
writeFileSync(filePath, serializeSlogObj(context), FILE_ENCODING);
} catch (err) {
console.warn('Error writing context to file: ', err);
console.error('Error writing context to file: ', err);
}
},

Expand All @@ -43,20 +39,20 @@ export const getContextFilePersistenceUtils = filePath => {
try {
return JSON.parse(readFileSync(filePath, FILE_ENCODING));
} catch (parseErr) {
console.warn('Error reading context from file: ', parseErr);
console.error('Error reading context from file: ', parseErr);
return null;
}
},
};
};

/**
* @param {Options} options
* @param {import('./index.js').MakeSlogSenderOptions} options
*/
export const makeSlogSender = async options => {
const { CHAIN_ID, OTEL_EXPORTER_OTLP_ENDPOINT } = options.env || {};
if (!(OTEL_EXPORTER_OTLP_ENDPOINT && options.stateDir))
return console.warn(
return console.error(
'Ignoring invocation of slogger "context-aware-slog" without the presence of "OTEL_EXPORTER_OTLP_ENDPOINT" and "stateDir"',
);

Expand Down

0 comments on commit d10fccd

Please sign in to comment.