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

Cloud logging does not include trace ID #256

Closed
ashih opened this issue Sep 23, 2020 · 3 comments
Closed

Cloud logging does not include trace ID #256

ashih opened this issue Sep 23, 2020 · 3 comments
Assignees
Labels
api: logging Issues related to the googleapis/java-logging API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@ashih
Copy link

ashih commented Sep 23, 2020

Is your feature request related to a problem? Please describe.
When using cloud logging with App Engine, app logs are not correlated with request logs.

Describe the solution you'd like
I would like cloud logging to include trace ID so stackdriver can correlate the logs together. This is documented in Writing app logs.

Describe alternatives you've considered
Alternative solution is to write structured logs, but it would be nice if GAE users could use Google Cloud Logging to have correlated logs (from app to request) out of the box.

Additional context
See screenshot
image

@product-auto-label product-auto-label bot added the api: logging Issues related to the googleapis/java-logging API. label Sep 23, 2020
@athakor athakor self-assigned this Sep 24, 2020
@athakor athakor added the type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. label Sep 24, 2020
@athakor
Copy link
Contributor

athakor commented Oct 9, 2020

@ashih I think this can be done using Logging-Logback with TraceLoggingEnhancer. please check the below example that captures the trace ID in a Servlet filter then sets a ThreadLocal (using TraceLoggingEnhancer.setCurrentTraceId(String id).

Dependency:

        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>google-cloud-logging-logback</artifactId>
            <version>0.118.2-alpha</version>
        </dependency>

Servlet filter :

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import com.google.cloud.logging.TraceLoggingEnhancer;
import java.io.IOException;
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.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@WebFilter(filterName = "HelloServletFilter")
public class HelloServletFilter implements Filter {

    private final Logger logger = LoggerFactory.getLogger(HelloServletFilter.class);

    /**
     * JoranConfigurator is used to parse the logback.xml configuration file to use logback Appender
     * for logging.
     *
     * @param filterConfig configuration for servlet filter
     */
    @Override
    public void init(FilterConfig filterConfig) {
        try {
            LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
            final ServletContext servletContext = filterConfig.getServletContext();
            JoranConfigurator jc = new JoranConfigurator();
            jc.setContext(context);
            context.reset();
            // set application-name will be used while logging.
            context.putProperty("application-name", "hello world");
            jc.doConfigure(servletContext.getRealPath("/WEB-INF/logback.xml"));
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        // setting current traceId
        TraceLoggingEnhancer.setCurrentTraceId(req.getRemoteHost() + " for " + req.getRequestURL());
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {}
}

Servlet :

import com.google.cloud.logging.TraceLoggingEnhancer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "helloworld", value = "")
@SuppressWarnings("serial")
public class HelloServlet extends HttpServlet {

  private final Logger logger = LoggerFactory.getLogger(HelloServlet.class);

  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
      PrintWriter out = resp.getWriter();
      // getting current traceId and logger is used for logging
      logger.info(TraceLoggingEnhancer.getCurrentTraceId());
      out.println("Hello, world - App Engine Flexible");
  }
}

logback.xml :

<configuration>
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <!-- "specify filepath where log file will generate -->
        <!-- "application-name" is a variable -->
        <File>filepath/${application-name}.log</File>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d %p %t %c - %m%n</Pattern>
        </layout>
    </appender>
    <root level="debug">
        <appender-ref ref="FILE"/>
    </root>
</configuration>

LogEntry :

Log

@keenan-devrel keenan-devrel added the priority: p2 Moderately-important priority. Fix may not be included in next release. label Nov 11, 2020
@next-diegogarciateba
Copy link

Hi,

Is there any timeline to have logs correlation available using java.util.logging?

Thanks in advance!

Diego

@freelerobot freelerobot assigned simonz130 and unassigned freelerobot Jun 29, 2021
@minherz
Copy link
Contributor

minherz commented Oct 3, 2021

Duplicates #688

@minherz minherz closed this as completed Oct 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: logging Issues related to the googleapis/java-logging API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

No branches or pull requests

7 participants