Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with opentracing keys with dashes/hyphens and JMS. #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright 2017-2019 The OpenTracing Authors
*
* 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 io.opentracing.contrib.spring.integration.messaging;

import io.opentracing.propagation.TextMap;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
* Helper class to extract span context from message properties
*/
public class JmsTextMapExtractAdapter implements TextMap {

private final Map<String, String> map = new HashMap<>();
static final String DASH = "_$dash$_";

public JmsTextMapExtractAdapter(TextMap message) {
if (message == null) {
return;
}
Iterator it = message.iterator();
while (it.hasNext()) {
Map.Entry headerPair = (Map.Entry) it.next();
String headerKey = (String) headerPair.getKey();
Object headerValue = headerPair.getValue();
if (headerValue instanceof String) {
map.put(decodeDash(headerKey), (String) headerValue);
}
}
}

@Override
public Iterator<Map.Entry<String, String>> iterator() {
return map.entrySet().iterator();
}

@Override
public void put(String key, String value) {
throw new UnsupportedOperationException(
"JmsTextMapExtractAdapter should only be used with Tracer.extract()");
}

private String decodeDash(String key) {
return key.replace(DASH, "-");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,21 @@ public Iterator<Map.Entry<String, String>> iterator() {

@Override
public void put(String key, String value) {
headers.put(key, byteHeaders.contains(key) ? value.getBytes() : value);
headers.put(encodeDash(key), byteHeaders.contains(key) ? value.getBytes() : value);
}

public Message<T> getMessage() {
return MessageBuilder.fromMessage(message)
.copyHeaders(headers)
.build();
}

private String encodeDash(String key) {
if (key == null || key.isEmpty()) {
return key;
}

return key.replace("-", JmsTextMapExtractAdapter.DASH);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017-2018 The OpenTracing Authors
* Copyright 2017-2019 The OpenTracing Authors
*
* 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
Expand Down Expand Up @@ -57,7 +57,7 @@ public Message<?> preSend(Message<?> message, MessageChannel channel) {
.withTag(Tags.MESSAGE_BUS_DESTINATION.getKey(), getChannelName(channel));

MessageTextMap<?> carrier = new MessageTextMap<>(message);
SpanContext extractedContext = tracer.extract(Format.Builtin.TEXT_MAP, carrier);
SpanContext extractedContext = tracer.extract(Format.Builtin.TEXT_MAP, new JmsTextMapExtractAdapter(carrier));
if (isConsumer) {
spanBuilder.addReference(References.FOLLOWS_FROM, extractedContext);
} else if (tracer.activeSpan() == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright 2017-2019 The OpenTracing Authors
*
* 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 io.opentracing.contrib.spring.integration.messaging;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;

public class JmsTextTextExtractAdapterTest {

@Test
public void shouldGetIterator() {
Map<String, String> headers = new HashMap<>(2);
headers.put("h1", "v1");
headers.put("h2", "v2");
Message<String> message = MessageBuilder.withPayload("test").copyHeaders(headers).build();
MessageTextMap<String> map = new MessageTextMap<>(message);
JmsTextMapExtractAdapter adapter = new JmsTextMapExtractAdapter(map);
assertThat(adapter.iterator()).containsAll(headers.entrySet());
}

@Test
public void shouldDecodeDash() {
Map<String, String> headers = new HashMap<>(1);
headers.put("uber_$dash$_trace_$dash$_id", "v1");
Map<String, String> chanedHeaders = new HashMap<>(1);
headers.put("uber-trace-id", "v1");
Message<String> message = MessageBuilder.withPayload("test").copyHeaders(headers).build();
MessageTextMap<String> map = new MessageTextMap<>(message);
JmsTextMapExtractAdapter adapter = new JmsTextMapExtractAdapter(map);
assertThat(adapter.iterator()).containsAll(chanedHeaders.entrySet());
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017-2018 The OpenTracing Authors
* Copyright 2017-2019 The OpenTracing Authors
*
* 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
Expand Down Expand Up @@ -55,6 +55,16 @@ public void shouldPutEntry() {
assertThat(map.iterator()).contains(new AbstractMap.SimpleEntry<>("k1", "v1"));
}

@Test
public void shouldEncodeKeyWithDash() {
Message<String> message = MessageBuilder.withPayload("test")
.build();
MessageTextMap<String> map = new MessageTextMap<>(message);
map.put("uber-trace-id", "1435645");

assertThat(map.iterator()).contains(new AbstractMap.SimpleEntry<>("uber_$dash$_trace_$dash$_id", "1435645"));
}

@Test
public void shouldGetMessageWithNewHeaders() {
Message<String> message = MessageBuilder.withPayload("test")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017-2018 The OpenTracing Authors
* Copyright 2017-2019 The OpenTracing Authors
*
* 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
Expand Down Expand Up @@ -122,7 +122,7 @@ public void preSendShouldStartSpanForServerReceivedMessage() {
Message<?> message = interceptor.preSend(originalMessage, mockMessageChannel);
assertThat(message.getPayload()).isEqualTo(originalMessage.getPayload());

verify(mockTracer).extract(eq(Format.Builtin.TEXT_MAP), any(MessageTextMap.class));
verify(mockTracer).extract(eq(Format.Builtin.TEXT_MAP), any(JmsTextMapExtractAdapter.class));
verify(mockTracer).buildSpan(String.format("receive:%s", mockMessageChannel.toString()));
verify(mockSpanBuilder).addReference(References.FOLLOWS_FROM, null);
verify(mockSpanBuilder).startActive(true);
Expand Down