Skip to content
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

feat: add ability to shutdonw tracing gracefully #212

Merged
merged 1 commit into from
Nov 16, 2024
Merged

Conversation

saidsef
Copy link
Owner

@saidsef saidsef commented Nov 16, 2024

  1. Logger Configuration:
  • The diag.setLogger function now uses a DiagConsoleLogger with a log level set to INFO. This ensures that tracing logs are properly configured for debugging purposes.
  1. Tracer Provider Initialization:
  • The NodeTracerProvider is now initialized with a Resource object that includes attributes such as CONTAINER_NAME and DEPLOYMENT_ENVIRONMENT. This provides context to the tracing system.
  • The tracerProvider is declared in module scope for access in the stopTracing function.
  1. Propagator Configuration:
  • A CompositePropagator is used to combine multiple propagators (W3CBaggagePropagator, W3CTraceContextPropagator, and B3Propagator) for efficient propagation of tracing context across different systems.
  1. Span Processor Configuration:
  • The OTLPTraceExporter is configured with options for exporting traces to a collector endpoint using gRPC.
  • A BatchSpanProcessor is registered with the tracer provider to handle batch processing of spans before exporting them.
  1. Instrumentation Registration:
  • Various instrumentations (Express, Pino, HTTP, AWS, DNS) are registered with the tracer provider to instrument specific parts of the application.
  • Each instrumentation is configured with relevant hooks and settings to ensure accurate and efficient tracing.
  1. Graceful Shutdown:
  • A new stopTracing function has been added to gracefully shut down the tracing system by shutting down the tracer provider and ensuring all pending spans are exported before cleanup.

This commit enhances the OpenTelemetry tracing setup by improving logger configuration, tracer provider initialization, propagator combination, span processor configuration, instrumentation registration, and adding a graceful shutdown mechanism. These changes ensure robustness and efficiency in tracing operations within the application.

@saidsef saidsef self-assigned this Nov 16, 2024
@saidsef saidsef added documentation Improvements or additions to documentation enhancement New feature or request labels Nov 16, 2024
github-actions[bot]
github-actions bot previously approved these changes Nov 16, 2024
github-actions[bot]
github-actions bot previously approved these changes Nov 16, 2024
1. **Logger Configuration**:
- The `diag.setLogger` function now uses a `DiagConsoleLogger` with a log level set to `INFO`. This ensures that tracing logs are properly configured for debugging purposes.

2. **Tracer Provider Initialization**:
- The `NodeTracerProvider` is now initialized with a `Resource` object that includes attributes such as `CONTAINER_NAME` and `DEPLOYMENT_ENVIRONMENT`. This provides context to the tracing system.
- The `tracerProvider` is declared in module scope for access in the `stopTracing` function.

3. **Propagator Configuration**:
- A `CompositePropagator` is used to combine multiple propagators (`W3CBaggagePropagator`, `W3CTraceContextPropagator`, and `B3Propagator`) for efficient propagation of tracing context across different systems.

4. **Span Processor Configuration**:
- The `OTLPTraceExporter` is configured with options for exporting traces to a collector endpoint using gRPC.
- A `BatchSpanProcessor` is registered with the tracer provider to handle batch processing of spans before exporting them.

5. **Instrumentation Registration**:
- Various instrumentations (Express, Pino, HTTP, AWS, DNS) are registered with the tracer provider to instrument specific parts of the application.
- Each instrumentation is configured with relevant hooks and settings to ensure accurate and efficient tracing.

6. **Graceful Shutdown**:
- A new `stopTracing` function has been added to gracefully shut down the tracing system by shutting down the tracer provider and ensuring all pending spans are exported before cleanup.

This commit enhances the OpenTelemetry tracing setup by improving logger configuration, tracer provider initialization, propagator combination, span processor configuration, instrumentation registration, and adding a graceful shutdown mechanism. These changes ensure robustness and efficiency in tracing operations within the application.

chore: update k8s test branch
@saidsef saidsef added the preview Preview label Nov 16, 2024
@saidsef
Copy link
Owner Author

saidsef commented Nov 16, 2024

/code pr

Copy link
Owner Author

@saidsef saidsef left a comment

Choose a reason for hiding this comment

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

🤖
The changes look solid overall, with good improvements to the tracing implementation. Here's my review:

Positives:

  • Good addition of graceful shutdown functionality via stopTracing()
  • Improved code organisation with module-scoped tracerProvider
  • Better error handling in shutdown process
  • Clear documentation with JSDoc comments
  • Proper separation of exporter and span processor setup

Suggestions for improvement:

  1. In stopTracing():
// Consider adding a timeout parameter
export async function stopTracing(timeoutMs = 5000) {
  if (!tracerProvider) return;
  
  try {
    await Promise.race([
      tracerProvider.shutdown(),
      new Promise((_, reject) => 
        setTimeout(() => reject(new Error('Shutdown timeout')), timeoutMs)
      )
    ]);
    console.info('Tracing shutdown complete');
  } catch (error) {
    console.error('Tracing shutdown failed:', error);
  }
}
  1. Fix indentation inconsistencies in the instrumentations setup:
registerInstrumentations({
  tracerProvider: tracerProvider,
  instrumentations: [
    // Keep consistent indentation throughout
  ]
});
  1. Consider adding a simple status check method:
export function isTracingActive() {
  return tracerProvider !== null;
}

The changes are well-structured and improve the codebase. Just ensure the indentation is consistent throughout the file.

> claude-3-5-sonnet-20241022 [pr | 1]

@saidsef saidsef merged commit 0f5b491 into main Nov 16, 2024
7 checks passed
@saidsef saidsef deleted the tracing-shutdown branch November 16, 2024 12:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request preview Preview
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant