aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/kamon/context/Propagation.scala
blob: 1d973ca9333f66f23bbec9b0416ca71e8c74958a (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-2018 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.context

/**
  * Inter-process Context propagation interface. Implementations are responsible from reading and writing a lossless
  * representation of a given Context instance to the appropriate mediums. Out of the box, Kamon ships with two
  * implementations:
  *
  *   * HttpPropagation: Uses HTTP headers as the medium to transport the Context data.
  *   * BinaryPropagation: Uses byte streams as the medium to transport the Context data.
  *
  */
trait Propagation[ReaderMedium, WriterMedium] {

  /**
    * Attempts to read a Context from the [[ReaderMedium]].
    *
    * @param medium An abstraction the reads data from the medium transporting the Context data.
    * @return The decoded Context instance or an empty Context if no entries or tags could be read from the medium.
    */
  def read(medium: ReaderMedium): Context

  /**
    * Attempts to write a Context instance to the [[WriterMedium]].
    *
    * @param context Context instance to be written.
    * @param medium An abstraction that writes data into the medium that will transport the Context data.
    */
  def write(context: Context, medium: WriterMedium): Unit

}

object Propagation {

  /**
    * Encapsulates logic required to read a single context entry from a medium. Implementations of this trait
    * must be aware of the entry they are able to read.
    */
  trait EntryReader[Medium] {

    /**
      * Tries to read a context entry from the medium. 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 the
      * context instance that was passed in, unchanged.
      *
      * @param medium  An abstraction the reads data from the medium transporting the Context data.
      * @param context Current context.
      * @return Either the original context passed in or a modified version of it, including the read entry.
      */
    def read(medium: Medium, context: Context): Context
  }

  /**
    * Encapsulates logic required to write a single context entry to the medium. Implementations of this trait
    * must be aware of the entry they are able to write.
    */
  trait EntryWriter[Medium] {

    /**
      * Tries to write a context entry into the medium.
      *
      * @param context   The context from which entries should be written.
      * @param medium    An abstraction that writes data into the medium that will transport the Context data.
      */
    def write(context: Context, medium: Medium): Unit
  }
}