Skip to content

Commit

Permalink
feat: 支持详细下载失败日志 TencentBlueKing#2326
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlkl committed Jul 2, 2024
1 parent 0f616af commit 4a08368
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ abstract class AbstractArtifactRepository : ArtifactRepository {
val range = HeaderUtils.getHeader(HttpHeaders.RANGE) ?: StringPool.DASH
logger.warn(
"User[$principal],ip[$clientAddress] download artifact[$artifactInfo] failed[$code]$message" +
" X_FORWARDED_FOR: $xForwardedFor, range: $range"
" X_FORWARDED_FOR: $xForwardedFor, range: $range", exception
)
ArtifactMetrics.getDownloadFailedCounter().increment()
} catch (exception: ArtifactNotFoundException){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

package com.tencent.bkrepo.common.artifact.resolve.response

import com.sun.org.slf4j.internal.LoggerFactory
import com.tencent.bkrepo.common.api.constant.HttpHeaders
import com.tencent.bkrepo.common.api.constant.HttpStatus
import com.tencent.bkrepo.common.api.exception.TooManyRequestsException
Expand Down Expand Up @@ -115,10 +116,13 @@ abstract class AbstractArtifactResourceHandler(
// org.springframework.http.converter.HttpMessageNotWritableException异常,会重定向到/error页面
// 又因为/error页面不存在,最终返回404,所以要对IOException进行包装,在上一层捕捉处理
val message = exception.message.orEmpty()
val status = if (IOExceptionUtils.isClientBroken(exception))
val status = if (IOExceptionUtils.isClientBroken(exception)){
HttpStatus.BAD_REQUEST
else
}
else {
logger.warn("write range stream failed", exception)
HttpStatus.INTERNAL_SERVER_ERROR
}
throw ArtifactResponseException(message, status)
}
}
Expand All @@ -131,4 +135,7 @@ abstract class AbstractArtifactResourceHandler(
return if (isRangeRequest) HttpStatus.PARTIAL_CONTENT.value else HttpStatus.OK.value
}

companion object {
private val logger = LoggerFactory.getLogger(AbstractArtifactResourceHandler::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

package com.tencent.bkrepo.common.artifact.resolve.response

import com.sun.org.slf4j.internal.LoggerFactory
import com.tencent.bkrepo.common.api.constant.HttpHeaders
import com.tencent.bkrepo.common.api.constant.HttpStatus
import com.tencent.bkrepo.common.api.constant.MediaTypes
Expand Down Expand Up @@ -198,7 +199,12 @@ open class DefaultArtifactResourceWriter(
}
} catch (exception: IOException) {
val message = exception.message.orEmpty()
val status = if (isClientBroken(exception)) HttpStatus.BAD_REQUEST else HttpStatus.INTERNAL_SERVER_ERROR
val status = if (isClientBroken(exception)) {
HttpStatus.BAD_REQUEST
} else {
logger.warn("write zip stream failed", exception)
HttpStatus.INTERNAL_SERVER_ERROR
}
throw ArtifactResponseException(message, status)
} finally {
resource.artifactMap.values.forEach { it.closeQuietly() }
Expand Down Expand Up @@ -232,6 +238,7 @@ open class DefaultArtifactResourceWriter(
}

companion object {
private val logger = LoggerFactory.getLogger(DefaultArtifactResourceWriter::class.java)
private val binaryMediaTypes = setOf(MediaTypes.APPLICATION_APK)
}
}

0 comments on commit 4a08368

Please sign in to comment.