Formatting. Building a message string you never emit is wasted work.
Allocation. Varargs arrays, boxed values, and message objects add garbage.
I/O and locking. A synchronous appender can block request threads on disk or on a shared lock.
Volume. Ten log lines per request at high traffic is a data pipeline you have to pay for.
Cardinality. Metric and span attributes with unbounded values (user IDs, request IDs as labels) explode storage and backend cost.
Logging guardrails
// Bad: concatenation always runslog.debug("Fetched profile "+id+" in "+elapsedMs+"ms");// Good: SLF4J formats only if DEBUG is enabledlog.debug("Fetched profile {} in {}ms",id,elapsedMs);if(log.isDebugEnabled()){log.debug("State: {}",expensiveDump(state));}Stringsnapshot;synchronized(lock){snapshot=state.summary();// cheap copy}log.info("Rebalanced: {}",snapshot);// logging outside the lock
Tracing guardrails
importio.opentelemetry.sdk.trace.SdkTracerProvider;importio.opentelemetry.sdk.trace.samplers.Sampler;SdkTracerProviderprovider=SdkTracerProvider.builder().setSampler(Sampler.parentBased(Sampler.traceIdRatioBased(0.05)))// ~5% of new traces.build();
Keep span attributes low-cardinality. Put high-cardinality identifiers in logs, not metric labels.
Avoid creating spans in tight loops. Instrument the request and the major dependencies.
Cap attribute value sizes.
Measure the overhead
importorg.openjdk.jmh.annotations.*;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;@State(Scope.Thread)@BenchmarkMode(Mode.AverageTime)@OutputTimeUnit(java.util.concurrent.TimeUnit.NANOSECONDS)publicclassLoggingBenchmark{privatestaticfinalLoggerlog=LoggerFactory.getLogger(LoggingBenchmark.class);// level = INFOprivatefinalStringid="user-12345";privatefinallongelapsed=42;@Benchmarkpublicvoidconcatenation(){log.debug("Fetched profile "+id+" in "+elapsed+"ms");}@Benchmarkpublicvoidparameterized(){log.debug("Fetched profile {} in {}ms",id,elapsed);}}
Failure modes
High-cardinality labels that overload the metrics backend.
Log amplification, where one failure triggers retries that each log at ERROR.
Synchronous appenders blocking request threads.
Tracing everything in staging, then discovering production cannot afford it.
A starter checklist
Parameterized logging everywhere, with guards for expensive arguments.
No logging inside locks or tight loops.
Async appender with a bounded queue.
Parent-based ratio sampling, with the ratio justified by numbers.
Attribute and label cardinality reviewed.
A benchmark or load test whenever you add instrumentation to a hot path.
Takeaways
Observability has a runtime cost that shows up in the tail.
Sample traces, bound log queues, and keep cardinality low.
Benchmark instrumentation like any other hot-path code.
Top comments (0)
Subscribe
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)