Tracer Library

Tracer Library - Detailed Documentation

Table of Contents

  1. Overview

  2. Error Handling

  3. Logging

  4. OpenTelemetry Integration

  5. Configuration

  6. Integration Guide

  7. API Reference

Overview

The Tracer library is a comprehensive distributed tracing solution for Spring Boot applications that provides:

  • Correlation ID Management: Automatic correlation ID extraction, generation, and propagation across HTTP requests and Kafka messages

  • Comprehensive Logging: Detailed logging of HTTP requests/responses, Kafka messages, and database operations

  • Error Handling: Centralized exception handling with Dead Letter Queue (DLQ) support

  • OpenTelemetry Integration: Distributed tracing capabilities with Jaeger

  • Request/Response Tracing: Full request/response body logging with configurable detail levels

Key Features

  • Automatic correlation ID management across microservices

  • Structured logging with correlation ID context

  • Error queue publishing for failed operations

  • Kafka message tracing (producer/consumer)

  • HTTP request/response tracing

  • OpenTelemetry integration for distributed tracing

  • Configurable logging levels and patterns

Error Handling

Overview

The tracer library provides comprehensive error handling capabilities through centralized exception management and Dead Letter Queue (DLQ) integration.

Exception Handling Architecture

1. Global Exception Handler (ExceptionAdvise)

The library uses a @ControllerAdvice annotated class that catches all exceptions thrown in the application:

2. Supported Exception Types

The library handles the following exception types:

  • HttpMediaTypeNotSupportedException: Unsupported media type errors

  • ResourceAccessException: Service connectivity issues

  • HttpMessageNotReadableException: JSON parsing errors

  • JsonParseException: JSON parsing failures

  • JsonMappingException: Object mapping errors

  • MethodArgumentNotValidException: Validation errors

  • BindException: Binding errors

  • CustomException: Custom business logic exceptions

  • General Exception: All other unhandled exceptions

3. Error Response Structure

All errors are structured using the ErrorRes model:

Dead Letter Queue (DLQ) Integration

1. DLQ Configuration

To enable DLQ functionality, set the following properties:

2. Error Queue Contract

Errors are published to DLQ using the ErrorQueueContract structure:

3. DLQ Publishing Process

The error publishing process works as follows:

  1. Exception Detection: Global exception handler catches all exceptions

  2. Error Context Collection:

    • Request body extraction

    • Correlation ID retrieval

    • Stack trace capture

    • Error response structure creation

  3. DLQ Publishing: Error details are sent to configured Kafka topic

  4. Fallback Handling: If serialization fails, JSON string fallback is used

4. Error Queue Producer

The ErrorQueueProducer handles sending errors to Kafka topics:

Custom Exception Handling

1. CustomException Usage

The library supports custom exceptions with error codes and messages:

2. Validation Error Handling

For validation errors, the library automatically extracts binding errors:

Error Handling Configuration Parameters

Parameter
Description
Default
Required

tracer.errors.publish

Enable error publishing to DLQ

false

No

tracer.errors.topic

DLQ topic name

egov-error

Yes (if DLQ enabled)

tracer.errorDetails.topic

Error details topic

error-details-indexer-topic

Yes (if DLQ enabled)

tracer.errorDetails.publishFlag

Enable error details publishing

false

No

tracer.errors.provideExceptionInDetails

Include exception details in response

false

No

Logging

Overview

The tracer library provides comprehensive logging capabilities for HTTP requests, Kafka operations, and database calls with correlation ID context.

HTTP Request/Response Logging

1. Request Logging

The TracerFilter handles incoming HTTP request logging:

2. Response Logging

Response logging captures HTTP status codes:

3. RestTemplate Logging

Outgoing HTTP requests are logged using RestTemplateLoggingInterceptor:

Example: RestTemplate.postForObject

Kafka Logging

1. Producer Logging

Kafka message production is logged using LogAwareKafkaTemplate:

2. Consumer Logging

Kafka message consumption is logged using KafkaTemplateLoggingInterceptors:

3. Kafka Error Logging

Kafka operation failures are logged with detailed error information:

Database Call Logging

Database operations are logged using RestTemplateLoggingInterceptor

Correlation ID Context

All logging automatically includes correlation ID context through MDC (Mapped Diagnostic Context)

Logging Configuration Parameters

Parameter
Description
Default
Required

tracer.requestLoggingEnabled

Enable HTTP request body logging

false

No

tracer.kafkaMessageLoggingEnabled

Enable Kafka message body logging

false

No

tracer.restTemplateDetailedLoggingEnabled

Enable RestTemplate detailed logging

false

No

tracer.filterSkipPattern

URL patterns to skip tracing

/health,/metrics

No

logging.pattern.console

Logging pattern with correlation ID

See integration guide

Yes

Logging Patterns

Recommended logging pattern for correlation ID inclusion:

OpenTelemetry Integration

