-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1328 from AbsaOSS/temp-branch
Merge release/0.7 to develop
- Loading branch information
Showing
8 changed files
with
162 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...pline/producer/rest/controller/ExecutionPlansControllerMessageLengthCapturingAspect.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2020 ABSA Group Limited | ||
* | ||
* 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 za.co.absa.spline.producer.rest.controller | ||
|
||
import org.aspectj.lang.ProceedingJoinPoint | ||
import org.aspectj.lang.annotation.{Around, Aspect, Pointcut} | ||
import org.slf4s.Logging | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.context.request.{RequestContextHolder, ServletRequestAttributes} | ||
import za.co.absa.spline.producer.model.{ExecutionPlan, v1_1, v1_2} | ||
import za.co.absa.spline.producer.rest.filter.MessageLengthCapturingFilter | ||
|
||
@Aspect | ||
@Component | ||
class ExecutionPlansControllerMessageLengthCapturingAspect extends Logging { | ||
|
||
@Pointcut("execution(public * za.co.absa.spline.producer.rest.controller.*Controller.*(..))") | ||
def publicControllerMethods(): Unit = {} | ||
|
||
@Pointcut("execution(* *(.., za.co.absa.spline.producer.model.ExecutionPlan, ..))") | ||
def acceptsEPv10(): Unit = {} | ||
|
||
@Pointcut("execution(* *(.., za.co.absa.spline.producer.model.v1_1.ExecutionPlan, ..))") | ||
def acceptsEPv11(): Unit = {} | ||
|
||
@Pointcut("execution(* *(.., za.co.absa.spline.producer.model.v1_2.ExecutionPlan, ..))") | ||
def acceptsEPv12(): Unit = {} | ||
|
||
@Around("publicControllerMethods() && (acceptsEPv10() || acceptsEPv11() || acceptsEPv12())") | ||
def aroundAdvice(jp: ProceedingJoinPoint): AnyRef = { | ||
val origArgs = jp.getArgs | ||
val fixedArgs = origArgs.map { | ||
case ep: ExecutionPlan => | ||
ep.copy(extraInfo = withMessageLengthInfo(ep.extraInfo)) | ||
case ep: v1_1.ExecutionPlan => | ||
ep.copy(extraInfo = withMessageLengthInfo(ep.extraInfo)) | ||
case ep: v1_2.ExecutionPlan => | ||
ep.copy(extraInfo = withMessageLengthInfo(ep.extraInfo)) | ||
case x => x | ||
} | ||
jp.proceed(fixedArgs) | ||
} | ||
|
||
private def withMessageLengthInfo(m: Map[String, Any]): Map[String, Any] = { | ||
val req = RequestContextHolder.getRequestAttributes.asInstanceOf[ServletRequestAttributes].getRequest | ||
val counters = MessageLengthCapturingFilter.getCounters(req).toArray | ||
m + ("__spline_msg_size" -> counters.map(_.count)) | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
.../src/main/scala/za/co/absa/spline/producer/rest/filter/MessageLengthCapturingFilter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2020 ABSA Group Limited | ||
* | ||
* 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 za.co.absa.spline.producer.rest.filter | ||
|
||
import za.co.absa.spline.producer.rest.filter.MessageLengthCapturingFilter.{LengthCountingInputStreamWrapper, getCounters} | ||
|
||
import java.io.InputStream | ||
import javax.servlet._ | ||
import javax.servlet.http.HttpServletRequest | ||
import scala.collection.mutable | ||
|
||
class MessageLengthCapturingFilter extends Filter { | ||
override def doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain): Unit = { | ||
val newRequest = request match { | ||
case r: HttpServletRequest => | ||
val inputStreamWrapper = new LengthCountingInputStreamWrapper(r.getInputStream) | ||
getCounters(r) += inputStreamWrapper.lengthCounter | ||
new HttpRequestWrapper(r, inputStreamWrapper) | ||
case _ => request | ||
} | ||
chain.doFilter(newRequest, response) | ||
} | ||
|
||
override def init(config: FilterConfig): Unit = { | ||
// nothing to do here | ||
} | ||
|
||
override def destroy(): Unit = { | ||
// nothing to do here | ||
} | ||
} | ||
|
||
object MessageLengthCapturingFilter { | ||
private val CountersRequestAttributeKey: String = s"${classOf[MessageLengthCapturingFilter].getName}.counters" | ||
|
||
def getCounters(r: ServletRequest): mutable.Buffer[ReadOnlyCounter] = { | ||
val countersAttrOrNull = r.getAttribute(CountersRequestAttributeKey).asInstanceOf[mutable.Buffer[ReadOnlyCounter]] | ||
val counters = Option(countersAttrOrNull).getOrElse(mutable.Buffer.empty[ReadOnlyCounter]) | ||
if (counters.isEmpty) r.setAttribute(CountersRequestAttributeKey, counters) | ||
counters | ||
} | ||
|
||
trait ReadOnlyCounter { | ||
def count: Int | ||
} | ||
|
||
class LengthCountingInputStreamWrapper(r: InputStream) extends InputStream { | ||
private var _bytesReadCount: Int = 0 | ||
|
||
val lengthCounter: ReadOnlyCounter = new ReadOnlyCounter { | ||
override def count: Int = _bytesReadCount | ||
} | ||
|
||
override def read(): Int = { | ||
_bytesReadCount += 1 | ||
r.read() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters