Skip to content

Commit 60c0446

Browse files
Add TraceParent extension (#146)
* Add TraceParent extension and test * Add test for traceparent as extension of IQ, and fix name in parsing tests
1 parent c47d314 commit 60c0446

4 files changed

Lines changed: 155 additions & 0 deletions

File tree

src/main/java/org/jitsi/xmpp/extensions/colibri2/IqProviderUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,5 +183,9 @@ private static void doRegisterProviders()
183183
ProviderManager.addExtensionProvider(Colibri2Error.ELEMENT,
184184
Colibri2Error.NAMESPACE,
185185
new Colibri2Error.Provider());
186+
187+
ProviderManager.addExtensionProvider(TraceParent.ELEMENT,
188+
TraceParent.NAMESPACE,
189+
new TraceParentProvider());
186190
}
187191
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright @ 2023 - present 8x8, 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 org.jitsi.xmpp.extensions
17+
18+
import org.jivesoftware.smack.packet.XmlEnvironment
19+
import org.jivesoftware.smack.parsing.SmackParsingException
20+
import org.jivesoftware.smack.provider.ExtensionElementProvider
21+
import org.jivesoftware.smack.xml.XmlPullParser
22+
import org.jivesoftware.smack.xml.XmlPullParserException
23+
import java.io.IOException
24+
25+
class TraceParent(val traceId: String, val parentId: String, val traceFlags: String) :
26+
AbstractPacketExtension(NAMESPACE, ELEMENT) {
27+
init {
28+
setAttribute(TRACE_ID_ATTR_NAME, traceId)
29+
setAttribute(PARENT_ID_ATTR_NAME, parentId)
30+
setAttribute(TRACE_FLAGS_ATTR_NAME, traceFlags)
31+
}
32+
33+
companion object {
34+
const val ELEMENT = "traceparent"
35+
const val NAMESPACE = "jitsi:opentelemetry"
36+
const val TRACE_ID_ATTR_NAME = "trace_id"
37+
const val PARENT_ID_ATTR_NAME = "parent_id"
38+
const val TRACE_FLAGS_ATTR_NAME = "trace_flags"
39+
}
40+
}
41+
42+
class TraceParentProvider : ExtensionElementProvider<TraceParent>() {
43+
@Throws(XmlPullParserException::class, IOException::class, SmackParsingException::class)
44+
override fun parse(parser: XmlPullParser, depth: Int, xml: XmlEnvironment?): TraceParent {
45+
val traceId = parser.getAttributeValue("", TraceParent.TRACE_ID_ATTR_NAME)
46+
?: throw SmackParsingException.RequiredAttributeMissingException(
47+
"Missing '${TraceParent.TRACE_ID_ATTR_NAME}' attribute"
48+
)
49+
val parentId = parser.getAttributeValue("", TraceParent.PARENT_ID_ATTR_NAME)
50+
?: throw SmackParsingException.RequiredAttributeMissingException(
51+
"Missing '${TraceParent.PARENT_ID_ATTR_NAME}' attribute"
52+
)
53+
val traceFlags = parser.getAttributeValue("", TraceParent.TRACE_FLAGS_ATTR_NAME)
54+
?: throw SmackParsingException.RequiredAttributeMissingException(
55+
"Missing '${TraceParent.TRACE_FLAGS_ATTR_NAME}' attribute"
56+
)
57+
return TraceParent(traceId, parentId, traceFlags)
58+
}
59+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright @ 2024 - present 8x8, 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 org.jitsi.xmpp.extensions
17+
18+
import io.kotest.assertions.throwables.shouldThrow
19+
import io.kotest.core.spec.style.ShouldSpec
20+
import io.kotest.matchers.shouldBe
21+
import org.jitsi.xmpp.extensions.colibri2.IqProviderUtils
22+
import org.jivesoftware.smack.parsing.SmackParsingException
23+
import org.jivesoftware.smack.util.PacketParserUtils
24+
25+
class TraceParentTest : ShouldSpec() {
26+
init {
27+
IqProviderUtils.registerProviders()
28+
val provider = TraceParentProvider()
29+
30+
context("Parsing a valid extension") {
31+
val traceParent = provider.parse(
32+
PacketParserUtils.getParserFor(
33+
"<traceparent trace_id='1586bb16ccd4475a9f494bf43563ce28' " +
34+
"parent_id='6e39899d78bfa0ae' trace_flags='35'/>"
35+
)
36+
)
37+
38+
traceParent.traceId shouldBe "1586bb16ccd4475a9f494bf43563ce28"
39+
traceParent.parentId shouldBe "6e39899d78bfa0ae"
40+
traceParent.traceFlags shouldBe "35"
41+
}
42+
43+
context("Parsing with missing traceId") {
44+
shouldThrow<SmackParsingException> {
45+
provider.parse(
46+
PacketParserUtils.getParserFor(
47+
"<traceparent parent_id='6e39899d78bfa0ae' trace_flags='35'/>"
48+
)
49+
)
50+
}
51+
}
52+
53+
context("Parsing with missing parentId") {
54+
shouldThrow<SmackParsingException> {
55+
provider.parse(
56+
PacketParserUtils.getParserFor(
57+
"<traceparent trace_id='1586bb16ccd4475a9f494bf43563ce28' trace_flags='35'/>"
58+
)
59+
)
60+
}
61+
}
62+
63+
context("Parsing with missing traceFlags") {
64+
shouldThrow<SmackParsingException> {
65+
provider.parse(
66+
PacketParserUtils.getParserFor(
67+
"<traceparent trace_id='1586bb16ccd4475a9f494bf43563ce28' " +
68+
"parent_id='6e39899d78bfa0ae'/>"
69+
)
70+
)
71+
}
72+
}
73+
}
74+
}

src/test/kotlin/org/jitsi/xmpp/extensions/colibri2/ConferenceModifyIQTest.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ package org.jitsi.xmpp.extensions.colibri2
1818
import io.kotest.assertions.throwables.shouldThrow
1919
import io.kotest.core.spec.style.ShouldSpec
2020
import io.kotest.matchers.shouldBe
21+
import io.kotest.matchers.shouldNotBe
2122
import org.jitsi.xmpp.extensions.IQUtils
23+
import org.jitsi.xmpp.extensions.TraceParent
2224
import org.jivesoftware.smack.parsing.SmackParsingException
2325

2426
class ConferenceModifyIQTest : ShouldSpec() {
2527
val provider = ConferenceModifyIQProvider()
2628
init {
29+
IqProviderUtils.registerProviders()
30+
2731
context("Parse a simple IQ correctly") {
2832
val conferenceModifyIQ = IQUtils.parse(
2933
"""
@@ -59,5 +63,19 @@ class ConferenceModifyIQTest : ShouldSpec() {
5963
)
6064
}
6165
}
66+
context("Parse IQ with TraceParent extension") {
67+
val conferenceModifyIQ = IQUtils.parse(
68+
"""
69+
<iq type='get' from='example.com' to='example.com'>
70+
<conference-modify xmlns='http://jitsi.org/protocol/colibri2' meeting-id='abc'>
71+
<traceparent xmlns='jitsi:opentelemetry' trace_id='1586bb16ccd4475a9f494bf43563ce28'
72+
parent_id='6e39899d78bfa0ae' trace_flags='35'/>
73+
</conference-modify>
74+
</iq>
75+
""".trimIndent(),
76+
provider
77+
)
78+
conferenceModifyIQ.getExtension(TraceParent::class.java) shouldNotBe null
79+
}
6280
}
6381
}

0 commit comments

Comments
 (0)