Skip to content

Commit 5999fd6

Browse files
authored
Merge pull request #180 from ExpediaDotCom/MethodToReadServiceNameFromServiceTag
Add SpanUtils.getEffectiveServiceName() to read service name from a span's service tag, iff it exists in the tag
2 parents 3c6fcd4 + 86c345b commit 5999fd6

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

reader/src/main/scala/com/expedia/www/haystack/trace/reader/readers/utils/SpanUtils.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ object SpanUtils {
4444
containsLogTag(span, SERVER_RECV_EVENT) && containsLogTag(span, SERVER_SEND_EVENT)
4545
}
4646

47+
def getEffectiveServiceName(span: Span): String = {
48+
if(containsServiceTag(span)) {
49+
val serviceTagValue = span.getTagsList.asScala.find(_.getKey == SERVICE_TAG_KEY).map(_.getVStr).get
50+
if(serviceTagValue == "") { // Span protobuf-generated code returns empty string for a missing tag key
51+
span.getServiceName
52+
} else {
53+
serviceTagValue
54+
}
55+
} else {
56+
span.getServiceName
57+
}
58+
}
59+
60+
def containsServiceTag(span: Span): Boolean = {
61+
containsTag(span, SERVICE_TAG_KEY)
62+
}
63+
4764
def containsClientLogTag(span: Span): Boolean = {
4865
containsLogTag(span, CLIENT_RECV_EVENT) && containsLogTag(span, CLIENT_RECV_EVENT)
4966
}
@@ -92,6 +109,12 @@ object SpanUtils {
92109
})
93110
}
94111

112+
private def containsTag(span: Span, key: String) = {
113+
span.getTagsList.asScala.exists(tag => {
114+
tag.getKey.equalsIgnoreCase(key)
115+
})
116+
}
117+
95118
def createAutoGeneratedRootSpan(spans: Seq[Span],
96119
reason: String,
97120
rootSpanId: String): Span.Builder = {
@@ -133,6 +156,7 @@ object SpanMarkers {
133156
val CLIENT_RECV_EVENT = "cr"
134157

135158
val SPAN_KIND_TAG_KEY = "span.kind"
159+
val SERVICE_TAG_KEY = "service"
136160
val SERVER_SPAN_KIND = "server"
137161
val CLIENT_SPAN_KIND = "client"
138162
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2018 Expedia, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.expedia.www.haystack.trace.reader.readers.utils
17+
18+
import java.lang.System.currentTimeMillis
19+
20+
import com.expedia.open.tracing.Span
21+
import com.expedia.open.tracing.Tag
22+
import com.expedia.www.haystack.trace.reader.readers.utils.SpanMarkers.SERVICE_TAG_KEY
23+
import com.expedia.www.haystack.trace.reader.unit.BaseUnitTestSpec
24+
25+
class SpanUtilsSpec extends BaseUnitTestSpec {
26+
val ServiceNameInAttribute: String = currentTimeMillis + "ServiceNameInAttribute"
27+
val ServiceNameInTag: String = currentTimeMillis + "ServiceNameInTag"
28+
29+
describe("SpanUtils object, getEffectiveServiceName() method") {
30+
it("should return the span's service name if the 'service' tag is not found") {
31+
val span = Span.newBuilder().setServiceName(ServiceNameInAttribute).build
32+
val serviceName = SpanUtils.getEffectiveServiceName(span)
33+
assert(serviceName == ServiceNameInAttribute)
34+
}
35+
36+
it("should return the tag's service name if the 'service' tag has a non-null value") {
37+
val tag = Tag.newBuilder().setKey(SERVICE_TAG_KEY).setVStr(ServiceNameInTag).build
38+
val span = Span.newBuilder().setServiceName(ServiceNameInAttribute).addTags(tag).build
39+
val serviceName = SpanUtils.getEffectiveServiceName(span)
40+
assert(serviceName == ServiceNameInTag)
41+
}
42+
43+
it("should return the span's service name if the 'service' tag has no value") {
44+
val tag = Tag.newBuilder().setKey(SERVICE_TAG_KEY).build
45+
val span = Span.newBuilder().setServiceName(ServiceNameInAttribute).addTags(tag).build
46+
val serviceName = SpanUtils.getEffectiveServiceName(span)
47+
assert(serviceName == ServiceNameInAttribute)
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)