aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/kamon/ContextPropagation.scala
blob: 8f2ed8e4e920290b792f7ea38122a425d5ba901a (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
82
83
84
85
86
87
88
89
90
91
92
package kamon

import com.typesafe.config.Config
import kamon.context.BinaryPropagation.{ByteStreamReader, ByteStreamWriter}
import kamon.context.HttpPropagation.{HeaderReader, HeaderWriter}
import kamon.context.{BinaryPropagation, HttpPropagation, Propagation}

trait ContextPropagation { self: Configuration with ClassLoading =>
  @volatile private var _propagationComponents: ContextPropagation.Components = _
  @volatile private var _defaultHttpPropagation: Propagation[HeaderReader, HeaderWriter] = _
  @volatile private var _defaultBinaryPropagation: Propagation[ByteStreamReader, ByteStreamWriter] = _

  // Initial configuration and reconfigures
  init(self.config)
  self.onReconfigure(newConfig => self.init(newConfig))


  /**
    * Retrieves the HTTP propagation channel with the supplied name. Propagation channels are configured on the
    * kamon.propagation.http configuration section.
    *
    * @param channelName Channel name to retrieve.
    * @return The HTTP propagation, if available.
    */
  def httpPropagation(channelName: String): Option[Propagation[HeaderReader, HeaderWriter]] =
    _propagationComponents.httpChannels.get(channelName)

  /**
    * Retrieves the binary propagation channel with the supplied name. Propagation channels are configured on the
    * kamon.propagation.binary configuration section.
    *
    * @param channelName Channel name to retrieve.
    * @return The binary propagation, if available.
    */
  def binaryPropagation(channelName: String): Option[Propagation[ByteStreamReader, ByteStreamWriter]] =
    _propagationComponents.binaryChannels.get(channelName)

  /**
    * Retrieves the default HTTP propagation channel. Configuration for this channel can be found under the
    * kamon.propagation.http.default configuration section.
    *
    * @return The default HTTP propagation.
    */
  def defaultHttpPropagation(): Propagation[HeaderReader, HeaderWriter] =
    _defaultHttpPropagation

  /**
    * Retrieves the default binary propagation channel. Configuration for this channel can be found under the
    * kamon.propagation.binary.default configuration section.
    *
    * @return The default HTTP propagation.
    */
  def defaultBinaryPropagation(): Propagation[ByteStreamReader, ByteStreamWriter] =
    _defaultBinaryPropagation



  private def init(config: Config): Unit = synchronized {
    _propagationComponents = ContextPropagation.Components.from(self.config, self)
    _defaultHttpPropagation = _propagationComponents.httpChannels(ContextPropagation.DefaultHttpChannel)
    _defaultBinaryPropagation = _propagationComponents.binaryChannels(ContextPropagation.DefaultBinaryChannel)
  }
}

object ContextPropagation {
  val DefaultHttpChannel = "default"
  val DefaultBinaryChannel = "default"

  case class Components(
    httpChannels: Map[String, Propagation[HeaderReader, HeaderWriter]],
    binaryChannels: Map[String, Propagation[ByteStreamReader, ByteStreamWriter]]
  )

  object Components {

    def from(config: Config, classLoading: ClassLoading): Components = {
      val propagationConfig = config.getConfig("kamon.propagation")
      val httpChannelsConfig = propagationConfig.getConfig("http").configurations
      val binaryChannelsConfig = propagationConfig.getConfig("binary").configurations

      val httpChannels = httpChannelsConfig.map {
        case (channelName, channelConfig) => (channelName -> HttpPropagation.from(channelConfig, classLoading))
      }

      val binaryChannels = binaryChannelsConfig.map {
        case (channelName, channelConfig) => (channelName -> BinaryPropagation.from(channelConfig, classLoading))
      }

      Components(httpChannels, binaryChannels)
    }
  }
}