Skip to content

Commit

Permalink
feat(mockotlpserver): add a luggite logger
Browse files Browse the repository at this point in the history
Hardcoded to stdout and INFO level for starters.
Use 'npm start | pino-pretty' for pretty rendering of the logs.

Also add 'npm run watch' for dev convenience.
  • Loading branch information
trentm committed Dec 20, 2023
1 parent 373206e commit 621493b
Show file tree
Hide file tree
Showing 6 changed files with 462 additions and 19 deletions.
13 changes: 9 additions & 4 deletions package-lock.json

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

14 changes: 7 additions & 7 deletions packages/mockotlpserver/lib/grpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ for (const [name, path] of Object.entries(packages)) {

function intakeTraces(call, callback) {
const tracesReq = call.request;
// console.log('grpc spans', tracesReq);
console.dir(tracesReq, {depth: 9});

callback(null, {
Expand All @@ -48,12 +47,13 @@ function intakeTraces(call, callback) {

/**
*
* @param {Object} options
* @param {string} options.hostname
* @param {number} options.port
* @param {Object} opts
* @param {import('./luggite').Logger} opts.log
* @param {string} opts.hostname
* @param {number} opts.port
*/
function startGrpc(options) {
const {hostname, port} = options;
function startGrpc(opts) {
const {log, hostname, port} = opts;
const grpcServer = new grpc.Server();

grpcServer.addService(packages.trace.TraceService.service, {
Expand All @@ -64,7 +64,7 @@ function startGrpc(options) {
`${hostname}:${port}`,
grpc.ServerCredentials.createInsecure(),
() => {
console.log(`OTLP/gRPC listening at http://${hostname}:${port}`);
log.info(`OTLP/gRPC listening at http://${hostname}:${port}`);
grpcServer.start();
}
);
Expand Down
13 changes: 7 additions & 6 deletions packages/mockotlpserver/lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,13 @@ function unknownParser(buff, req) {

/**
*
* @param {Object} options
* @param {string} options.hostname
* @param {number} options.port
* @param {Object} opts
* @param {import('./luggite').Logger} opts.log
* @param {string} opts.hostname
* @param {number} opts.port
*/
function startHttp(options) {
const {hostname, port} = options;
function startHttp(opts) {
const {log, hostname, port} = opts;
const server = http.createServer((req, res) => {
const chunks = [];
req.on('data', (chunk) => chunks.push(chunk));
Expand Down Expand Up @@ -140,7 +141,7 @@ function startHttp(options) {
addr.family === 'IPv6'
? `http://[${addr.address}]:${addr.port}`
: `http://${addr.address}:${addr.port}`;
console.log(`OTLP/HTTP listening at ${endpoint}`);
log.info(`OTLP/HTTP listening at ${endpoint}`);
});
}

Expand Down
6 changes: 6 additions & 0 deletions packages/mockotlpserver/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const luggite = require('./luggite');

const {startHttp} = require('./http');
const {startGrpc} = require('./grpc');

const log = luggite.createLogger({name: 'mockotlpserver'});

// Default hostname to 'localhost', because that is what `DEFAULT_COLLECTOR_URL`
// uses in the OTel core exporter packages. Note that 'localhost' can by IPv6
// '::1' or IPv4 '127.0.0.1', which can potentially cause confusion.
Expand All @@ -12,6 +16,7 @@ const DEFAULT_GRPC_PORT = 4317;
// Handles `OTEL_EXPORTER_OTLP_PROTOCOL=http/proto` and
// `OTEL_EXPORTER_OTLP_PROTOCOL=http/json`.
startHttp({
log,
hostname: DEFAULT_HOSTNAME,
port: DEFAULT_HTTP_PORT,
});
Expand All @@ -21,6 +26,7 @@ startHttp({
//
// NOTE: to debug read this: https://github.com/grpc/grpc-node/blob/master/TROUBLESHOOTING.md
startGrpc({
log,
hostname: DEFAULT_HOSTNAME,
port: DEFAULT_GRPC_PORT,
});
Loading

0 comments on commit 621493b

Please sign in to comment.