Why OpenTelemetry and Micrometer

  • Jaeger client libraries are deprecated and not compatible with JDK 17.

  • OpenTelemetry provides end-to-end tracing across HTTP and Kafka boundaries.

  • Micrometer adds standard service metrics (Tomcat threads, DB pools, Kafka clients) for better operational insights.

Version Note

  • OpenTelemetry and Micrometer integrations are available from tracer v2.9.1-SNAPSHOT.

pom.xml Changes

  1. Use tracer 2.9.1-SNAPSHOT.

  2. Import OpenTelemetry BOMs to align versions of OTEL artifacts used transitively by services.

Application Properties (Tracing and Micrometer)

Add these properties to enable OTEL tracing and Kafka instrumentation. Adjust values per service.

Notes

  • Set otel.service.name to the exact service name for correct grouping in Jaeger/Grafana Tempo.

  • Health and metrics endpoints should be excluded via otel.instrumentation.http.server.ignore-urls to reduce trace noise.

Database Query Tracing

To enable JDBC tracing:

  1. Update the JDBC URL to the OTEL variant (env/values file):

  1. Point your service's SPRING_DATASOURCE_URL to this OTEL URL.

Note

  • Prepending jdbc:otel: enables OTEL JDBC instrumentation for DB spans.

Property Reference

  • otel.traces.exporter: Use otlp to send traces to an OTLP-compatible backend (e.g., Jaeger collector).

  • otel.service.name: Logical service name used to group and search traces.

  • otel.logs.exporter: Set to none when logs are not exported via OTEL.

  • otel.metrics.exporter: Set to none when metrics are handled by Micrometer/Prometheus.

  • otel.exporter.otlp.endpoint: HTTP endpoint of the collector.

  • otel.exporter.otlp.protocol: Usually http/protobuf for Jaeger OTLP.

  • otel.instrumentation.kafka.enabled: Enables Kafka producer/consumer spans.

  • otel.instrumentation.kafka.experimental-span-attributes: Adds richer Kafka span attributes.

  • otel.instrumentation.http.server.ignore-urls: Comma-separated list of paths to exclude from tracing.

Configuration

Application Properties

Complete configuration example:

Configuration Properties Reference

Property
Type
Default
Description

tracer.filter.enabled

boolean

true

Enable tracer filter

tracer.opentracing.enabled

boolean

false

Enable OpenTelemetry tracing

tracer.requestLoggingEnabled

boolean

false

Enable HTTP request body logging

tracer.kafkaMessageLoggingEnabled

boolean

false

Enable Kafka message body logging

tracer.restTemplateDetailedLoggingEnabled

boolean

false

Enable RestTemplate detailed logging

tracer.errors.publish

boolean

false

Enable error publishing to DLQ

tracer.errors.topic

String

egov-error

DLQ topic name

tracer.errorDetails.topic

String

error-details-indexer-topic

Error details topic

tracer.errorDetails.publishFlag

boolean

false

Enable error details publishing

tracer.errors.provideExceptionInDetails

boolean

false

Include exception details in response

tracer.filterSkipPattern

String

/health,/metrics

URL patterns to skip tracing

Integration Guide

Maven Dependency

Add the tracer library to your pom.xml:

Spring Boot Integration

1. Import Tracer Configuration

Add the @Import annotation to your main application class:

2. Configure Application Properties

Add the required configuration to application.properties:

3. Use Tracer Components

For HTTP Requests:

Notes: The tracer library will automatically log the outgoing request, response, and correlation ID. No extra code is needed for correlation ID propagation or logging if the tracer is properly integrated.

For Kafka Operations:

API Reference

Core Classes

TracerConfiguration

Main configuration class that sets up all tracer components.

TracerProperties

Configuration properties holder for all tracer settings.

ExceptionAdvise

Global exception handler that processes all exceptions and manages DLQ publishing.

ErrorQueueProducer

Component responsible for publishing errors to Kafka DLQ topics.

LogAwareKafkaTemplate

Enhanced Kafka template with automatic logging and correlation ID management.

RestTemplateLoggingInterceptor

Interceptor for RestTemplate that adds correlation IDs and logs requests/responses.

TracerFilter

HTTP filter that manages correlation IDs and request/response logging.

Models

ErrorQueueContract

Structure for error messages sent to DLQ:

ErrorRes

Standard error response structure:

Error

Individual error structure:

Constants

TracerConstants

Key constants used throughout the library:

Annotations

Required annotation to import tracer configuration into Spring Boot application.

Dependencies

Required Dependencies

  • Spring Boot Web Starter

  • Spring Kafka

  • Apache Kafka Clients

  • Jackson (JSON processing)

  • Logback (logging)

  • Lombok (code generation)

Optional Dependencies

  • OpenTelemetry (for distributed tracing)

  • Jaeger (tracing backend)


Last updated

Was this helpful?