forked from openzipkin/zipkin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Requests.scala
109 lines (87 loc) · 3.79 KB
/
Requests.scala
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
package com.twitter.zipkin.tracegen
/*
* Copyright 2012 Twitter Inc.
*
* 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.
*
*/
import com.twitter.zipkin.gen
import java.nio.ByteBuffer
import com.twitter.logging.Logger
import com.twitter.scrooge.BinaryThriftStructSerializer
import org.apache.thrift.protocol.TBinaryProtocol
import com.twitter.finagle.builder.ClientBuilder
import com.twitter.finagle.thrift.ThriftClientFramedCodec
import scala.{List, Seq}
class Requests(collectorHost: String, collectorPort: Int, queryHost: String, queryPort: Int) {
val log = Logger.get(getClass.getName)
def logTraces(traces: scala.List[gen.Trace]): Unit = {
val protocol = new TBinaryProtocol.Factory()
val service = ClientBuilder()
.hosts(collectorHost + ":" + collectorPort)
.hostConnectionLimit(1)
.codec(ThriftClientFramedCodec())
.build()
val client = new gen.ZipkinCollector.FinagledClient(service, protocol)
val serializer = new BinaryThriftStructSerializer[gen.Span] {
def codec = gen.Span
}
traces.foreach(t => {
t.spans.foreach(s => {
val entries = List(gen.LogEntry("zipkin", serializer.toString(s)))
println("Sending: " + s + ". Response: " + client.log(entries)())
})
})
service.release()
}
def printTrace(traceIds: Seq[Long], client: gen.ZipkinQuery.FinagledClient) {
val traces = client.getTracesByIds(traceIds, List(gen.Adjust.TimeSkew))()
traces.foreach {
trace =>
trace.spans.foreach {
s =>
println("Got span: " + s)
}
}
}
def querySpan(service: String, span: String, annotation: String,
kvAnnotation: (String, ByteBuffer), maxTraces: Int): Unit = {
val protocol = new TBinaryProtocol.Factory()
val serviceClient = ClientBuilder()
.hosts(queryHost + ":" + queryPort)
.hostConnectionLimit(1)
.codec(ThriftClientFramedCodec())
.build()
val client = new gen.ZipkinQuery.FinagledClient(serviceClient, protocol)
println("Querying for service name: " + service + " and span name " + span)
val ts1 = client.getTraceIdsBySpanName(service, span, Long.MaxValue, maxTraces, gen.Order.DurationDesc)()
printTrace(ts1, client)
println("Querying for service name: " + service)
val ts2 = client.getTraceIdsBySpanName(service, "", Long.MaxValue, maxTraces, gen.Order.DurationDesc)()
printTrace(ts2, client)
println("Querying for annotation: " + annotation)
val ts3 = client.getTraceIdsByAnnotation(service, annotation, ByteBuffer.wrap("".getBytes), Long.MaxValue, maxTraces, gen.Order.DurationDesc)()
printTrace(ts3, client)
println("Querying for kv annotation: " + kvAnnotation)
val ts4 = client.getTraceIdsByAnnotation(service, kvAnnotation._1, kvAnnotation._2, Long.MaxValue, maxTraces, gen.Order.DurationDesc)()
printTrace(ts4, client)
val traces = client.getTracesByIds(ts4, List(gen.Adjust.TimeSkew))()
println(traces.toString)
val traceTimeline = client.getTraceTimelinesByIds(ts4, List(gen.Adjust.TimeSkew))()
println(traceTimeline.toString)
println("Data ttl: " + client.getDataTimeToLive()())
println("Service names: " + client.getServiceNames()())
println("Span names for : " + service + " " + client.getSpanNames(service)())
serviceClient.release()
}
}