Skip to content

Commit

Permalink
Merge pull request #1227 from Tencent/master
Browse files Browse the repository at this point in the history
merge: merge master to 3.6.x
  • Loading branch information
wangyu096 authored Aug 26, 2022
2 parents dd5d398 + 2266a04 commit 7201e36
Show file tree
Hide file tree
Showing 82 changed files with 427 additions and 318 deletions.
1 change: 1 addition & 0 deletions package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ fi
cp -r support-files/bk-cmdb/ release/job/support-files/
cp -r support-files/bkiam/ release/job/support-files/
cp -r support-files/dependJarInfo/ release/job/support-files/
cp support-files/javaagent/* release/job/backend/
# Package dependJarLists
if [[ -d "support-files/dependJarLists/" ]]; then
cp -r support-files/dependJarLists/ release/job/support-files/
Expand Down
9 changes: 9 additions & 0 deletions src/backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ ext {
set("springBootVersion", "2.4.6")
// https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies
set('springCloudVersion', "2020.0.3")
set('springCloudOtelVersion', "1.0.0-M13")
// https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui
set('swaggerVersion', "2.9.2")
set('junitVersion', "5.5.2")
Expand Down Expand Up @@ -154,6 +155,12 @@ allprojects {
mavenLocal()
maven { url mavenRepoUrl }
maven { url "https://plugins.gradle.org/m2/" }
maven {
url "https://repo.spring.io/snapshot"
}
maven {
url "https://repo.spring.io/milestone"
}
mavenCentral()
}
}
Expand Down Expand Up @@ -198,6 +205,7 @@ subprojects {
imports {
mavenBom "org.springframework.boot:spring-boot-dependencies:$springBootVersion"
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
mavenBom "org.springframework.cloud:spring-cloud-sleuth-otel-dependencies:${springCloudOtelVersion}"
}
dependency "org.junit.jupiter:junit-jupiter:$junitVersion"
dependency "org.springframework.boot:spring-boot-starter-web:$springBootVersion"
Expand Down Expand Up @@ -262,6 +270,7 @@ subprojects {
configurations {
all*.exclude group: 'junit', module: 'junit'
all*.exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
all*.exclude group: 'org.springframework.cloud', module: 'spring-cloud-sleuth-brave'
}
}
apply plugin: TaskTreePlugin
Expand Down
4 changes: 4 additions & 0 deletions src/backend/commons/common-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,9 @@ dependencies {
api 'io.jsonwebtoken:jjwt'
api 'io.prometheus:simpleclient_pushgateway'
api 'io.micrometer:micrometer-registry-prometheus'
api "org.springframework.cloud:spring-cloud-sleuth-api"
api "org.springframework.cloud:spring-cloud-sleuth-otel"
api "org.springframework.cloud:spring-cloud-sleuth-otel-autoconfigure"
api "io.opentelemetry:opentelemetry-exporter-otlp-trace"
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@

package com.tencent.bk.job.common.trace.executors;

import brave.ScopedSpan;
import brave.Tracer;
import brave.Tracing;
import brave.propagation.TraceContext;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;

import java.util.concurrent.Callable;

Expand All @@ -39,25 +37,30 @@ public class TraceCallable<V> implements Callable<V> {
/**
* 调用链父上下文
*/
private final TraceContext parent;
private final Span parent;
private Callable<V> delegate;

public TraceCallable(Callable<V> callable, Tracing tracing) {
public TraceCallable(Callable<V> callable, Tracer tracer) {
this.delegate = callable;
this.tracer = tracing.tracer();
this.parent = tracing.currentTraceContext().get();
this.tracer = tracer;
this.parent = tracer.currentSpan();
}

@Override
public V call() throws Exception {
ScopedSpan span = this.tracer.startScopedSpanWithParent("async", parent);
Span span = null;
try {
span = tracer.nextSpan(parent).name("async");
return delegate.call();
} catch (Throwable e) {
span.error(e);
if (span != null) {
span.error(e);
}
throw e;
} finally {
span.finish();
if (span != null) {
span.end();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@

package com.tencent.bk.job.common.trace.executors;

import brave.ScopedSpan;
import brave.Tracer;
import brave.Tracing;
import brave.propagation.TraceContext;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;

public class TraceRunnable implements Runnable {
/**
Expand All @@ -37,25 +35,30 @@ public class TraceRunnable implements Runnable {
/**
* 调用链父上下文
*/
private final TraceContext parent;
private final Span parent;
private Runnable delegate;

public TraceRunnable(Runnable runnable, Tracing tracing) {
public TraceRunnable(Runnable runnable, Tracer tracer) {
this.delegate = runnable;
this.tracer = tracing.tracer();
this.parent = tracing.currentTraceContext().get();
this.tracer = tracer;
this.parent = tracer.currentSpan();
}

@Override
public void run() {
ScopedSpan span = this.tracer.startScopedSpanWithParent("async", parent);
Span span = null;
try {
span = tracer.nextSpan(parent).name("async");
delegate.run();
} catch (Throwable e) {
span.error(e);
if (span != null) {
span.error(e);
}
throw e;
} finally {
span.finish();
if (span != null) {
span.end();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

package com.tencent.bk.job.common.trace.executors;

import brave.Tracing;
import org.springframework.cloud.sleuth.Tracer;

import java.util.Collection;
import java.util.List;
Expand All @@ -38,11 +38,11 @@
public class TraceableExecutorService implements ExecutorService {

protected ExecutorService delegateExecutorService;
protected Tracing tracing;
protected Tracer tracer;

public TraceableExecutorService(ExecutorService executorService, Tracing tracing) {
public TraceableExecutorService(ExecutorService executorService, Tracer tracer) {
this.delegateExecutorService = executorService;
this.tracing = tracing;
this.tracer = tracer;
}

public ExecutorService getDelegateExecutorService() {
Expand Down Expand Up @@ -76,17 +76,17 @@ public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedE

@Override
public <T> Future<T> submit(Callable<T> task) {
return delegateExecutorService.submit(new TraceCallable(task, tracing));
return delegateExecutorService.submit(new TraceCallable(task, tracer));
}

@Override
public <T> Future<T> submit(Runnable task, T result) {
return delegateExecutorService.submit(new TraceRunnable(task, tracing), result);
return delegateExecutorService.submit(new TraceRunnable(task, tracer), result);
}

@Override
public Future<?> submit(Runnable task) {
return delegateExecutorService.submit(new TraceRunnable(task, tracing));
return delegateExecutorService.submit(new TraceRunnable(task, tracer));
}

@Override
Expand All @@ -113,6 +113,6 @@ public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout,

@Override
public void execute(Runnable command) {
delegateExecutorService.execute(new TraceRunnable(command, tracing));
delegateExecutorService.execute(new TraceRunnable(command, tracer));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@
package com.tencent.bk.job.common.trace.executors;


import brave.Tracing;
import org.springframework.cloud.sleuth.Tracer;

import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class TraceableScheduledExecutorService extends TraceableExecutorService implements ScheduledExecutorService {
public TraceableScheduledExecutorService(final ScheduledExecutorService delegate, final Tracing tracing) {
super(delegate, tracing);
public TraceableScheduledExecutorService(final ScheduledExecutorService delegate, final Tracer tracer) {
super(delegate, tracer);
}

private ScheduledExecutorService getScheduledExecutorService() {
Expand All @@ -43,26 +43,26 @@ private ScheduledExecutorService getScheduledExecutorService() {

@Override
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
Runnable r = new TraceRunnable(command, tracing);
Runnable r = new TraceRunnable(command, tracer);
return getScheduledExecutorService().schedule(r, delay, unit);
}


@Override
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
Callable<V> c = new TraceCallable<>(callable, tracing);
Callable<V> c = new TraceCallable<>(callable, tracer);
return getScheduledExecutorService().schedule(c, delay, unit);
}

@Override
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
Runnable r = new TraceRunnable(command, tracing);
Runnable r = new TraceRunnable(command, tracer);
return getScheduledExecutorService().scheduleAtFixedRate(r, initialDelay, period, unit);
}

@Override
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
Runnable r = new TraceRunnable(command, tracing);
Runnable r = new TraceRunnable(command, tracer);
return getScheduledExecutorService().scheduleWithFixedDelay(r, initialDelay, delay, unit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />

<include resource="logback/logback-app-props.xml"/>
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<property name="FILE_LOG_PATTERN" value="${FILE_LOG_PATTERN:-%d{${LOG_DATEFORMAT_PATTERN:-[yyyy-MM-dd HH:mm:ss.SSS]}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(%5p [${APP_NAME:-anon-service},%X{trace_id:-},%X{span_id:-}]) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<property name="FILE_LOG_PATTERN" value="${FILE_LOG_PATTERN:-%d{${LOG_DATEFORMAT_PATTERN:-[yyyy-MM-dd HH:mm:ss.SSS]}} %5p [${APP_NAME:-anon-service},%X{trace_id:-},%X{span_id:-}] ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<property name="APP_LOG_FILE" value="${APP_LOG_DIR}/${APP_LOG_NAME}"/>
<property name="ERROR_LOG_FILE" value="${APP_LOG_DIR}/error.log"/>
<property name="ESB_LOG_FILE" value="${APP_LOG_DIR}/esb_client.log"/>
Expand Down
1 change: 1 addition & 0 deletions src/backend/commons/common-web/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ dependencies {
api project(':commons:common-iam')
api 'org.springframework.boot:spring-boot-starter-web'
api 'org.springframework.boot:spring-boot-starter-security'
implementation 'com.google.guava:guava'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

package com.tencent.bk.job.common.web.interceptor;

import brave.Tracer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.tencent.bk.job.common.constant.HttpRequestSourceEnum;
Expand All @@ -42,11 +41,14 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpMethod;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.AsyncHandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand All @@ -63,10 +65,11 @@
*/
@Slf4j
@Component
public class JobCommonInterceptor extends HandlerInterceptorAdapter {
public class JobCommonInterceptor implements AsyncHandlerInterceptor {
private static final Pattern SCOPE_PATTERN = Pattern.compile("/scope/(\\w+)/(\\d+)");

private final Tracer tracer;
private Tracer.SpanInScope spanInScope = null;

private AppScopeMappingService appScopeMappingService;

Expand All @@ -87,12 +90,14 @@ public JobCommonInterceptor(Tracer tracer) {
}

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
public boolean preHandle(@NonNull HttpServletRequest request,
@NonNull HttpServletResponse response,
@NonNull Object handler) {
JobContextUtil.setStartTime();
JobContextUtil.setRequest(request);
JobContextUtil.setResponse(response);

addRequestId();
initSpanAndAddRequestId();

if (!shouldFilter(request)) {
return true;
Expand All @@ -111,8 +116,14 @@ private boolean shouldFilter(HttpServletRequest request) {
return uri.startsWith("/web/") || uri.startsWith("/service/") || uri.startsWith("/esb/");
}

private void addRequestId() {
String traceId = tracer.currentSpan().context().traceIdString();
private void initSpanAndAddRequestId() {
Span currentSpan = tracer.currentSpan();
if (currentSpan == null) {
currentSpan = tracer.nextSpan().start();
}
spanInScope = tracer.withSpan(currentSpan);
log.debug("currentSpan={}", currentSpan);
String traceId = currentSpan.context().traceId();
JobContextUtil.setRequestId(traceId);
}

Expand Down Expand Up @@ -325,7 +336,9 @@ private String parseValueFromQueryStringOrBody(HttpServletRequest request, Strin
}

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
public void postHandle(@NonNull HttpServletRequest request,
@NonNull HttpServletResponse response,
@NonNull Object handler,
ModelAndView modelAndView) {
if (log.isDebugEnabled()) {
log.debug("Post handler|{}|{}|{}|{}|{}", JobContextUtil.getRequestId(),
Expand All @@ -336,8 +349,9 @@ public void postHandle(HttpServletRequest request, HttpServletResponse response,
}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
Object handler,
public void afterCompletion(@NonNull HttpServletRequest request,
@NonNull HttpServletResponse response,
@NonNull Object handler,
Exception ex) {
try {
if (isClientOrServerError(response)) {
Expand All @@ -355,6 +369,9 @@ public void afterCompletion(HttpServletRequest request, HttpServletResponse resp
request.getRequestURI());
}
} finally {
if (spanInScope != null) {
spanInScope.close();
}
JobContextUtil.unsetContext();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
spring:
application:
name: job-analysis
profiles:
active: prod

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
spring:
application:
name: job-analysis
cloud:
kubernetes:
config:
Expand Down
Loading

0 comments on commit 7201e36

Please sign in to comment.