Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

feat(logger): use a pretty logger in locale development with OTel enabled #1292

Merged
merged 4 commits into from
Feb 28, 2024

Conversation

10xLaCroixDrinker
Copy link
Member

Description

Default to pretty logger in console when using OTel for local development. Keep option to use OTel's console exporter when --log-format=machine

I also got rid of the extra logs that are printed during unit tests

Motivation and Context

Make logs easier to consume during local dev when using OTel logging

How Has This Been Tested?

Ran the application with the following combinations:

  • otel: enabled, NODE_ENV=production
  • otel: disabled, NODE_ENV=production
  • otel: enabled, NODE_ENV=development
  • otel: enabled, NODE_ENV=development, --log-format=machine
  • otel: disabled, NODE_ENV=development
  • otel: disabled, NODE_ENV=development, --log-format=machine

New and existing unit tests

Types of Changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation (adding or updating documentation)
  • Dependency update
  • Security update

Checklist:

  • My change requires a change to the documentation and I have updated the documentation accordingly.
  • These changes should be applied to a maintenance branch.
  • This change requires cross browser checks.
  • Performance tests should be ran against the server prior to merging.
  • This change impacts caching for client browsers.
  • This change impacts HTTP headers.
  • This change adds additional environment variable requirements for One App users.
  • I have added the Apache 2.0 license header to any new files created.

What is the Impact to Developers Using One App?

More easily consumable logs by default during local dev when OTel is enabled

Copy link
Contributor

github-actions bot commented Feb 8, 2024

Size Change: 0 B

Total Size: 719 kB

ℹ️ View Unchanged
Filename Size
./build/app/app.js 171 kB
./build/app/app~vendors.js 410 kB
./build/app/runtime.js 7.07 kB
./build/app/service-worker-client.js 7.25 kB
./build/app/vendors.js 123 kB

compressed-size-action

Comment on lines 47 to 53
let transport;

if (transportStreams.length === 1) {
[transport] = transportStreams;
} else if (transportStreams.length > 1) {
transport = multistream(transportStreams);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be converted into a small fn to make it more modular and readable

Suggested change
let transport;
if (transportStreams.length === 1) {
[transport] = transportStreams;
} else if (transportStreams.length > 1) {
transport = multistream(transportStreams);
}
const getTransport = (transportStreams = []) => {
if (transportStreams.length === 1) return transportStreams[0];
if (transportStreams.length > 1) return multistream(transportStreams);
}
...
const transport = getTransport(transportStreams);

transportStreams.push(require('./config/development').default);
}

let transport;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if transportStreams is an empty array?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if transportStreams is an empty array the variable is not used, and we passed undefined for the transport

Comment on lines 29 to 45
let pinoConfig = baseConfig;
const transportStreams = [];

if (process.env.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT) {
transportStreams.push(createOtelTransport({
grpc: true,
console: process.env.NODE_ENV === 'development' && argv.logFormat === 'machine',
}));
pinoConfig = deepmerge(baseConfig, otelConfig);
} else if (useProductionConfig) {
pinoConfig = deepmerge(baseConfig, productionConfig);
}

if (!useProductionConfig && !process.env.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT) {
if (!useProductionConfig) {
// eslint-disable-next-line global-require -- do not load development logger in production
transport = require('./config/development').default;
} else if (process.env.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT) {
transport = createOtelTransport();
transportStreams.push(require('./config/development').default);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the problem with mutation, especially here, is that cases for transport streams and for pino config are different, I would convert them into functions to make it more readable, like

const getTransportStreams = () => {
  const transportStreams = [];

  if (process.env.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT) {
    transportStreams.push(createOtelTransport({
      grpc: true,
      console: process.env.NODE_ENV === 'development' && argv.logFormat === 'machine',
    }));
  }

  if (!useProductionConfig) {
    // eslint-disable-next-line global-require -- do not load development logger in production
    transportStreams.push(require('./config/development').default);
  }

  return transportStreams;
}

const getPinoConfig = () => {
  if (process.env.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT) {
    return deepmerge(baseConfig, otelConfig);
  }

  if (useProductionConfig) {
    return deepmerge(baseConfig, productionConfig);
  }

  return baseConfig
}

@10xLaCroixDrinker 10xLaCroixDrinker force-pushed the feature/pretty-logger-with-otel-local branch from 0bf34d1 to 193515c Compare February 21, 2024 23:07
}

if (logRecordProcessorOptions.length === 1) {
[logRecordProcessorOptions] = logRecordProcessorOptions;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this intended for logRecordProcessorOptions to either be empty array or array of objects or single object{} ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

@10xLaCroixDrinker 10xLaCroixDrinker merged commit a810139 into main Feb 28, 2024
9 checks passed
@10xLaCroixDrinker 10xLaCroixDrinker deleted the feature/pretty-logger-with-otel-local branch February 28, 2024 19:44
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants