Skip to content

Commit

Permalink
feat: 集群间请求传递traceid TencentBlueKing#1770
Browse files Browse the repository at this point in the history
  • Loading branch information
yaoxuwan authored Feb 18, 2024
1 parent 5ba5ebb commit 8c89ae3
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,8 @@ const val ACCESS_FROM_API = "api"
* 来源于浏览器访问
*/
const val ACCESS_FROM_WEB = "web"

/**
* b3 trace header
*/
const val B3_TRACE = "b3"
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package com.tencent.bkrepo.common.service.feign

import com.google.common.hash.Hashing
import com.tencent.bkrepo.common.api.constant.B3_TRACE
import com.tencent.bkrepo.common.api.constant.HttpHeaders
import com.tencent.bkrepo.common.api.constant.MS_AUTH_HEADER_UID
import com.tencent.bkrepo.common.api.constant.MS_REQUEST_SRC_CLUSTER
Expand All @@ -37,6 +38,7 @@ import com.tencent.bkrepo.common.api.constant.ensureSuffix
import com.tencent.bkrepo.common.api.constant.urlEncode
import com.tencent.bkrepo.common.api.util.BasicAuthUtils
import com.tencent.bkrepo.common.service.cluster.ClusterInfo
import com.tencent.bkrepo.common.service.otel.util.TraceHeaderUtils
import com.tencent.bkrepo.common.service.util.HeaderUtils
import com.tencent.bkrepo.common.service.util.HttpContextHolder
import com.tencent.bkrepo.common.service.util.HttpSigner
Expand Down Expand Up @@ -117,6 +119,7 @@ object FeignClientFactory {
if (!srcClusterName.isNullOrBlank()) {
it.header(MS_REQUEST_SRC_CLUSTER, srcClusterName)
}
it.header(B3_TRACE, TraceHeaderUtils.buildB3Header())
if (cluster.appId != null) {
// 内部集群请求签名
require(cluster.accessKey != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.tencent.bkrepo.common.service.otel.util

import com.tencent.bkrepo.common.service.util.SpringContextUtils
import org.springframework.beans.BeansException
import org.springframework.cloud.sleuth.Tracer

object TraceHeaderUtils {

fun buildB3Header(): String {
val trace = try {
SpringContextUtils.getBean<Tracer>()
} catch (_ : BeansException) {
null
}

val context = trace?.currentSpan()?.context()
return "${context?.traceId()}-${context?.spanId()}"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.tencent.bkrepo.replication.replica.base.interceptor

import com.tencent.bkrepo.common.api.constant.B3_TRACE
import com.tencent.bkrepo.common.service.otel.util.TraceHeaderUtils
import okhttp3.Interceptor
import okhttp3.Response

class TraceInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val newRequest = request.newBuilder().header(B3_TRACE, TraceHeaderUtils.buildB3Header()).build()
return chain.proceed(newRequest)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import com.tencent.bkrepo.replication.pojo.task.ReplicaTaskDetail
import com.tencent.bkrepo.replication.pojo.task.objects.ReplicaObjectInfo
import com.tencent.bkrepo.replication.replica.base.interceptor.RetryInterceptor
import com.tencent.bkrepo.replication.replica.base.interceptor.SignInterceptor
import com.tencent.bkrepo.replication.replica.base.interceptor.TraceInterceptor
import com.tencent.bkrepo.replication.replica.replicator.Replicator
import com.tencent.bkrepo.replication.replica.replicator.commitedge.CenterClusterReplicator
import com.tencent.bkrepo.replication.replica.replicator.commitedge.CenterRemoteReplicator
Expand Down Expand Up @@ -129,7 +130,8 @@ class ReplicaContext(
writeTimeout,
closeTimeout,
BasicAuthInterceptor(cluster.username!!, cluster.password!!),
RetryInterceptor()
RetryInterceptor(),
TraceInterceptor()
)
} else {
OkHttpClientPool.getHttpClient(
Expand All @@ -139,7 +141,8 @@ class ReplicaContext(
writeTimeout,
closeTimeout,
SignInterceptor(cluster),
RetryInterceptor()
RetryInterceptor(),
TraceInterceptor()
)
}
}
Expand Down

0 comments on commit 8c89ae3

Please sign in to comment.