aboutsummaryrefslogtreecommitdiff
path: root/kamon-annotation/src/main/scala/kamon/annotation/instrumentation/AnnotationInstrumentation.scala
blob: 381aeb722965fe7f8f2de17c0b02b1bdfcc068f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
 * =========================================================================================
 * Copyright © 2013-2015 the kamon project <http://kamon.io/>
 *
 * 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 kamon.annotation.instrumentation

import kamon.Kamon
import kamon.annotation._
import kamon.metric.instrument
import kamon.metric.instrument.{ MinMaxCounter, Counter }
import org.aspectj.lang.annotation._
import org.aspectj.lang.{ JoinPoint, ProceedingJoinPoint }
import java.util.concurrent.atomic.AtomicReferenceArray

@Aspect
class AnnotationInstrumentation extends BaseAnnotationInstrumentation {

  @After("execution((@kamon.annotation.EnableKamon AnnotationInstruments+).new(..)) && this(obj)")
  def creation(jps: JoinPoint.StaticPart, obj: AnnotationInstruments): Unit = {
    val size = Kamon(Annotation).arraySize
    obj.traces = new AtomicReferenceArray[TraceContextInfo](size)
    obj.segments = new AtomicReferenceArray[SegmentInfo](size)
    obj.counters = new AtomicReferenceArray[Counter](size)
    obj.minMaxCounters = new AtomicReferenceArray[MinMaxCounter](size)
    obj.histograms = new AtomicReferenceArray[instrument.Histogram](size)
  }

  @Around("execution(@kamon.annotation.Trace !static * (@kamon.annotation.EnableKamon AnnotationInstruments+).*(..)) && this(obj)")
  def trace(pjp: ProceedingJoinPoint, obj: AnnotationInstruments): AnyRef = {
    var traceInfo = obj.traces.get(pjp.getStaticPart.getId)
    if (traceInfo == null) traceInfo = registerTrace(pjp.getStaticPart, obj.traces, StringEvaluator(obj), TagsEvaluator(obj))
    processTrace(traceInfo, pjp)
  }

  @Around("execution(@kamon.annotation.Segment !static * (@kamon.annotation.EnableKamon AnnotationInstruments+).*(..)) && this(obj)")
  def segment(pjp: ProceedingJoinPoint, obj: AnnotationInstruments): AnyRef = {
    var segmentInfo = obj.segments.get(pjp.getStaticPart.getId)
    if (segmentInfo == null) segmentInfo = registerSegment(pjp.getStaticPart, obj.segments, StringEvaluator(obj), TagsEvaluator(obj))
    processSegment(segmentInfo, pjp)
  }

  @Around("execution(@kamon.annotation.Time !static * (@kamon.annotation.EnableKamon AnnotationInstruments+).*(..)) && this(obj)")
  def time(pjp: ProceedingJoinPoint, obj: AnnotationInstruments): AnyRef = {
    var histogram = obj.histograms.get(pjp.getStaticPart.getId)
    if (histogram == null) histogram = registerTime(pjp.getStaticPart, obj.histograms, StringEvaluator(obj), TagsEvaluator(obj))
    processTime(histogram, pjp)
  }

  @Around("execution(@kamon.annotation.Count !static * (@kamon.annotation.EnableKamon AnnotationInstruments+).*(..)) && this(obj)")
  def count(pjp: ProceedingJoinPoint, obj: AnnotationInstruments): AnyRef = {
    var counter = obj.counters.get(pjp.getStaticPart.getId)
    if (counter == null) counter = registerCounter(pjp.getStaticPart, obj.counters, StringEvaluator(obj), TagsEvaluator(obj))
    processCount(counter, pjp)
  }

  @Around("execution(@kamon.annotation.MinMaxCount !static * (@kamon.annotation.EnableKamon AnnotationInstruments+).*(..)) && this(obj)")
  def minMax(pjp: ProceedingJoinPoint, obj: AnnotationInstruments): AnyRef = {
    var minMax = obj.minMaxCounters.get(pjp.getStaticPart.getId)
    if (minMax == null) minMax = registerMinMaxCounter(pjp.getStaticPart, obj.minMaxCounters, StringEvaluator(obj), TagsEvaluator(obj))
    processMinMax(minMax, pjp)
  }

  @AfterReturning(pointcut = "execution(@kamon.annotation.Histogram !static (int || long || double || float) (@kamon.annotation.EnableKamon AnnotationInstruments+).*(..)) && this(obj)", returning = "result")
  def histogram(jps: JoinPoint.StaticPart, obj: AnnotationInstruments, result: AnyRef): Unit = {
    var histogram = obj.histograms.get(jps.getId)
    if (histogram == null) histogram = registerHistogram(jps, obj.histograms, StringEvaluator(obj), TagsEvaluator(obj))
    processHistogram(histogram, result, jps)
  }
}