-
Notifications
You must be signed in to change notification settings - Fork 862
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
Implement the text format so that w3c trace context headers are propagated. #601
Closed
jkwatson
wants to merge
1
commit into
open-telemetry:master
from
newrelic-forks:implement_distributed_context_text_format
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
...c/main/java/io/opentelemetry/sdk/distributedcontext/DistributedContextHttpTextFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2019, OpenTelemetry 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.opentelemetry.sdk.distributedcontext; | ||
|
||
import io.opentelemetry.context.propagation.HttpTextFormat; | ||
import io.opentelemetry.distributedcontext.DistributedContext; | ||
import io.opentelemetry.distributedcontext.Entry; | ||
import io.opentelemetry.distributedcontext.EntryKey; | ||
import io.opentelemetry.distributedcontext.EntryValue; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** W3C Trace Context implementation of the HttpTextFormat for a DistributedContext. */ | ||
class DistributedContextHttpTextFormat implements HttpTextFormat<DistributedContext> { | ||
static final String TRACEPARENT = "traceparent"; | ||
static final String TRACESTATE = "tracestate"; | ||
private static final List<String> FIELDS = | ||
Collections.unmodifiableList(Arrays.asList(TRACEPARENT, TRACESTATE)); | ||
|
||
private static final DistributedContextHttpTextFormat INSTANCE = | ||
new DistributedContextHttpTextFormat(); | ||
|
||
public static DistributedContextHttpTextFormat getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public List<String> fields() { | ||
return FIELDS; | ||
} | ||
|
||
@Override | ||
public <C> void inject(DistributedContext value, C carrier, Setter<C> setter) { | ||
for (String field : fields()) { | ||
final EntryValue entryValue = value.getEntryValue(EntryKey.create(field)); | ||
if (entryValue != null) { | ||
setter.put(carrier, field, entryValue.asString()); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public <C> DistributedContext extract(C carrier, Getter<C> getter) { | ||
final DistributedContextSdk.Builder builder = new DistributedContextSdk.Builder(); | ||
for (String field : fields()) { | ||
final String value = getter.get(carrier, field); | ||
if (value != null) { | ||
builder.put( | ||
EntryKey.create(field), EntryValue.create(value), Entry.METADATA_UNLIMITED_PROPAGATION); | ||
} | ||
} | ||
return builder.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
...st/java/io/opentelemetry/sdk/distributedcontext/DistributedContextHttpTextFormatTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* Copyright 2019, OpenTelemetry 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.opentelemetry.sdk.distributedcontext; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import io.opentelemetry.context.propagation.HttpTextFormat; | ||
import io.opentelemetry.distributedcontext.DistributedContext; | ||
import io.opentelemetry.distributedcontext.Entry; | ||
import io.opentelemetry.distributedcontext.EntryKey; | ||
import io.opentelemetry.distributedcontext.EntryValue; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import javax.annotation.Nullable; | ||
import org.junit.Test; | ||
|
||
public class DistributedContextHttpTextFormatTest { | ||
|
||
@Test | ||
public void inject() { | ||
DistributedContextHttpTextFormat httpTextFormat = | ||
DistributedContextHttpTextFormat.getInstance(); | ||
|
||
Map<String, String> dataCarrier = new HashMap<>(); | ||
DistributedContext distributedContext = | ||
new DistributedContextSdk.Builder() | ||
.put( | ||
EntryKey.create("foo"), | ||
EntryValue.create("bar"), | ||
Entry.METADATA_UNLIMITED_PROPAGATION) | ||
.put( | ||
EntryKey.create(DistributedContextHttpTextFormat.TRACEPARENT), | ||
EntryValue.create("traceParentData"), | ||
Entry.METADATA_UNLIMITED_PROPAGATION) | ||
.put( | ||
EntryKey.create(DistributedContextHttpTextFormat.TRACESTATE), | ||
EntryValue.create("traceStateData"), | ||
Entry.METADATA_UNLIMITED_PROPAGATION) | ||
.build(); | ||
|
||
httpTextFormat.inject( | ||
distributedContext, | ||
dataCarrier, | ||
new HttpTextFormat.Setter<Map<String, String>>() { | ||
@Override | ||
public void put(Map<String, String> carrier, String key, String value) { | ||
carrier.put(key, value); | ||
} | ||
}); | ||
|
||
assertEquals(2, dataCarrier.size()); | ||
assertEquals("traceParentData", dataCarrier.get(DistributedContextHttpTextFormat.TRACEPARENT)); | ||
assertEquals("traceStateData", dataCarrier.get(DistributedContextHttpTextFormat.TRACESTATE)); | ||
} | ||
|
||
@Test | ||
public void inject_nullValues() { | ||
DistributedContextHttpTextFormat httpTextFormat = | ||
DistributedContextHttpTextFormat.getInstance(); | ||
|
||
Map<String, String> dataCarrier = new HashMap<>(); | ||
DistributedContext distributedContext = | ||
new DistributedContextSdk.Builder() | ||
.put( | ||
EntryKey.create("foo"), | ||
EntryValue.create("bar"), | ||
Entry.METADATA_UNLIMITED_PROPAGATION) | ||
.build(); | ||
|
||
httpTextFormat.inject( | ||
distributedContext, | ||
dataCarrier, | ||
new HttpTextFormat.Setter<Map<String, String>>() { | ||
@Override | ||
public void put(Map<String, String> carrier, String key, String value) { | ||
carrier.put(key, value); | ||
} | ||
}); | ||
|
||
assertTrue(dataCarrier.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void extract() throws Exception { | ||
DistributedContextHttpTextFormat httpTextFormat = | ||
DistributedContextHttpTextFormat.getInstance(); | ||
Map<String, String> dataCarrier = new HashMap<>(); | ||
dataCarrier.put(DistributedContextHttpTextFormat.TRACESTATE, "traceStateData"); | ||
dataCarrier.put(DistributedContextHttpTextFormat.TRACEPARENT, "traceParentData"); | ||
|
||
DistributedContext context = | ||
httpTextFormat.extract( | ||
dataCarrier, | ||
new HttpTextFormat.Getter<Map<String, String>>() { | ||
@Nullable | ||
@Override | ||
public String get(Map<String, String> carrier, String key) { | ||
return carrier.get(key); | ||
} | ||
}); | ||
|
||
assertEquals(2, context.getEntries().size()); | ||
assertEquals( | ||
EntryValue.create("traceStateData"), | ||
context.getEntryValue(EntryKey.create(DistributedContextHttpTextFormat.TRACESTATE))); | ||
assertEquals( | ||
EntryValue.create("traceParentData"), | ||
context.getEntryValue(EntryKey.create(DistributedContextHttpTextFormat.TRACEPARENT))); | ||
} | ||
|
||
@Test | ||
public void extract_missingValues() throws Exception { | ||
DistributedContextHttpTextFormat httpTextFormat = | ||
DistributedContextHttpTextFormat.getInstance(); | ||
Map<String, String> dataCarrier = new HashMap<>(); | ||
|
||
DistributedContext context = | ||
httpTextFormat.extract( | ||
dataCarrier, | ||
new HttpTextFormat.Getter<Map<String, String>>() { | ||
@Nullable | ||
@Override | ||
public String get(Map<String, String> carrier, String key) { | ||
return carrier.get(key); | ||
} | ||
}); | ||
|
||
assertEquals(0, context.getEntries().size()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is the correlation-context from the w3c.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there something you think should be renamed in here?