Rename TagContext[Builder] to TagMap[Builder], remove state.
This commit is contained in:
parent
a58ec271d7
commit
b72a5ec5cd
|
@ -22,7 +22,7 @@ import openconsensus.metrics.data.AttachmentValue;
|
|||
import openconsensus.metrics.data.AttachmentValue.AttachmentValueString;
|
||||
import openconsensus.stats.Measure.MeasureDouble;
|
||||
import openconsensus.stats.Measure.MeasureLong;
|
||||
import openconsensus.tags.TagContext;
|
||||
import openconsensus.tags.TagMap;
|
||||
|
||||
/**
|
||||
* A map from {@link Measure}s to measured values to be recorded at the same time.
|
||||
|
@ -90,7 +90,7 @@ public abstract class MeasureMap {
|
|||
}
|
||||
|
||||
/**
|
||||
* Records all of the measures at the same time, with the current {@link TagContext}.
|
||||
* Records all of the measures at the same time, with the current {@link TagMap}.
|
||||
*
|
||||
* <p>This method records all of the stats in the {@code MeasureMap} every time it is called.
|
||||
*
|
||||
|
@ -99,12 +99,12 @@ public abstract class MeasureMap {
|
|||
public abstract void record();
|
||||
|
||||
/**
|
||||
* Records all of the measures at the same time, with an explicit {@link TagContext}.
|
||||
* Records all of the measures at the same time, with an explicit {@link TagMap}.
|
||||
*
|
||||
* <p>This method records all of the stats in the {@code MeasureMap} every time it is called.
|
||||
*
|
||||
* @param tags the tags associated with the measurements.
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public abstract void record(TagContext tags);
|
||||
public abstract void record(TagMap tags);
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ import openconsensus.common.Timestamp;
|
|||
import openconsensus.internal.Utils;
|
||||
import openconsensus.stats.Measure.MeasureDouble;
|
||||
import openconsensus.stats.Measure.MeasureLong;
|
||||
import openconsensus.tags.TagContext;
|
||||
import openconsensus.tags.TagMap;
|
||||
import openconsensus.tags.TagValue;
|
||||
|
||||
/** No-op implementations of stats classes. */
|
||||
|
@ -142,7 +142,7 @@ final class NoopStats {
|
|||
public void record() {}
|
||||
|
||||
@Override
|
||||
public void record(TagContext tags) {
|
||||
public void record(TagMap tags) {
|
||||
Utils.checkNotNull(tags, "tags");
|
||||
|
||||
if (hasUnsupportedValues) {
|
||||
|
|
|
@ -1,39 +0,0 @@
|
|||
/*
|
||||
* Copyright 2019, OpenConsensus 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 openconsensus.tags;
|
||||
|
||||
import java.util.Iterator;
|
||||
import openconsensus.common.Internal;
|
||||
|
||||
/**
|
||||
* Internal tagging utilities.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
@Internal
|
||||
public final class InternalUtils {
|
||||
private InternalUtils() {}
|
||||
|
||||
/**
|
||||
* Internal tag accessor.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static Iterator<Tag> getTags(TagContext tags) {
|
||||
return tags.getIterator();
|
||||
}
|
||||
}
|
|
@ -16,8 +16,6 @@
|
|||
|
||||
package openconsensus.tags;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import openconsensus.common.Scope;
|
||||
|
@ -41,30 +39,30 @@ final class NoopTags {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns a {@code Tagger} that only produces {@link TagContext}s with no tags.
|
||||
* Returns a {@code Tagger} that only produces {@link TagMap}s with no tags.
|
||||
*
|
||||
* @return a {@code Tagger} that only produces {@code TagContext}s with no tags.
|
||||
* @return a {@code Tagger} that only produces {@code TagMap}s with no tags.
|
||||
*/
|
||||
static Tagger getNoopTagger() {
|
||||
return NoopTagger.INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@code TagContextBuilder} that ignores all calls to {@link TagContextBuilder#put}.
|
||||
* Returns a {@code TagMapBuilder} that ignores all calls to {@link TagMapBuilder#put}.
|
||||
*
|
||||
* @return a {@code TagContextBuilder} that ignores all calls to {@link TagContextBuilder#put}.
|
||||
* @return a {@code TagMapBuilder} that ignores all calls to {@link TagMapBuilder#put}.
|
||||
*/
|
||||
static TagContextBuilder getNoopTagContextBuilder() {
|
||||
return NoopTagContextBuilder.INSTANCE;
|
||||
static TagMapBuilder getNoopTagContextBuilder() {
|
||||
return NoopTagMapBuilder.INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@code TagContext} that does not contain any tags.
|
||||
* Returns a {@code TagMap} that does not contain any tags.
|
||||
*
|
||||
* @return a {@code TagContext} that does not contain any tags.
|
||||
* @return a {@code TagMap} that does not contain any tags.
|
||||
*/
|
||||
static TagContext getNoopTagContext() {
|
||||
return NoopTagContext.INSTANCE;
|
||||
static TagMap getNoopTagContext() {
|
||||
return NoopTagMap.INSTANCE;
|
||||
}
|
||||
|
||||
/** Returns a {@code TagPropagationComponent} that contains no-op serializers. */
|
||||
|
@ -73,8 +71,8 @@ final class NoopTags {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns a {@code TagContextBinarySerializer} that serializes all {@code TagContext}s to zero
|
||||
* bytes and deserializes all inputs to empty {@code TagContext}s.
|
||||
* Returns a {@code TagContextBinarySerializer} that serializes all {@code TagMap}s to zero bytes
|
||||
* and deserializes all inputs to empty {@code TagMap}s.
|
||||
*/
|
||||
static TagContextBinarySerializer getNoopTagContextBinarySerializer() {
|
||||
return NoopTagContextBinarySerializer.INSTANCE;
|
||||
|
@ -82,8 +80,6 @@ final class NoopTags {
|
|||
|
||||
@ThreadSafe
|
||||
private static final class NoopTagsComponent extends TagsComponent {
|
||||
private volatile boolean isRead;
|
||||
|
||||
@Override
|
||||
public Tagger getTagger() {
|
||||
return getNoopTagger();
|
||||
|
@ -93,19 +89,6 @@ final class NoopTags {
|
|||
public TagPropagationComponent getTagPropagationComponent() {
|
||||
return getNoopTagPropagationComponent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TaggingState getState() {
|
||||
isRead = true;
|
||||
return TaggingState.DISABLED;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public void setState(TaggingState state) {
|
||||
Utils.checkNotNull(state, "state");
|
||||
Utils.checkState(!isRead, "State was already read, cannot set state.");
|
||||
}
|
||||
}
|
||||
|
||||
@Immutable
|
||||
|
@ -113,52 +96,44 @@ final class NoopTags {
|
|||
static final Tagger INSTANCE = new NoopTagger();
|
||||
|
||||
@Override
|
||||
public TagContext empty() {
|
||||
public TagMap empty() {
|
||||
return getNoopTagContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagContext getCurrentTagContext() {
|
||||
public TagMap getCurrentTagContext() {
|
||||
return getNoopTagContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagContextBuilder emptyBuilder() {
|
||||
public TagMapBuilder emptyBuilder() {
|
||||
return getNoopTagContextBuilder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagContextBuilder toBuilder(TagContext tags) {
|
||||
public TagMapBuilder toBuilder(TagMap tags) {
|
||||
Utils.checkNotNull(tags, "tags");
|
||||
return getNoopTagContextBuilder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagContextBuilder currentBuilder() {
|
||||
public TagMapBuilder currentBuilder() {
|
||||
return getNoopTagContextBuilder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Scope withTagContext(TagContext tags) {
|
||||
public Scope withTagContext(TagMap tags) {
|
||||
Utils.checkNotNull(tags, "tags");
|
||||
return NoopScope.getInstance();
|
||||
}
|
||||
}
|
||||
|
||||
@Immutable
|
||||
private static final class NoopTagContextBuilder extends TagContextBuilder {
|
||||
static final TagContextBuilder INSTANCE = new NoopTagContextBuilder();
|
||||
private static final class NoopTagMapBuilder extends TagMapBuilder {
|
||||
static final TagMapBuilder INSTANCE = new NoopTagMapBuilder();
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("deprecation")
|
||||
public TagContextBuilder put(TagKey key, TagValue value) {
|
||||
Utils.checkNotNull(key, "key");
|
||||
Utils.checkNotNull(value, "value");
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagContextBuilder put(TagKey key, TagValue value, TagMetadata tagMetadata) {
|
||||
public TagMapBuilder put(TagKey key, TagValue value, TagMetadata tagMetadata) {
|
||||
Utils.checkNotNull(key, "key");
|
||||
Utils.checkNotNull(value, "value");
|
||||
Utils.checkNotNull(tagMetadata, "tagMetadata");
|
||||
|
@ -166,13 +141,13 @@ final class NoopTags {
|
|||
}
|
||||
|
||||
@Override
|
||||
public TagContextBuilder remove(TagKey key) {
|
||||
public TagMapBuilder remove(TagKey key) {
|
||||
Utils.checkNotNull(key, "key");
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagContext build() {
|
||||
public TagMap build() {
|
||||
return getNoopTagContext();
|
||||
}
|
||||
|
||||
|
@ -183,14 +158,8 @@ final class NoopTags {
|
|||
}
|
||||
|
||||
@Immutable
|
||||
private static final class NoopTagContext extends TagContext {
|
||||
static final TagContext INSTANCE = new NoopTagContext();
|
||||
|
||||
// TODO(sebright): Is there any way to let the user know that their tags were ignored?
|
||||
@Override
|
||||
protected Iterator<Tag> getIterator() {
|
||||
return Collections.<Tag>emptySet().iterator();
|
||||
}
|
||||
private static final class NoopTagMap extends TagMap {
|
||||
static final TagMap INSTANCE = new NoopTagMap();
|
||||
}
|
||||
|
||||
@Immutable
|
||||
|
@ -209,13 +178,13 @@ final class NoopTags {
|
|||
static final byte[] EMPTY_BYTE_ARRAY = {};
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray(TagContext tags) {
|
||||
public byte[] toByteArray(TagMap tags) {
|
||||
Utils.checkNotNull(tags, "tags");
|
||||
return EMPTY_BYTE_ARRAY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TagContext fromByteArray(byte[] bytes) {
|
||||
public TagMap fromByteArray(byte[] bytes) {
|
||||
Utils.checkNotNull(bytes, "bytes");
|
||||
return getNoopTagContext();
|
||||
}
|
||||
|
|
|
@ -1,109 +0,0 @@
|
|||
/*
|
||||
* Copyright 2019, OpenConsensus 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 openconsensus.tags;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* A map from {@link TagKey} to {@link TagValue} that can be used to label anything that is
|
||||
* associated with a specific operation.
|
||||
*
|
||||
* <p>For example, {@code TagContext}s can be used to label stats, log messages, or debugging
|
||||
* information.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
@Immutable
|
||||
public abstract class TagContext {
|
||||
|
||||
/**
|
||||
* Returns an iterator over the tags in this {@code TagContext}.
|
||||
*
|
||||
* @return an iterator over the tags in this {@code TagContext}.
|
||||
* @since 0.1.0
|
||||
*/
|
||||
// This method is protected to prevent client code from accessing the tags of any TagContext. We
|
||||
// don't currently support efficient access to tags. However, every TagContext subclass needs to
|
||||
// provide access to its tags to the stats and tagging implementations by implementing this
|
||||
// method. If we decide to support access to tags in the future, we can add a public iterator()
|
||||
// method and implement it for all subclasses by calling getIterator().
|
||||
//
|
||||
// The stats and tagging implementations can access any TagContext's tags through
|
||||
// InternalUtils.getTags, which calls this method.
|
||||
protected abstract Iterator<Tag> getIterator();
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TagContext";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true iff the other object is an instance of {@code TagContext} and contains the same
|
||||
* key-value pairs. Implementations are free to override this method to provide better
|
||||
* performance.
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(@Nullable Object other) {
|
||||
if (!(other instanceof TagContext)) {
|
||||
return false;
|
||||
}
|
||||
TagContext otherTags = (TagContext) other;
|
||||
Iterator<Tag> iter1 = getIterator();
|
||||
Iterator<Tag> iter2 = otherTags.getIterator();
|
||||
HashMap<Tag, Integer> tags = new HashMap<Tag, Integer>();
|
||||
while (iter1 != null && iter1.hasNext()) {
|
||||
Tag tag = iter1.next();
|
||||
if (tags.containsKey(tag)) {
|
||||
tags.put(tag, tags.get(tag) + 1);
|
||||
} else {
|
||||
tags.put(tag, 1);
|
||||
}
|
||||
}
|
||||
while (iter2 != null && iter2.hasNext()) {
|
||||
Tag tag = iter2.next();
|
||||
if (!tags.containsKey(tag)) {
|
||||
return false;
|
||||
}
|
||||
int count = tags.get(tag);
|
||||
if (count > 1) {
|
||||
tags.put(tag, count - 1);
|
||||
} else {
|
||||
tags.remove(tag);
|
||||
}
|
||||
}
|
||||
return tags.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int hashCode() {
|
||||
int hashCode = 0;
|
||||
Iterator<Tag> i = getIterator();
|
||||
if (i == null) {
|
||||
return hashCode;
|
||||
}
|
||||
while (i.hasNext()) {
|
||||
Tag tag = i.next();
|
||||
if (tag != null) {
|
||||
hashCode += tag.hashCode();
|
||||
}
|
||||
}
|
||||
return hashCode;
|
||||
}
|
||||
}
|
|
@ -1,83 +0,0 @@
|
|||
/*
|
||||
* Copyright 2019, OpenConsensus 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 openconsensus.tags;
|
||||
|
||||
import openconsensus.common.Scope;
|
||||
|
||||
/**
|
||||
* Builder for the {@link TagContext} class.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public abstract class TagContextBuilder {
|
||||
|
||||
/**
|
||||
* Adds the key/value pair regardless of whether the key is present.
|
||||
*
|
||||
* <p>For backwards-compatibility this method still produces propagating {@link Tag}s.
|
||||
*
|
||||
* <p>Equivalent to calling {@code put(key, value,
|
||||
* TagMetadata.create(TagTtl.UNLIMITED_PROPAGATION))}.
|
||||
*
|
||||
* @param key the {@code TagKey} which will be set.
|
||||
* @param value the {@code TagValue} to set for the given key.
|
||||
* @return this
|
||||
* @since 0.1.0
|
||||
* @deprecated in favor of {@link #put(TagKey, TagValue, TagMetadata)}.
|
||||
*/
|
||||
@Deprecated
|
||||
public abstract TagContextBuilder put(TagKey key, TagValue value);
|
||||
|
||||
/**
|
||||
* Adds the key/value pair and metadata regardless of whether the key is present.
|
||||
*
|
||||
* @param key the {@code TagKey} which will be set.
|
||||
* @param value the {@code TagValue} to set for the given key.
|
||||
* @param tagMetadata the {@code TagMetadata} associated with this {@link Tag}.
|
||||
* @return this
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public abstract TagContextBuilder put(TagKey key, TagValue value, TagMetadata tagMetadata);
|
||||
|
||||
/**
|
||||
* Removes the key if it exists.
|
||||
*
|
||||
* @param key the {@code TagKey} which will be removed.
|
||||
* @return this
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public abstract TagContextBuilder remove(TagKey key);
|
||||
|
||||
/**
|
||||
* Creates a {@code TagContext} from this builder.
|
||||
*
|
||||
* @return a {@code TagContext} with the same tags as this builder.
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public abstract TagContext build();
|
||||
|
||||
/**
|
||||
* Enters the scope of code where the {@link TagContext} created from this builder is in the
|
||||
* current context and returns an object that represents that scope. The scope is exited when the
|
||||
* returned object is closed.
|
||||
*
|
||||
* @return an object that defines a scope where the {@code TagContext} created from this builder
|
||||
* is set to the current context.
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public abstract Scope buildScoped();
|
||||
}
|
|
@ -22,7 +22,7 @@ import openconsensus.internal.StringUtils;
|
|||
import openconsensus.internal.Utils;
|
||||
|
||||
/**
|
||||
* A key to a value stored in a {@link TagContext}.
|
||||
* A key to a value stored in a {@link TagMap}.
|
||||
*
|
||||
* <p>Each {@code TagKey} has a {@code String} name. Names have a maximum length of {@link
|
||||
* #MAX_LENGTH} and contain only printable ASCII characters.
|
||||
|
|
|
@ -19,34 +19,34 @@ package openconsensus.tags;
|
|||
import openconsensus.common.Scope;
|
||||
|
||||
/**
|
||||
* Object for creating new {@link TagContext}s and {@code TagContext}s based on the current context.
|
||||
* Object for creating new {@link TagMap}s and {@code TagMap}s based on the current context.
|
||||
*
|
||||
* <p>This class returns {@link TagContextBuilder builders} that can be used to create the
|
||||
* implementation-dependent {@link TagContext}s.
|
||||
* <p>This class returns {@link TagMapBuilder builders} that can be used to create the
|
||||
* implementation-dependent {@link TagMap}s.
|
||||
*
|
||||
* <p>Implementations may have different constraints and are free to convert tag contexts to their
|
||||
* own subtypes. This means callers cannot assume the {@link #getCurrentTagContext() current
|
||||
* context} is the same instance as the one {@link #withTagContext(TagContext) placed into scope}.
|
||||
* context} is the same instance as the one {@link #withTagContext(TagMap) placed into scope}.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public abstract class Tagger {
|
||||
|
||||
/**
|
||||
* Returns an empty {@code TagContext}.
|
||||
* Returns an empty {@code TagMap}.
|
||||
*
|
||||
* @return an empty {@code TagContext}.
|
||||
* @return an empty {@code TagMap}.
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public abstract TagContext empty();
|
||||
public abstract TagMap empty();
|
||||
|
||||
/**
|
||||
* Returns the current {@code TagContext}.
|
||||
* Returns the current {@code TagMap}.
|
||||
*
|
||||
* @return the current {@code TagContext}.
|
||||
* @return the current {@code TagMap}.
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public abstract TagContext getCurrentTagContext();
|
||||
public abstract TagMap getCurrentTagContext();
|
||||
|
||||
/**
|
||||
* Returns a new empty {@code Builder}.
|
||||
|
@ -54,33 +54,33 @@ public abstract class Tagger {
|
|||
* @return a new empty {@code Builder}.
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public abstract TagContextBuilder emptyBuilder();
|
||||
public abstract TagMapBuilder emptyBuilder();
|
||||
|
||||
/**
|
||||
* Returns a builder based on this {@code TagContext}.
|
||||
* Returns a builder based on this {@code TagMap}.
|
||||
*
|
||||
* @return a builder based on this {@code TagContext}.
|
||||
* @return a builder based on this {@code TagMap}.
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public abstract TagContextBuilder toBuilder(TagContext tags);
|
||||
public abstract TagMapBuilder toBuilder(TagMap tags);
|
||||
|
||||
/**
|
||||
* Returns a new builder created from the current {@code TagContext}.
|
||||
* Returns a new builder created from the current {@code TagMap}.
|
||||
*
|
||||
* @return a new builder created from the current {@code TagContext}.
|
||||
* @return a new builder created from the current {@code TagMap}.
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public abstract TagContextBuilder currentBuilder();
|
||||
public abstract TagMapBuilder currentBuilder();
|
||||
|
||||
/**
|
||||
* Enters the scope of code where the given {@code TagContext} is in the current context
|
||||
* (replacing the previous {@code TagContext}) and returns an object that represents that scope.
|
||||
* The scope is exited when the returned object is closed.
|
||||
* Enters the scope of code where the given {@code TagMap} is in the current context (replacing
|
||||
* the previous {@code TagMap}) and returns an object that represents that scope. The scope is
|
||||
* exited when the returned object is closed.
|
||||
*
|
||||
* @param tags the {@code TagContext} to be set to the current context.
|
||||
* @return an object that defines a scope where the given {@code TagContext} is set to the current
|
||||
* @param tags the {@code TagMap} to be set to the current context.
|
||||
* @return an object that defines a scope where the given {@code TagMap} is set to the current
|
||||
* context.
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public abstract Scope withTagContext(TagContext tags);
|
||||
public abstract Scope withTagContext(TagMap tags);
|
||||
}
|
||||
|
|
|
@ -1,48 +0,0 @@
|
|||
/*
|
||||
* Copyright 2019, OpenConsensus 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 openconsensus.tags;
|
||||
|
||||
/**
|
||||
* State of the {@link TagsComponent}.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public enum TaggingState {
|
||||
// TODO(sebright): Should we add a state that propagates the tags, but doesn't allow
|
||||
// modifications?
|
||||
|
||||
/**
|
||||
* State that fully enables tagging.
|
||||
*
|
||||
* <p>The {@link TagsComponent} can add tags to {@link TagContext}s, propagate {@code TagContext}s
|
||||
* in the current context, and serialize {@code TagContext}s.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
ENABLED,
|
||||
|
||||
/**
|
||||
* State that disables tagging.
|
||||
*
|
||||
* <p>The {@link TagsComponent} may not add tags to {@link TagContext}s, propagate {@code
|
||||
* TagContext}s in the current context, or serialize {@code TagContext}s.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
// TODO(sebright): Document how this interacts with stats collection.
|
||||
DISABLED
|
||||
}
|
|
@ -56,40 +56,6 @@ public final class Tags {
|
|||
return tagsComponent.getTagPropagationComponent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current {@code TaggingState}.
|
||||
*
|
||||
* <p>When no implementation is available, {@code getState} always returns {@link
|
||||
* TaggingState#DISABLED}.
|
||||
*
|
||||
* <p>Once {@link #getState()} is called, subsequent calls to {@link #setState(TaggingState)} will
|
||||
* throw an {@code IllegalStateException}.
|
||||
*
|
||||
* @return the current {@code TaggingState}.
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static TaggingState getState() {
|
||||
return tagsComponent.getState();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the current {@code TaggingState}.
|
||||
*
|
||||
* <p>When no implementation is available, {@code setState} does not change the state.
|
||||
*
|
||||
* @param state the new {@code TaggingState}.
|
||||
* @throws IllegalStateException if {@link #getState()} was previously called.
|
||||
* @deprecated This method is deprecated because other libraries could cache the result of {@link
|
||||
* #getState()}, use a stale value, and behave incorrectly. It is only safe to call early in
|
||||
* initialization. This method throws {@link IllegalStateException} after {@link #getState()}
|
||||
* has been called, in order to limit changes to the result of {@code getState()}.
|
||||
* @since 0.1.0
|
||||
*/
|
||||
@Deprecated
|
||||
public static void setState(TaggingState state) {
|
||||
tagsComponent.setState(state);
|
||||
}
|
||||
|
||||
// Any provider that may be used for TagsComponent can be added here.
|
||||
@DefaultVisibilityForTesting
|
||||
static TagsComponent loadTagsComponent(@Nullable ClassLoader classLoader) {
|
||||
|
|
|
@ -40,34 +40,4 @@ public abstract class TagsComponent {
|
|||
* @since 0.1.0
|
||||
*/
|
||||
public abstract TagPropagationComponent getTagPropagationComponent();
|
||||
|
||||
/**
|
||||
* Returns the current {@code TaggingState}.
|
||||
*
|
||||
* <p>When no implementation is available, {@code getState} always returns {@link
|
||||
* TaggingState#DISABLED}.
|
||||
*
|
||||
* <p>Once {@link #getState()} is called, subsequent calls to {@link #setState(TaggingState)} will
|
||||
* throw an {@code IllegalStateException}.
|
||||
*
|
||||
* @return the current {@code TaggingState}.
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public abstract TaggingState getState();
|
||||
|
||||
/**
|
||||
* Sets the current {@code TaggingState}.
|
||||
*
|
||||
* <p>When no implementation is available, {@code setState} does not change the state.
|
||||
*
|
||||
* @param state the new {@code TaggingState}.
|
||||
* @throws IllegalStateException if {@link #getState()} was previously called.
|
||||
* @deprecated This method is deprecated because other libraries could cache the result of {@link
|
||||
* #getState()}, use a stale value, and behave incorrectly. It is only safe to call early in
|
||||
* initialization. This method throws {@link IllegalStateException} after {@code getState()}
|
||||
* has been called, in order to limit changes to the result of {@code getState()}.
|
||||
* @since 0.1.0
|
||||
*/
|
||||
@Deprecated
|
||||
public abstract void setState(TaggingState state);
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
*
|
||||
* <p>{@link openconsensus.tags.Tag Tags} are key-value pairs. The {@link openconsensus.tags.TagKey
|
||||
* keys} and {@link openconsensus.tags.TagValue values} are wrapped {@code String}s. They are stored
|
||||
* as a map in a {@link openconsensus.tags.TagContext}.
|
||||
* as a map in a {@link openconsensus.tags.TagMap}.
|
||||
*
|
||||
* <p>Note that tags are independent of the tracing data that is propagated in the {@code
|
||||
* io.grpc.Context}, such as trace ID.
|
||||
|
|
|
@ -17,12 +17,12 @@
|
|||
package openconsensus.tags.propagation;
|
||||
|
||||
import openconsensus.tags.Tag;
|
||||
import openconsensus.tags.TagContext;
|
||||
import openconsensus.tags.TagMap;
|
||||
import openconsensus.tags.TagMetadata;
|
||||
import openconsensus.tags.TagMetadata.TagTtl;
|
||||
|
||||
/**
|
||||
* Object for serializing and deserializing {@link TagContext}s with the binary format.
|
||||
* Object for serializing and deserializing {@link TagMap}s with the binary format.
|
||||
*
|
||||
* <p>See <a
|
||||
* href="https://github.com/census-instrumentation/opencensus-specs/blob/master/encodings/BinaryEncoding.md#tag-context">opencensus-specs</a>
|
||||
|
@ -33,31 +33,31 @@ import openconsensus.tags.TagMetadata.TagTtl;
|
|||
public abstract class TagContextBinarySerializer {
|
||||
|
||||
/**
|
||||
* Serializes the {@code TagContext} into the on-the-wire representation.
|
||||
* Serializes the {@code TagMap} into the on-the-wire representation.
|
||||
*
|
||||
* <p>This method should be the inverse of {@link #fromByteArray}.
|
||||
*
|
||||
* <p>{@link Tag}s that have a {@link TagMetadata} with {@link TagTtl#NO_PROPAGATION} will not be
|
||||
* serialized.
|
||||
*
|
||||
* @param tags the {@code TagContext} to serialize.
|
||||
* @return the on-the-wire representation of a {@code TagContext}.
|
||||
* @param tags the {@code TagMap} to serialize.
|
||||
* @return the on-the-wire representation of a {@code TagMap}.
|
||||
* @throws TagContextSerializationException if the result would be larger than the maximum allowed
|
||||
* serialized size.
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public abstract byte[] toByteArray(TagContext tags) throws TagContextSerializationException;
|
||||
public abstract byte[] toByteArray(TagMap tags) throws TagContextSerializationException;
|
||||
|
||||
/**
|
||||
* Creates a {@code TagContext} from the given on-the-wire encoded representation.
|
||||
* Creates a {@code TagMap} from the given on-the-wire encoded representation.
|
||||
*
|
||||
* <p>This method should be the inverse of {@link #toByteArray}.
|
||||
*
|
||||
* @param bytes on-the-wire representation of a {@code TagContext}.
|
||||
* @return a {@code TagContext} deserialized from {@code bytes}.
|
||||
* @param bytes on-the-wire representation of a {@code TagMap}.
|
||||
* @return a {@code TagMap} deserialized from {@code bytes}.
|
||||
* @throws TagContextDeserializationException if there is a parse error, the input contains
|
||||
* invalid tags, or the input is larger than the maximum allowed serialized size.
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public abstract TagContext fromByteArray(byte[] bytes) throws TagContextDeserializationException;
|
||||
public abstract TagMap fromByteArray(byte[] bytes) throws TagContextDeserializationException;
|
||||
}
|
||||
|
|
|
@ -16,10 +16,10 @@
|
|||
|
||||
package openconsensus.tags.propagation;
|
||||
|
||||
import openconsensus.tags.TagContext;
|
||||
import openconsensus.tags.TagMap;
|
||||
|
||||
/**
|
||||
* Exception thrown when a {@link TagContext} cannot be parsed.
|
||||
* Exception thrown when a {@link TagMap} cannot be parsed.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
|
|
|
@ -16,10 +16,10 @@
|
|||
|
||||
package openconsensus.tags.propagation;
|
||||
|
||||
import openconsensus.tags.TagContext;
|
||||
import openconsensus.tags.TagMap;
|
||||
|
||||
/**
|
||||
* Exception thrown when a {@link TagContext} cannot be serialized.
|
||||
* Exception thrown when a {@link TagMap} cannot be serialized.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
|
|
|
@ -16,10 +16,10 @@
|
|||
|
||||
package openconsensus.tags.propagation;
|
||||
|
||||
import openconsensus.tags.TagContext;
|
||||
import openconsensus.tags.TagMap;
|
||||
|
||||
/**
|
||||
* Object containing all supported {@link TagContext} propagation formats.
|
||||
* Object containing all supported {@link TagMap} propagation formats.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
|
|
|
@ -17,40 +17,31 @@
|
|||
package openconsensus.tags.unsafe;
|
||||
|
||||
import io.grpc.Context;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
import openconsensus.tags.Tag;
|
||||
import openconsensus.tags.TagContext;
|
||||
import openconsensus.tags.TagMap;
|
||||
|
||||
/**
|
||||
* Utility methods for accessing the {@link TagContext} contained in the {@link io.grpc.Context}.
|
||||
* Utility methods for accessing the {@link TagMap} contained in the {@link io.grpc.Context}.
|
||||
*
|
||||
* <p>Most code should interact with the current context via the public APIs in {@link TagContext}
|
||||
* and avoid accessing {@link #TAG_CONTEXT_KEY} directly.
|
||||
* <p>Most code should interact with the current context via the public APIs in {@link TagMap} and
|
||||
* avoid accessing {@link #TAG_CONTEXT_KEY} directly.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public final class ContextUtils {
|
||||
private static final TagContext EMPTY_TAG_CONTEXT = new EmptyTagContext();
|
||||
private static final TagMap EMPTY_TAG_CONTEXT = new EmptyTagMap();
|
||||
|
||||
private ContextUtils() {}
|
||||
|
||||
/**
|
||||
* The {@link io.grpc.Context.Key} used to interact with the {@code TagContext} contained in the
|
||||
* The {@link io.grpc.Context.Key} used to interact with the {@code TagMap} contained in the
|
||||
* {@link io.grpc.Context}.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static final Context.Key<TagContext> TAG_CONTEXT_KEY =
|
||||
public static final Context.Key<TagMap> TAG_CONTEXT_KEY =
|
||||
Context.keyWithDefault("opencensus-tag-context-key", EMPTY_TAG_CONTEXT);
|
||||
|
||||
@Immutable
|
||||
private static final class EmptyTagContext extends TagContext {
|
||||
|
||||
@Override
|
||||
protected Iterator<Tag> getIterator() {
|
||||
return Collections.<Tag>emptySet().iterator();
|
||||
}
|
||||
}
|
||||
private static final class EmptyTagMap extends TagMap {}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue