|
| 1 | +/* |
| 2 | + * Copyright 2019, OpenCensus Authors |
| 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 | + |
| 17 | +package io.opencensus.tags.propagation; |
| 18 | + |
| 19 | +import io.opencensus.tags.TagContext; |
| 20 | +import java.util.List; |
| 21 | +import javax.annotation.Nullable; |
| 22 | + |
| 23 | +/*>>> |
| 24 | +import org.checkerframework.checker.nullness.qual.NonNull; |
| 25 | +*/ |
| 26 | + |
| 27 | +/** |
| 28 | + * Object for injecting and extracting {@link TagContext} as text into carriers that travel in-band |
| 29 | + * across process boundaries. Tags are often encoded as messaging or RPC request headers. |
| 30 | + * |
| 31 | + * <p>When using http, the carrier of propagated data on both the client (injector) and server |
| 32 | + * (extractor) side is usually an http request. Propagation is usually implemented via library- |
| 33 | + * specific request interceptors, where the client-side injects tags and the server-side extracts |
| 34 | + * them. |
| 35 | + * |
| 36 | + * <p>Example of usage on the client: |
| 37 | + * |
| 38 | + * <pre>{@code |
| 39 | + * private static final Tagger tagger = Tags.getTagger(); |
| 40 | + * private static final TagContextTextFormat textFormat = |
| 41 | + * Tags.getPropagationComponent().getCorrelationContextFormat(); |
| 42 | + * private static final TagContextTextFormat.Setter setter = |
| 43 | + * new TagContextTextFormat.Setter<HttpURLConnection>() { |
| 44 | + * public void put(HttpURLConnection carrier, String key, String value) { |
| 45 | + * carrier.setRequestProperty(field, value); |
| 46 | + * } |
| 47 | + * }; |
| 48 | + * |
| 49 | + * void makeHttpRequest() { |
| 50 | + * TagContext tagContext = tagger.emptyBuilder().put(K, V).build(); |
| 51 | + * try (Scope s = tagger.withTagContext(tagContext)) { |
| 52 | + * HttpURLConnection connection = |
| 53 | + * (HttpURLConnection) new URL("http://myserver").openConnection(); |
| 54 | + * textFormat.inject(tagContext, connection, httpURLConnectionSetter); |
| 55 | + * // Send the request, wait for response and maybe set the status if not ok. |
| 56 | + * } |
| 57 | + * } |
| 58 | + * }</pre> |
| 59 | + * |
| 60 | + * <p>Example of usage on the server: |
| 61 | + * |
| 62 | + * <pre>{@code |
| 63 | + * private static final Tagger tagger = Tags.getTagger(); |
| 64 | + * private static final TagContextTextFormat textFormat = |
| 65 | + * Tags.getPropagationComponent().getCorrelationContextFormat(); |
| 66 | + * private static final TagContextTextFormat.Getter<HttpRequest> getter = ...; |
| 67 | + * |
| 68 | + * void onRequestReceived(HttpRequest request) { |
| 69 | + * TagContext tagContext = textFormat.extract(request, getter); |
| 70 | + * try (Scope s = tagger.withTagContext(tagContext)) { |
| 71 | + * // Handle request and send response back. |
| 72 | + * } |
| 73 | + * } |
| 74 | + * }</pre> |
| 75 | + * |
| 76 | + * @since 0.21 |
| 77 | + */ |
| 78 | +public abstract class TagContextTextFormat { |
| 79 | + |
| 80 | + /** |
| 81 | + * The propagation fields defined. If your carrier is reused, you should delete the fields here |
| 82 | + * before calling {@link #inject(TagContext, Object, Setter)}. |
| 83 | + * |
| 84 | + * <p>For example, if the carrier is a single-use or immutable request object, you don't need to |
| 85 | + * clear fields as they couldn't have been set before. If it is a mutable, retryable object, |
| 86 | + * successive calls should clear these fields first. |
| 87 | + * |
| 88 | + * @since 0.21 |
| 89 | + */ |
| 90 | + // The use cases of this are: |
| 91 | + // * allow pre-allocation of fields, especially in systems like gRPC Metadata |
| 92 | + // * allow a single-pass over an iterator (ex OpenTracing has no getter in TextMap) |
| 93 | + public abstract List<String> fields(); |
| 94 | + |
| 95 | + /** |
| 96 | + * Injects the tag context downstream. For example, as http headers. |
| 97 | + * |
| 98 | + * @param tagContext the tag context. |
| 99 | + * @param carrier holds propagation fields. For example, an outgoing message or http request. |
| 100 | + * @param setter invoked for each propagation key to add or remove. |
| 101 | + * @throws TagContextSerializationException if the given tag context cannot be serialized. |
| 102 | + * @since 0.21 |
| 103 | + */ |
| 104 | + public abstract <C /*>>> extends @NonNull Object*/> void inject( |
| 105 | + TagContext tagContext, C carrier, Setter<C> setter) throws TagContextSerializationException; |
| 106 | + |
| 107 | + /** |
| 108 | + * Class that allows a {@code TagContextTextFormat} to set propagated fields into a carrier. |
| 109 | + * |
| 110 | + * <p>{@code Setter} is stateless and allows to be saved as a constant to avoid runtime |
| 111 | + * allocations. |
| 112 | + * |
| 113 | + * @param <C> carrier of propagation fields, such as an http request |
| 114 | + * @since 0.21 |
| 115 | + */ |
| 116 | + public abstract static class Setter<C> { |
| 117 | + |
| 118 | + /** |
| 119 | + * Replaces a propagated field with the given value. |
| 120 | + * |
| 121 | + * <p>For example, a setter for an {@link java.net.HttpURLConnection} would be the method |
| 122 | + * reference {@link java.net.HttpURLConnection#addRequestProperty(String, String)} |
| 123 | + * |
| 124 | + * @param carrier holds propagation fields. For example, an outgoing message or http request. |
| 125 | + * @param key the key of the field. |
| 126 | + * @param value the value of the field. |
| 127 | + * @since 0.21 |
| 128 | + */ |
| 129 | + public abstract void put(C carrier, String key, String value); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Extracts the tag context from upstream. For example, as http headers. |
| 134 | + * |
| 135 | + * @param carrier holds propagation fields. For example, an outgoing message or http request. |
| 136 | + * @param getter invoked for each propagation key to get. |
| 137 | + * @throws TagContextDeserializationException if the input is invalid |
| 138 | + * @since 0.21 |
| 139 | + */ |
| 140 | + public abstract <C /*>>> extends @NonNull Object*/> TagContext extract( |
| 141 | + C carrier, Getter<C> getter) throws TagContextDeserializationException; |
| 142 | + |
| 143 | + /** |
| 144 | + * Class that allows a {@code TagContextTextFormat} to read propagated fields from a carrier. |
| 145 | + * |
| 146 | + * <p>{@code Getter} is stateless and allows to be saved as a constant to avoid runtime |
| 147 | + * allocations. |
| 148 | + * |
| 149 | + * @param <C> carrier of propagation fields, such as an http request |
| 150 | + * @since 0.21 |
| 151 | + */ |
| 152 | + public abstract static class Getter<C> { |
| 153 | + |
| 154 | + /** |
| 155 | + * Returns the first value of the given propagation {@code key} or returns {@code null}. |
| 156 | + * |
| 157 | + * @param carrier carrier of propagation fields, such as an http request |
| 158 | + * @param key the key of the field. |
| 159 | + * @return the first value of the given propagation {@code key} or returns {@code null}. |
| 160 | + * @since 0.21 |
| 161 | + */ |
| 162 | + @Nullable |
| 163 | + public abstract String get(C carrier, String key); |
| 164 | + } |
| 165 | +} |
0 commit comments