Skip to content
This repository was archived by the owner on Sep 15, 2021. It is now read-only.
Merged
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
12 changes: 12 additions & 0 deletions java/log_correlation/stackdriver/logback/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# OpenCensus/Logback/Stackdriver Log Correlation Demo

An application that demonstrates log correlation in Stackdriver with
`opencensus-contrib-log-correlation-stackdriver`. The application contains SLF4J log statements and
OpenCensus tracing instrumentation. It configures logging with a Logback XML configuration and
exports logs using the
[Stackdriver Logging Logback appender](https://cloud.google.com/logging/docs/setup/java#logback_appender).
It also exports traces using `opencensus-exporter-trace-stackdriver`, so that Stackdriver can show
the log entries associated with each trace.

The application requires a Google Cloud project with Stackdriver Logging enabled. It can be run with
`./gradlew run`.
32 changes: 32 additions & 0 deletions java/log_correlation/stackdriver/logback/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
description = 'OpenCensus/Logback/Stackdriver Log Correlation Demo'

apply plugin: 'java'

repositories {
mavenCentral()
}

group = "io.opencensus"
version = "0.16.0-SNAPSHOT"

def opencensusVersion = "0.15.0"

tasks.withType(JavaCompile) {
sourceCompatibility = '1.7'
targetCompatibility = '1.7'
}

dependencies {
compile "io.opencensus:opencensus-api:${opencensusVersion}",
"io.opencensus:opencensus-exporter-trace-stackdriver:${opencensusVersion}",
"org.slf4j:slf4j-api:1.7.25"

runtime "io.opencensus:opencensus-contrib-log-correlation-stackdriver:${opencensusVersion}",
"io.opencensus:opencensus-impl-lite:${opencensusVersion}",
"com.google.cloud:google-cloud-logging-logback:0.48.0-alpha"
}

apply plugin: 'application'

mainClassName =
'io.opencensus.demo.logcorrelation.stackdriver.logback.OpenCensusTraceLoggingEnhancerDemo'
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
172 changes: 172 additions & 0 deletions java/log_correlation/stackdriver/logback/gradlew

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions java/log_correlation/stackdriver/logback/gradlew.bat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2018, OpenCensus Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.opencensus.demo.logcorrelation.stackdriver.logback;

import io.opencensus.common.Scope;
import io.opencensus.exporter.trace.stackdriver.StackdriverTraceConfiguration;
import io.opencensus.exporter.trace.stackdriver.StackdriverTraceExporter;
import io.opencensus.trace.Sampler;
import io.opencensus.trace.Tracer;
import io.opencensus.trace.Tracing;
import io.opencensus.trace.samplers.Samplers;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Demo for {@code OpenCensusTraceLoggingEnhancer} using SLF4J and Logback. */
public final class OpenCensusTraceLoggingEnhancerDemo {

private static final Logger logger =
LoggerFactory.getLogger(OpenCensusTraceLoggingEnhancerDemo.class.getName());

private static final Tracer tracer = Tracing.getTracer();

private OpenCensusTraceLoggingEnhancerDemo() {}

/**
* Runs the demo. It creates a sampled and a non-sampled trace to demonstrate how log correlation
* handles both cases.
*/
public static void main(String[] args) throws IOException {
StackdriverTraceExporter.createAndRegister(StackdriverTraceConfiguration.builder().build());
createSampledTrace();
createNonSampledTrace();
Tracing.getExportComponent().shutdown();
}

private static void createSampledTrace() {
doWork(Samplers.alwaysSample());
}

private static void createNonSampledTrace() {
doWork(Samplers.neverSample());
}

private static void doWork(Sampler sampler) {
try (Scope scope = tracer.spanBuilder("ParentSpan").setSampler(sampler).startScopedSpan()) {
pause();
logger.warn("parent span log message 1");
pause();
doMoreWork();
pause();
logger.info("parent span log message 2");
pause();
}
}

private static void doMoreWork() {
try (Scope scope = tracer.spanBuilder("ChildSpan").startScopedSpan()) {
pause();
logger.info("child span log message 1");
pause();
logger.error("child span log message 2");
pause();
}
}

/** Sleeps for 500 ms to spread out the events on the trace. */
private static void pause() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// ignore
}
}
}
Loading