|
| 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.jaeger; |
| 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.jaegertracing.thrift.internal.senders.ThriftSender; |
| 23 | +import io.opencensus.common.Duration; |
| 24 | +import javax.annotation.Nullable; |
| 25 | +import javax.annotation.concurrent.Immutable; |
| 26 | + |
| 27 | +/** |
| 28 | + * Configurations for {@link JaegerTraceExporter}. |
| 29 | + * |
| 30 | + * @since 0.22 |
| 31 | + */ |
| 32 | +@AutoValue |
| 33 | +@Immutable |
| 34 | +public abstract class JaegerExporterConfiguration { |
| 35 | + |
| 36 | + @VisibleForTesting static final Duration DEFAULT_DEADLINE = Duration.create(10, 0); |
| 37 | + |
| 38 | + JaegerExporterConfiguration() {} |
| 39 | + |
| 40 | + /** |
| 41 | + * Returns the service name. |
| 42 | + * |
| 43 | + * @return the service name. |
| 44 | + * @since 0.22 |
| 45 | + */ |
| 46 | + public abstract String getServiceName(); |
| 47 | + |
| 48 | + /** |
| 49 | + * Returns the Thrift endpoint of your Jaeger instance. |
| 50 | + * |
| 51 | + * @return the Thrift endpoint. |
| 52 | + * @since 0.22 |
| 53 | + */ |
| 54 | + public abstract String getThriftEndpoint(); |
| 55 | + |
| 56 | + /** |
| 57 | + * Returns the Thrift sender. |
| 58 | + * |
| 59 | + * @return the Thrift sender. |
| 60 | + * @since 0.22 |
| 61 | + */ |
| 62 | + @Nullable |
| 63 | + public abstract ThriftSender getThriftSender(); |
| 64 | + |
| 65 | + /** |
| 66 | + * Returns the deadline for exporting to Jaeger. |
| 67 | + * |
| 68 | + * <p>Default value is 10 seconds. |
| 69 | + * |
| 70 | + * @return the export deadline. |
| 71 | + * @since 0.22 |
| 72 | + */ |
| 73 | + public abstract Duration getDeadline(); |
| 74 | + |
| 75 | + /** |
| 76 | + * Returns a new {@link Builder}. |
| 77 | + * |
| 78 | + * @return a {@code Builder}. |
| 79 | + * @since 0.22 |
| 80 | + */ |
| 81 | + public static Builder builder() { |
| 82 | + return new AutoValue_JaegerExporterConfiguration.Builder() |
| 83 | + .setThriftEndpoint("") |
| 84 | + .setDeadline(DEFAULT_DEADLINE); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Builder for {@link JaegerExporterConfiguration}. |
| 89 | + * |
| 90 | + * @since 0.22 |
| 91 | + */ |
| 92 | + @AutoValue.Builder |
| 93 | + public abstract static class Builder { |
| 94 | + |
| 95 | + @VisibleForTesting static final Duration ZERO = Duration.fromMillis(0); |
| 96 | + |
| 97 | + Builder() {} |
| 98 | + |
| 99 | + /** |
| 100 | + * Sets the service name. |
| 101 | + * |
| 102 | + * @param serviceName the service name. |
| 103 | + * @return this. |
| 104 | + * @since 0.22 |
| 105 | + */ |
| 106 | + public abstract Builder setServiceName(String serviceName); |
| 107 | + |
| 108 | + /** |
| 109 | + * Sets the Thrift endpoint of your Jaeger instance. e.g.: "http://127.0.0.1:14268/api/traces". |
| 110 | + * |
| 111 | + * <p>At least one of {@code ThriftEndpoint} and {@code ThriftSender} needs to be specified. If |
| 112 | + * both {@code ThriftEndpoint} and {@code ThriftSender} are set, {@code ThriftSender} takes |
| 113 | + * precedence. |
| 114 | + * |
| 115 | + * @param thriftEndpoint the Thrift endpoint. |
| 116 | + * @return this. |
| 117 | + * @since 0.22 |
| 118 | + */ |
| 119 | + public abstract Builder setThriftEndpoint(String thriftEndpoint); |
| 120 | + |
| 121 | + /** |
| 122 | + * Sets the Thrift sender. |
| 123 | + * |
| 124 | + * <p>At least one of {@code ThriftEndpoint} and {@code ThriftSender} needs to be specified. If |
| 125 | + * both {@code ThriftEndpoint} and {@code ThriftSender} are set, {@code ThriftSender} takes |
| 126 | + * precedence. |
| 127 | + * |
| 128 | + * @param sender the Thrift sender. |
| 129 | + * @return this. |
| 130 | + * @since 0.22 |
| 131 | + */ |
| 132 | + public abstract Builder setThriftSender(ThriftSender sender); |
| 133 | + |
| 134 | + /** |
| 135 | + * Sets the deadline for exporting to Jaeger. |
| 136 | + * |
| 137 | + * @param deadline the export deadline. |
| 138 | + * @return this |
| 139 | + * @since 0.22 |
| 140 | + */ |
| 141 | + public abstract Builder setDeadline(Duration deadline); |
| 142 | + |
| 143 | + abstract Duration getDeadline(); |
| 144 | + |
| 145 | + abstract String getThriftEndpoint(); |
| 146 | + |
| 147 | + @Nullable |
| 148 | + abstract ThriftSender getThriftSender(); |
| 149 | + |
| 150 | + abstract JaegerExporterConfiguration autoBuild(); |
| 151 | + |
| 152 | + /** |
| 153 | + * Builds a {@link JaegerExporterConfiguration}. |
| 154 | + * |
| 155 | + * @return a {@code JaegerExporterConfiguration}. |
| 156 | + * @since 0.22 |
| 157 | + */ |
| 158 | + public JaegerExporterConfiguration build() { |
| 159 | + Preconditions.checkArgument(getDeadline().compareTo(ZERO) > 0, "Deadline must be positive."); |
| 160 | + Preconditions.checkArgument( |
| 161 | + !getThriftEndpoint().isEmpty() || getThriftSender() != null, |
| 162 | + "Neither Thrift endpoint nor Thrift sender is specified."); |
| 163 | + return autoBuild(); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments