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.elasticsearch; 18 19 import com.google.auto.value.AutoValue; 20 import com.google.common.annotations.VisibleForTesting; 21 import com.google.common.base.Preconditions; 22 import com.google.common.base.Strings; 23 import io.opencensus.common.Duration; 24 import javax.annotation.Nullable; 25 import javax.annotation.concurrent.Immutable; 26 27 /** 28 * Configurations for {@link ElasticsearchTraceExporter}. 29 * 30 * @since 0.20.0 31 */ 32 @AutoValue 33 @Immutable 34 public abstract class ElasticsearchTraceConfiguration { 35 36 @VisibleForTesting static final Duration DEFAULT_DEADLINE = Duration.create(10, 0); 37 @VisibleForTesting static final Duration ZERO = Duration.fromMillis(0); 38 39 /** 40 * Returns a new {@link Builder}. 41 * 42 * @return a {@code Builder}. 43 * @since 0.20.0 44 */ builder()45 public static Builder builder() { 46 return new AutoValue_ElasticsearchTraceConfiguration.Builder().setDeadline(DEFAULT_DEADLINE); 47 } 48 49 /** 50 * Retrieves the app name configured. 51 * 52 * @return the name of app to include in traces. 53 * @since 0.20.0 54 */ getAppName()55 public abstract String getAppName(); 56 57 /** 58 * Retrieves user name used to access Elasticsearch. 59 * 60 * @return the username for Elasticsearch. 61 * @since 0.20.0 62 */ 63 @Nullable getUserName()64 public abstract String getUserName(); 65 66 /** 67 * Retrieves password used to access Elasticsearch. 68 * 69 * @return the password for Elasticsearch. 70 * @since 0.20.0 71 */ 72 @Nullable getPassword()73 public abstract String getPassword(); 74 75 /** 76 * Retrieves base url for Elasticsearch. 77 * 78 * @return the url for Elasticsearch. 79 * @since 0.20.0 80 */ getElasticsearchUrl()81 public abstract String getElasticsearchUrl(); 82 83 /** 84 * Retrieves index in Elasticsearch configured for storing trace data. 85 * 86 * @return the Elasticsearch index where the trace will be saved. 87 * @since 0.20.0 88 */ getElasticsearchIndex()89 public abstract String getElasticsearchIndex(); 90 91 /** 92 * Retrieves type in Elasticsearch configured for storing trace data. 93 * 94 * @return the Elasticsearch type where the trace will be saved. 95 * @since 0.20.0 96 */ getElasticsearchType()97 public abstract String getElasticsearchType(); 98 99 /** 100 * Returns the deadline for exporting to Elasticsearch. 101 * 102 * <p>Default value is 10 seconds. 103 * 104 * @return the export deadline. 105 * @since 0.22 106 */ getDeadline()107 public abstract Duration getDeadline(); 108 109 /** 110 * Builds a {@link ElasticsearchTraceConfiguration}. 111 * 112 * @since 0.20.0 113 */ 114 @AutoValue.Builder 115 public abstract static class Builder { 116 Builder()117 Builder() {} 118 autoBuild()119 abstract ElasticsearchTraceConfiguration autoBuild(); 120 121 /** 122 * Sets the name of the app used in traces. 123 * 124 * @param appName the name of app to include in traces. 125 * @return this. 126 * @since 0.20.0 127 */ setAppName(String appName)128 public abstract Builder setAppName(String appName); 129 130 /** 131 * Sets the username of elasticsearch if protected. 132 * 133 * @param userName of Elasticsearch cluster. 134 * @return this. 135 * @since 0.20.0 136 */ setUserName(String userName)137 public abstract Builder setUserName(String userName); 138 139 /** 140 * Sets the password of elasticsearch if protected. 141 * 142 * @param password of Elasticsearch cluster. 143 * @return this. 144 * @since 0.20.0 145 */ setPassword(String password)146 public abstract Builder setPassword(String password); 147 148 /** 149 * Sets the base URL of Elasticsearch. 150 * 151 * @param elasticsearchUrl URL of Elasticsearch. 152 * @return this. 153 * @since 0.20.0 154 */ setElasticsearchUrl(String elasticsearchUrl)155 public abstract Builder setElasticsearchUrl(String elasticsearchUrl); 156 157 /** 158 * Sets the data index of Elasticsearch. 159 * 160 * @param elasticsearchIndex the Elasticsearch index. 161 * @return this. 162 * @since 0.20.0 163 */ setElasticsearchIndex(String elasticsearchIndex)164 public abstract Builder setElasticsearchIndex(String elasticsearchIndex); 165 166 /** 167 * Sets the Elasticsearch type. 168 * 169 * @param elasticsearchType the Elasticsearch type. 170 * @return this. 171 * @since 0.20.0 172 */ setElasticsearchType(String elasticsearchType)173 public abstract Builder setElasticsearchType(String elasticsearchType); 174 175 /** 176 * Sets the deadline for exporting to Elasticsearch. 177 * 178 * @param deadline the export deadline. 179 * @return this 180 * @since 0.22 181 */ setDeadline(Duration deadline)182 public abstract Builder setDeadline(Duration deadline); 183 184 /** 185 * Builder for {@link ElasticsearchTraceConfiguration}. 186 * 187 * @return a {@code ElasticsearchTraceConfiguration}. 188 * @since 0.20.0 189 */ build()190 public ElasticsearchTraceConfiguration build() { 191 ElasticsearchTraceConfiguration elasticsearchTraceConfiguration = autoBuild(); 192 Preconditions.checkArgument( 193 !Strings.isNullOrEmpty(elasticsearchTraceConfiguration.getAppName()), 194 "Invalid App name "); 195 Preconditions.checkArgument( 196 !Strings.isNullOrEmpty(elasticsearchTraceConfiguration.getElasticsearchIndex()), 197 "Invalid Elasticsearch index."); 198 Preconditions.checkArgument( 199 !Strings.isNullOrEmpty(elasticsearchTraceConfiguration.getElasticsearchIndex()), 200 "Invalid Elasticsearch type."); 201 Preconditions.checkArgument( 202 elasticsearchTraceConfiguration.getDeadline().compareTo(ZERO) > 0, 203 "Deadline must be positive."); 204 return elasticsearchTraceConfiguration; 205 } 206 } 207 } 208