aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/kamon/context/HttpPropagation.scala
blob: d53a62509a1ed1521057370c8565bcf3a6463978 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package kamon
package context

import com.typesafe.config.Config
import org.slf4j.LoggerFactory

import scala.reflect.ClassTag
import scala.util.control.NonFatal
import scala.util.{Failure, Success}

/**
  * Context Propagation for HTTP transports. When using HTTP transports all the context related information is
  * read from and written to HTTP headers. The context information may be included in the following directions:
  *   - Incoming: Used for HTTP requests coming into this service. Implicitly used when using HttpPropagation.read.
  *   - Outgoing: Used for HTTP requests leaving this service.
  *   - Returning: Used for HTTP responses send back to clients of this service.
  */
trait HttpPropagation {

  /**
    * Uses the provided [[HttpPropagation.HeaderReader]] to read as many HTTP Headers as necessary and create a
    * [[Context]] instance. The way in which context tags and entries are read from and written to HTTP Headers is
    * implementation specific.
    *
    * @param reader Wrapper on the HTTP message from which headers are read.
    * @return The decoded Context instance. If no entries or tags could be read from the HTTP message then an
    *          empty context is returned instead.
    */
  def readContext(reader: HttpPropagation.HeaderReader): Context

  /**
    * Writes the tags and entries from the supplied context using the supplied [[HttpPropagation.HeaderWriter]]
    * instance. The way in which context tags and entries are read from and written to HTTP Headers is implementation
    * specific.
    *
    * Implementations are expected to produce side effects on the wrapped HTTP Messages.
    *
    * @param context Context instance to be written.
    * @param writer Wrapper on the HTTP message that will carry the context headers.
    * @param direction Write direction. It can be either Outgoing or Returning.
    */
  def writeContext(context: Context, writer: HttpPropagation.HeaderWriter, direction: HttpPropagation.Direction.Write): Unit

}

object HttpPropagation {

  /**
    * Encapsulates logic required to read a single context entry from HTTP headers. Implementations of this trait
    * must be aware of the entry they are able to read and the HTTP headers required to do so.
    */
  trait EntryReader {

    /**
      * Tries to read a context entry from HTTP headers. If a context entry is successfully read, implementations
      * must return an updated context instance that includes such entry. If no entry could be read simply return
      * context instance that was passed in, untouched.
      *
      * @param reader Wrapper on the HTTP message from which headers are read.
      * @param context Current context.
      * @return Either the original context passed in or a modified version of it, including the read entry.
      */
    def readEntry(reader: HttpPropagation.HeaderReader, context: Context): Context
  }

  /**
    * Encapsulates logic required to write a single context entry to HTTP headers. Implementations of this trait
    * must be aware of the entry they are able to write and the HTTP headers required to do so.
    */
  trait EntryWriter {

    /**
      * Tries to write a context entry into HTTP headers.
      *
      * @param context The context from which entries should be written.
      * @param writer Wrapper on the HTTP message that will carry the context headers.
      * @param direction Write direction. It can be either Outgoing or Returning.
      */
    def writeEntry(context: Context, writer: HttpPropagation.HeaderWriter, direction: Direction.Write): Unit
  }


  /**
    * Wrapper that reads HTTP headers from HTTP a message.
    */
  trait HeaderReader {

    /**
      * Reads an HTTP header value
      *
      * @param header HTTP header name
      * @return The HTTP header value, if present.
      */
    def readHeader(header: String): Option[String]
  }

  /**
    * Wrapper that writes HTTP headers to a HTTP message.
    */
  trait HeaderWriter {

    /**
      * Writes a HTTP header into a HTTP message.
      *
      * @param header HTTP header name.
      * @param value HTTP header value.
      */
    def writeHeader(header: String, value: String): Unit
  }


  /**
    * Create a new default HttpPropagation instance from the provided configuration.
    *
    * @param config HTTP propagation channel configuration
    * @return A newly constructed HttpPropagation instance.
    */
  def from(config: Config, classLoading: ClassLoading): HttpPropagation = {
    new HttpPropagation.Default(Components.from(config, classLoading))
  }

  /**
    * Default HTTP Propagation in Kamon.
    */
  final class Default(components: Components) extends HttpPropagation {
    private val log = LoggerFactory.getLogger(classOf[HttpPropagation.Default])

    /**
      * Reads context tags and entries on the following order:
      *   - Read all context tags from the context tags header.
      *   - Read all context tags with explicit mappings. This overrides any tag from the previous step in case
      *     of a tag key clash.
      *   - Read all context entries using the incoming entries configuration.
      */
    override def readContext(reader: HeaderReader): Context = {
      val tags = Map.newBuilder[String, String]

      // Tags encoded together in the context tags header.
      try {
        reader.readHeader(components.tagsHeaderName).foreach { contextTagsHeader =>
          contextTagsHeader.split(";").foreach(tagData => {
            val tagPair = tagData.split("=")
            if (tagPair.length == 2) {
              tags += (tagPair(0) -> tagPair(1))
            }
          })
        }
      } catch {
        case NonFatal(t) => log.warn("Failed to read the context tags header", t.asInstanceOf[Any])
      }

      // Tags explicitly mapped on the tags.mappings configuration.
      components.tagsMappings.foreach {
        case (tagName, httpHeader) =>
          try {
            reader.readHeader(httpHeader).foreach(tagValue => tags += (tagName -> tagValue))
          } catch {
            case NonFatal(t) => log.warn("Failed to read mapped tag [{}]", tagName, t.asInstanceOf[Any])
          }
      }

      // Incoming Entries
      components.incomingEntries.foldLeft(Context.of(tags.result())) {
        case (context, (entryName, entryDecoder)) =>
          var result = context
          try {
            result = entryDecoder.readEntry(reader, context)
          } catch {
            case NonFatal(t) => log.warn("Failed to read entry [{}]", entryName.asInstanceOf[Any], t.asInstanceOf[Any])
          }

          result
      }
    }

    /**
      * Writes context tags and entries
      */
    override def writeContext(context: Context, writer: HeaderWriter, direction: Direction.Write): Unit = {
      val keys = direction match {
        case Direction.Outgoing => components.outgoingEntries
        case Direction.Returning => components.returningEntries
      }

      val contextTagsHeader = new StringBuilder()
      def appendTag(key: String, value: String): Unit = {
        contextTagsHeader
          .append(key)
          .append('=')
          .append(value)
          .append(';')
      }

      // Write tags with specific mappings or append them to the context tags header.
      context.tags.foreach {
        case (tagKey, tagValue) => components.tagsMappings.get(tagKey) match {
          case Some(mappedHeader) => writer.writeHeader(mappedHeader, tagValue)
          case None => appendTag(tagKey, tagValue)
        }
      }

      // Write the context tags header.
      if(contextTagsHeader.nonEmpty) {
        writer.writeHeader(components.tagsHeaderName, contextTagsHeader.result())
      }

      // Write entries for the specified direction.
      keys.foreach {
        case (entryName, entryWriter) =>
          try {
            entryWriter.writeEntry(context, writer, direction)
          } catch {
            case NonFatal(t) => log.warn("Failed to write entry [{}] due to: {}", entryName.asInstanceOf[Any], t.asInstanceOf[Any])
          }
      }
    }
  }

  /**
    * Propagation direction. Used to decide whether incoming, outgoing or returning keys must be used to
    * propagate context.
    */
  sealed trait Direction
  object Direction {

    /**
      * Marker trait for all directions that require write operations.
      */
    sealed trait Write

    /**
      * Requests coming into this service.
      */
    case object Incoming extends Direction

    /**
      * Requests going from this service to others.
      */
    case object Outgoing extends Direction with Write

    /**
      * Responses sent from this service to clients.
      */
    case object Returning extends Direction with Write
  }


  case class Components(
    tagsHeaderName: String,
    tagsMappings: Map[String, String],
    incomingEntries: Map[String, HttpPropagation.EntryReader],
    outgoingEntries: Map[String, HttpPropagation.EntryWriter],
    returningEntries: Map[String, HttpPropagation.EntryWriter]
  )

  object Components {
    private val log = LoggerFactory.getLogger(classOf[HttpPropagation.Components])

    def from(config: Config, classLoading: ClassLoading): Components = {
      def buildInstances[ExpectedType : ClassTag](mappings: Map[String, String]): Map[String, ExpectedType] = {
        val entryReaders = Map.newBuilder[String, ExpectedType]

        mappings.foreach {
          case (contextKey, readerClass) => classLoading.createInstance[ExpectedType](readerClass, Nil) match {
            case Success(readerInstance) => entryReaders += (contextKey -> readerInstance)
            case Failure(exception) => log.warn("Failed to instantiate {} [{}] due to []",
              implicitly[ClassTag[ExpectedType]].runtimeClass.getName, readerClass, exception)
          }
        }

        entryReaders.result()
      }

      val tagsHeaderName = config.getString("tags.header-name")
      val tagsMappings = config.getConfig("tags.mappings").pairs
      val incomingEntries = buildInstances[HttpPropagation.EntryReader](config.getConfig("entries.incoming").pairs)
      val outgoingEntries = buildInstances[HttpPropagation.EntryWriter](config.getConfig("entries.outgoing").pairs)
      val returningEntries = buildInstances[HttpPropagation.EntryWriter](config.getConfig("entries.returning").pairs)

      Components(tagsHeaderName, tagsMappings, incomingEntries, outgoingEntries, returningEntries)
    }
  }

}