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

WIP: #260: add open-tracing support #278

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion modules/logging/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
</dependency>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
Expand Down Expand Up @@ -59,6 +59,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency> -->
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,36 @@ public interface DiagnosticContextFacade {
*/
void removeCorrelationId();

// /**
// *
// */
// void removeTraceId();
//
// /**
// *
// */
// void removeSpanId();
//
// /**
// *
// * @return
// */
// String getTraceId();
//
// /**
// *
// * @return
// */
// String getSpanId();
//
// /**
// * @param spanId
// */
// void setSpanId(String spanId);
//
// /**
// * @param traceId
// */
// void setTraceId(String traceId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ public final class LoggingConstants {
*/
public static final String CORRELATION_ID = "correlationId";

/**
* SPAN ID
*/
public static final String SPAN_ID = "spanId";

/**
* TRACEID
*/
public static final String TRACE_ID = "traceId";

/**
* SPANNAME
*/
public static final String SPAN_NAME = "spanName";

/**
* PARENT_ID
*/
public static final String PARENT_ID = "parentSpanId";

/**
* Construction prohibited.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,43 @@ public void removeCorrelationId() {
MDC.remove(LoggingConstants.CORRELATION_ID);
}

// @Override
// public void setTraceId(String traceId) {
//
// MDC.put(LoggingConstants.TRACE_ID, traceId);
//
// }
//
// @Override
// public void setSpanId(String spanId) {
//
// MDC.put(LoggingConstants.SPAN_ID, spanId);
// }
//
// @Override
// public void removeTraceId() {
//
// MDC.remove(LoggingConstants.TRACE_ID);
//
// }
//
// @Override
// public void removeSpanId() {
//
// MDC.remove(LoggingConstants.SPAN_ID);
//
// }
//
// @Override
// public String getTraceId() {
//
// return MDC.get(LoggingConstants.TRACE_ID);
// }
//
// @Override
// public String getSpanId() {
//
// return MDC.get(LoggingConstants.SPAN_ID);
// }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
// package com.devonfw.module.logging.common.impl;
//
// import java.io.IOException;
//
// import javax.inject.Inject;
// import javax.servlet.Filter;
// import javax.servlet.FilterChain;
// import javax.servlet.FilterConfig;
// import javax.servlet.ServletContext;
// import javax.servlet.ServletException;
// import javax.servlet.ServletRequest;
// import javax.servlet.ServletResponse;
// import javax.servlet.http.HttpServletRequest;
//
// import org.slf4j.Logger;
// import org.slf4j.LoggerFactory;
//
// import com.devonfw.module.logging.common.api.DiagnosticContextFacade;
// import com.devonfw.module.logging.common.api.LoggingConstants;
//
// import brave.Tracer;
// import brave.propagation.TraceContext;
//
/// **
// *
// */
// public class TraceContextFilter implements Filter {
//
// private static final Logger LOG = LoggerFactory.getLogger(TraceContextFilter.class);
//
// /**
// *
// */
// public static final String TRACE_ID_HEADER_NAME_PARAM = "traceIdHeaderName";
//
// /**
// *
// */
// public static final String TRACE_ID_HEADER_NAME_DEFAULT = "X-Trace-Id";
//
// /**
// *
// */
// public static final String SPAN_ID_HEADER_NAME_PARAM = "spanIdHeaderName";
//
// /**
// *
// */
// public static final String SPAN_ID_HEADER_NAME_DEFAULT = "X-Span-Id";
//
// /** @see #setTraceIdHttpHeaderName(String) */
// private String traceIdHttpHeaderName;
//
// /** @see #setSpanIdHttpHeaderName(String) */
// private String spanIdHttpHeaderName;
//
// private DiagnosticContextFacade diagnosticContextFacade;
//
// private TraceHeadersInjector traceHeadersInjector;
//
// @Inject
// private Tracer tracer;
//
// /**
// * The constructor.
// */
// public TraceContextFilter() {
//
// super();
// this.traceIdHttpHeaderName = TRACE_ID_HEADER_NAME_DEFAULT;
// this.spanIdHttpHeaderName = SPAN_ID_HEADER_NAME_DEFAULT;
// }
//
// /**
// * @param traceIdHttpHeaderName
// */
// public void setTraceIdHttpHeaderName(String traceIdHttpHeaderName) {
//
// this.traceIdHttpHeaderName = traceIdHttpHeaderName;
// }
//
// /**
// * @param spanIdHeaderName
// */
// public void setSpanIdHeaderName(String spanIdHeaderName) {
//
// this.spanIdHttpHeaderName = spanIdHeaderName;
// }
//
// private static String normalizeValue(String value) {
//
// if (value != null) {
// String result = value.trim();
// if (!result.isEmpty()) {
// return result;
// }
// }
// return null;
// }
//
// @Override
// public void init(FilterConfig filterConfig) throws ServletException {
//
// String traceHeaderName = filterConfig.getInitParameter(TRACE_ID_HEADER_NAME_PARAM);
// String spanHeaderName = filterConfig.getInitParameter(SPAN_ID_HEADER_NAME_PARAM);
//
// if (traceHeaderName == null) {
// LOG.debug("Parameter {} not configured via filter config.", TRACE_ID_HEADER_NAME_PARAM);
// } else {
// this.traceIdHttpHeaderName = traceHeaderName;
// }
//
// if (spanHeaderName == null) {
// LOG.debug("Parameter {} not configured via filter config.", SPAN_ID_HEADER_NAME_PARAM);
// } else {
// this.spanIdHttpHeaderName = spanHeaderName;
// }
//
// LOG.info("trace ID header initialized to: {}", this.traceIdHttpHeaderName);
// LOG.info("span ID header initialized to: {}", this.spanIdHttpHeaderName);
//
// if (this.diagnosticContextFacade == null) {
// try {
// // ATTENTION: We do not import these classes as we keep spring as an optional dependency.
// // If spring is not available in your classpath (e.g. some real JEE context) then this will produce a
// // ClassNotFoundException and use the fallback in the catch statement.
// ServletContext servletContext = filterConfig.getServletContext();
// org.springframework.web.context.WebApplicationContext springContext;
// springContext = org.springframework.web.context.support.WebApplicationContextUtils
// .getWebApplicationContext(servletContext);
// this.diagnosticContextFacade = springContext.getBean(DiagnosticContextFacade.class);
// } catch (Throwable e) {
// LOG.warn("DiagnosticContextFacade not defined in spring. Falling back to default", e);
// this.diagnosticContextFacade = new DiagnosticContextFacadeImpl();
// }
//
// }
//
// if (this.traceHeadersInjector == null) {
// try {
// // ATTENTION: We do not import these classes as we keep spring as an optional dependency.
// // If spring is not available in your classpath (e.g. some real JEE context) then this will produce a
// // ClassNotFoundException and use the fallback in the catch statement.
// ServletContext servletContext = filterConfig.getServletContext();
// org.springframework.web.context.WebApplicationContext springContext;
// springContext = org.springframework.web.context.support.WebApplicationContextUtils
// .getWebApplicationContext(servletContext);
// this.traceHeadersInjector = springContext.getBean(TraceHeadersInjector.class);
// } catch (Throwable e) {
// LOG.warn("TraceHeadersInjector not defined in spring. Falling back to default", e);
// this.traceHeadersInjector = new TraceHeadersInjector();
// }
//
// }
//
// }
//
// @Override
// public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
// throws IOException, ServletException {
//
// setTraceAndSpanId(request);
// try {
// chain.doFilter(request, response);
// } finally {
// this.diagnosticContextFacade.removeSpanId();
// this.diagnosticContextFacade.removeTraceId();
// }
//
// }
//
// private void setTraceAndSpanId(ServletRequest request) {
//
// String traceId = null;
// String spanId = null;
//
// if (request instanceof HttpServletRequest && this.traceIdHttpHeaderName != null
// && this.spanIdHttpHeaderName != null) {
//
// traceId = normalizeValue(((HttpServletRequest) request).getHeader(this.traceIdHttpHeaderName));
//
// if (traceId == null) {
// LOG.debug("No trace ID found for HTTP header {}.", this.traceIdHttpHeaderName);
// } else {
// this.diagnosticContextFacade.setTraceId(traceId);
// LOG.debug("Using traceId ID {} from HTTP header {}.", traceId, this.traceIdHttpHeaderName);
// return;
// }
//
// spanId = normalizeValue(((HttpServletRequest) request).getHeader(this.spanIdHttpHeaderName));
//
// if (spanId == null) {
// LOG.debug("No spanId ID found for HTTP header {}.", this.spanIdHttpHeaderName);
// } else {
// this.diagnosticContextFacade.setSpanId(spanId);
// LOG.debug("Using spanId ID {} from HTTP header {}.", spanId, this.spanIdHttpHeaderName);
// return;
// }
//
// }
//
// if (traceId == null && spanId == null) {
// // potential fallback if initialized before this filter...
// traceId = normalizeValue(this.diagnosticContextFacade.getTraceId());
// spanId = normalizeValue(this.diagnosticContextFacade.getSpanId());
//
// if (traceId != null && spanId != null) {
// LOG.debug("Trace ID and Span ID was already set to {} and {} before TraceContextFilter has been invoked.",
// traceId, spanId);
// } else {
// // no traceId ID and span ID present, inject from trace context
// TraceContext context = getActiveTraceContext();
// this.traceHeadersInjector.inject(context, this.diagnosticContextFacade);
// LOG.debug("Injected trace ID {} and span ID {} .", context.traceIdString(), context.spanIdString());
// }
// }
//
// }
//
// private TraceContext getActiveTraceContext() {
//
// if (this.tracer.currentSpan() == null) {
// this.tracer.nextSpan().name(LoggingConstants.SPAN_NAME).start();
// return this.tracer.nextSpan().context();
// }
//
// return this.tracer.currentSpan().context();
// }
//
// @Override
// public void destroy() {
//
// }
//
// }
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// package com.devonfw.module.logging.common.impl;
//
// import com.devonfw.module.logging.common.api.DiagnosticContextFacade;
//
// import brave.propagation.TraceContext;
//
/// **
// *
// */
// public class TraceHeadersInjector implements TraceContext.Injector<DiagnosticContextFacade> {
//
// @Override
// public void inject(TraceContext traceContext, DiagnosticContextFacade contextFacade) {
//
// String traceId = traceContext.traceIdString();
// contextFacade.setTraceId(traceId);
//
// String spanId = traceContext.spanIdString();
// contextFacade.setSpanId(spanId);
//
// }
//
// }
Loading