aboutsummaryrefslogtreecommitdiff
path: root/metrics/brave-backend/src/test/scala/com/softwaremill/sttp/brave/BraveBackendTest.scala
blob: ff9ac31378776c32609eb8ef3d81b34ee7ac73f3 (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
package com.softwaremill.sttp.brave

import brave.http.HttpTracing
import brave.test.http.ITHttpClient
import brave.internal.HexCodec
import com.softwaremill.sttp._
import okhttp3.mockwebserver.MockResponse
import org.scalatest.{BeforeAndAfter, FlatSpec, Matchers}
import zipkin2.Span

class BraveBackendTest extends FlatSpec with Matchers with BeforeAndAfter {

  // test proxy - contains the brave instrumentation tests
  private var t: ITHttpClient[SttpBackend[Id, Nothing]] = null

  // we need to extract these protected ITHttpClient members to use in the custom test
  private var _backend: SttpBackend[Id, Nothing] = null
  private var _httpTracing: HttpTracing = null
  private var _takeSpan: () => Span = null

  def newT(): Unit = {
    t = new ITHttpClient[SttpBackend[Id, Nothing]]() {
      override def post(client: SttpBackend[Id, Nothing], pathIncludingQuery: String, body: String): Unit = {
        client.send(sttp.post(uri"${url(pathIncludingQuery)}").body(body))
      }

      override def get(client: SttpBackend[Id, Nothing], pathIncludingQuery: String): Unit = {
        client.send(sttp.get(uri"${url(pathIncludingQuery)}"))
      }

      override def closeClient(client: SttpBackend[Id, Nothing]): Unit =
        client.close()

      override def newClient(port: Int): SttpBackend[Id, Nothing] = {
        _backend = BraveBackend[Id, Nothing](HttpURLConnectionBackend(), httpTracing)
        _httpTracing = httpTracing
        _takeSpan = () => takeSpan()

        _backend
      }
    }
  }

  before {
    newT()
    t.setup()
  }

  after {
    t.close()
    t.server.shutdown()
  }

  it should "propagatesSpan" in {
    t.propagatesSpan()
  }

  it should "makesChildOfCurrentSpan" in {
    t.makesChildOfCurrentSpan()
  }

  it should "propagatesExtra_newTrace" in {
    t.propagatesExtra_newTrace()
  }

  it should "propagatesExtra_unsampledTrace" in {
    t.propagatesExtra_unsampledTrace()
  }

  it should "propagates_sampledFalse" in {
    t.propagates_sampledFalse()
  }

  it should "customSampler" in {
    t.customSampler()
  }

  it should "reportsClientKindToZipkin" in {
    t.reportsClientKindToZipkin()
  }

  it should "reportsServerAddress" in {
    t.reportsServerAddress()
  }

  it should "defaultSpanNameIsMethodName" in {
    t.defaultSpanNameIsMethodName()
  }

  it should "supportsPortableCustomization" in {
    t.supportsPortableCustomization()
  }

  it should "addsStatusCodeWhenNotOk" in {
    t.addsStatusCodeWhenNotOk()
  }

  it should "redirect" in {
    t.redirect()
  }

  it should "post" in {
    t.post()
  }

// these tests take a very long time to complete, but pass last time I checked

//  it should "reportsSpanOnTransportException" in {
//    t.reportsSpanOnTransportException()
//  }

//  it should "addsErrorTagOnTransportException" in {
//    t.addsErrorTagOnTransportException()
//  }

  it should "httpPathTagExcludesQueryParams" in {
    t.httpPathTagExcludesQueryParams()
  }

  it should "use the tracing context from tags if available" in {
    val tracer = _httpTracing.tracing.tracer
    t.server.enqueue(new MockResponse)

    val parent = tracer.newTrace.name("test").start
    try {
      import com.softwaremill.sttp.brave.BraveBackend._
      _backend.send(
        sttp
          .get(uri"http://127.0.0.1:${t.server.getPort}/foo")
          .tagWithTraceContext(parent.context()))
    } finally parent.finish()

    val request = t.server.takeRequest
    request.getHeader("x-b3-traceId") should be(parent.context.traceIdString)
    request.getHeader("x-b3-parentspanid") should be(HexCodec.toLowerHex(parent.context.spanId))

    Set(_takeSpan(), _takeSpan()).map(_.kind) should be(Set(null, Span.Kind.CLIENT))
  }
}