|
| 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.exporter.trace.zipkin; |
| 18 | + |
| 19 | +import com.google.auto.value.AutoValue; |
| 20 | +import com.google.common.annotations.VisibleForTesting; |
| 21 | +import com.google.common.base.Preconditions; |
| 22 | +import io.opencensus.common.Duration; |
| 23 | +import javax.annotation.Nullable; |
| 24 | +import javax.annotation.concurrent.Immutable; |
| 25 | +import zipkin2.codec.SpanBytesEncoder; |
| 26 | +import zipkin2.reporter.Sender; |
| 27 | + |
| 28 | +/** |
| 29 | + * Configurations for {@link ZipkinTraceExporter}. |
| 30 | + * |
| 31 | + * @since 0.22 |
| 32 | + */ |
| 33 | +@AutoValue |
| 34 | +@Immutable |
| 35 | +public abstract class ZipkinExporterConfiguration { |
| 36 | + |
| 37 | + @VisibleForTesting static final Duration DEFAULT_DEADLINE = Duration.create(10, 0); |
| 38 | + |
| 39 | + ZipkinExporterConfiguration() {} |
| 40 | + |
| 41 | + /** |
| 42 | + * Returns the service name. |
| 43 | + * |
| 44 | + * @return the service name. |
| 45 | + * @since 0.22 |
| 46 | + */ |
| 47 | + public abstract String getServiceName(); |
| 48 | + |
| 49 | + /** |
| 50 | + * Returns the Zipkin V2 URL. |
| 51 | + * |
| 52 | + * @return the Zipkin V2 URL. |
| 53 | + * @since 0.22 |
| 54 | + */ |
| 55 | + public abstract String getV2Url(); |
| 56 | + |
| 57 | + /** |
| 58 | + * Returns the Zipkin sender. |
| 59 | + * |
| 60 | + * @return the Zipkin sender. |
| 61 | + * @since 0.22 |
| 62 | + */ |
| 63 | + @Nullable |
| 64 | + public abstract Sender getSender(); |
| 65 | + |
| 66 | + /** |
| 67 | + * Returns the {@link SpanBytesEncoder}. |
| 68 | + * |
| 69 | + * <p>Default is {@link SpanBytesEncoder#JSON_V2}. |
| 70 | + * |
| 71 | + * @return the {@code SpanBytesEncoder} |
| 72 | + * @since 0.22 |
| 73 | + */ |
| 74 | + public abstract SpanBytesEncoder getEncoder(); |
| 75 | + |
| 76 | + /** |
| 77 | + * Returns the deadline for exporting to Zipkin. |
| 78 | + * |
| 79 | + * <p>Default value is 10 seconds. |
| 80 | + * |
| 81 | + * @return the export deadline. |
| 82 | + * @since 0.22 |
| 83 | + */ |
| 84 | + public abstract Duration getDeadline(); |
| 85 | + |
| 86 | + /** |
| 87 | + * Returns a new {@link Builder}. |
| 88 | + * |
| 89 | + * @return a {@code Builder}. |
| 90 | + * @since 0.22 |
| 91 | + */ |
| 92 | + public static Builder builder() { |
| 93 | + return new AutoValue_ZipkinExporterConfiguration.Builder() |
| 94 | + .setV2Url("") |
| 95 | + .setEncoder(SpanBytesEncoder.JSON_V2) |
| 96 | + .setDeadline(DEFAULT_DEADLINE); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Builder for {@link ZipkinExporterConfiguration}. |
| 101 | + * |
| 102 | + * @since 0.22 |
| 103 | + */ |
| 104 | + @AutoValue.Builder |
| 105 | + public abstract static class Builder { |
| 106 | + |
| 107 | + @VisibleForTesting static final Duration ZERO = Duration.fromMillis(0); |
| 108 | + |
| 109 | + Builder() {} |
| 110 | + |
| 111 | + /** |
| 112 | + * Sets the service name. |
| 113 | + * |
| 114 | + * @param serviceName the service name. |
| 115 | + * @return this. |
| 116 | + * @since 0.22 |
| 117 | + */ |
| 118 | + public abstract Builder setServiceName(String serviceName); |
| 119 | + |
| 120 | + /** |
| 121 | + * Sets the Zipkin V2 URL, e.g.: "http://127.0.0.1:9411/api/v2/spans". |
| 122 | + * |
| 123 | + * <p>At least one of {@code V2Url} and {@code Sender} needs to be specified. If both {@code |
| 124 | + * V2Url} and {@code Sender} are set, {@code Sender} takes precedence. |
| 125 | + * |
| 126 | + * @param v2Url the Zipkin V2 URL. |
| 127 | + * @return this. |
| 128 | + * @since 0.22 |
| 129 | + */ |
| 130 | + public abstract Builder setV2Url(String v2Url); |
| 131 | + |
| 132 | + /** |
| 133 | + * Sets the Zipkin sender. |
| 134 | + * |
| 135 | + * <p>At least one of {@code V2Url} and {@code Sender} needs to be specified. If both {@code |
| 136 | + * V2Url} and {@code Sender} are set, {@code Sender} takes precedence. |
| 137 | + * |
| 138 | + * @param sender the Zipkin sender. |
| 139 | + * @return this. |
| 140 | + * @since 0.22 |
| 141 | + */ |
| 142 | + public abstract Builder setSender(Sender sender); |
| 143 | + |
| 144 | + /** |
| 145 | + * Sets the {@link SpanBytesEncoder}. |
| 146 | + * |
| 147 | + * @param encoder the {@code SpanBytesEncoder}. |
| 148 | + * @return this |
| 149 | + * @since 0.22 |
| 150 | + */ |
| 151 | + public abstract Builder setEncoder(SpanBytesEncoder encoder); |
| 152 | + |
| 153 | + /** |
| 154 | + * Sets the deadline for exporting to Zipkin. |
| 155 | + * |
| 156 | + * @param deadline the export deadline. |
| 157 | + * @return this |
| 158 | + * @since 0.22 |
| 159 | + */ |
| 160 | + public abstract Builder setDeadline(Duration deadline); |
| 161 | + |
| 162 | + abstract Duration getDeadline(); |
| 163 | + |
| 164 | + abstract String getV2Url(); |
| 165 | + |
| 166 | + @Nullable |
| 167 | + abstract Sender getSender(); |
| 168 | + |
| 169 | + abstract ZipkinExporterConfiguration autoBuild(); |
| 170 | + |
| 171 | + /** |
| 172 | + * Builds a {@link ZipkinExporterConfiguration}. |
| 173 | + * |
| 174 | + * @return a {@code ZipkinExporterConfiguration}. |
| 175 | + * @since 0.22 |
| 176 | + */ |
| 177 | + public ZipkinExporterConfiguration build() { |
| 178 | + Preconditions.checkArgument(getDeadline().compareTo(ZERO) > 0, "Deadline must be positive."); |
| 179 | + Preconditions.checkArgument( |
| 180 | + !getV2Url().isEmpty() || getSender() != null, |
| 181 | + "Neither Zipkin V2 URL nor Zipkin sender is specified."); |
| 182 | + return autoBuild(); |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments