001package io.freefair.spring.okhttp.autoconfigure.metrics;
002
003import io.micrometer.core.instrument.binder.okhttp3.OkHttpConnectionPoolMetrics;
004import io.micrometer.core.instrument.binder.okhttp3.OkHttpMetricsEventListener;
005import okhttp3.Request;
006import org.springframework.boot.context.properties.ConfigurationProperties;
007import org.springframework.boot.context.properties.NestedConfigurationProperty;
008import java.util.ArrayList;
009import java.util.HashMap;
010import java.util.List;
011import java.util.Map;
012
013/**
014 * @author Lars Grefer
015 * @see OkHttpMetricsEventListener
016 * @see OkHttpMetricsAutoConfiguration
017 */
018@ConfigurationProperties("okhttp.metrics")
019public class OkHttpMetricsProperties {
020    private boolean enabled = true;
021    /**
022     * Name for the metrics.
023     */
024    private String name = "okhttp";
025    /**
026     * Whether to include the {@code host} tag.
027     *
028     * @see OkHttpMetricsEventListener.Builder#includeHostTag(boolean)
029     */
030    private boolean includeHostTag = true;
031    /**
032     * Tag keys for {@link Request#tag()} or {@link Request#tag(Class)}.
033     *
034     * @see OkHttpMetricsEventListener.Builder#requestTagKeys(Iterable)
035     */
036    private List<String> requestTagKeys = new ArrayList<>();
037    /**
038     * @see OkHttpMetricsEventListener.Builder#tags(Iterable)
039     */
040    private Map<String, String> tags = new HashMap<>();
041    @NestedConfigurationProperty
042    private final ConnectionPoolMetricsProperties pool = new ConnectionPoolMetricsProperties();
043
044
045    /**
046     * @see OkHttpConnectionPoolMetrics
047     */
048    public static class ConnectionPoolMetricsProperties {
049        private boolean enabled = true;
050        /**
051         * @see OkHttpConnectionPoolMetrics#namePrefix
052         */
053        private String namePrefix = "okhttp.pool";
054        /**
055         * @see OkHttpConnectionPoolMetrics#tags
056         */
057        private Map<String, String> tags = new HashMap<>();
058
059        public ConnectionPoolMetricsProperties() {
060        }
061
062        public boolean isEnabled() {
063            return this.enabled;
064        }
065
066        /**
067         * @see OkHttpConnectionPoolMetrics#namePrefix
068         */
069        public String getNamePrefix() {
070            return this.namePrefix;
071        }
072
073        /**
074         * @see OkHttpConnectionPoolMetrics#tags
075         */
076        public Map<String, String> getTags() {
077            return this.tags;
078        }
079
080        public void setEnabled(final boolean enabled) {
081            this.enabled = enabled;
082        }
083
084        /**
085         * @see OkHttpConnectionPoolMetrics#namePrefix
086         */
087        public void setNamePrefix(final String namePrefix) {
088            this.namePrefix = namePrefix;
089        }
090
091        /**
092         * @see OkHttpConnectionPoolMetrics#tags
093         */
094        public void setTags(final Map<String, String> tags) {
095            this.tags = tags;
096        }
097
098        @Override
099        public boolean equals(final Object o) {
100            if (o == this) return true;
101            if (!(o instanceof OkHttpMetricsProperties.ConnectionPoolMetricsProperties)) return false;
102            final OkHttpMetricsProperties.ConnectionPoolMetricsProperties other = (OkHttpMetricsProperties.ConnectionPoolMetricsProperties) o;
103            if (!other.canEqual((Object) this)) return false;
104            if (this.isEnabled() != other.isEnabled()) return false;
105            final Object this$namePrefix = this.getNamePrefix();
106            final Object other$namePrefix = other.getNamePrefix();
107            if (this$namePrefix == null ? other$namePrefix != null : !this$namePrefix.equals(other$namePrefix)) return false;
108            final Object this$tags = this.getTags();
109            final Object other$tags = other.getTags();
110            if (this$tags == null ? other$tags != null : !this$tags.equals(other$tags)) return false;
111            return true;
112        }
113
114        protected boolean canEqual(final Object other) {
115            return other instanceof OkHttpMetricsProperties.ConnectionPoolMetricsProperties;
116        }
117
118        @Override
119        public int hashCode() {
120            final int PRIME = 59;
121            int result = 1;
122            result = result * PRIME + (this.isEnabled() ? 79 : 97);
123            final Object $namePrefix = this.getNamePrefix();
124            result = result * PRIME + ($namePrefix == null ? 43 : $namePrefix.hashCode());
125            final Object $tags = this.getTags();
126            result = result * PRIME + ($tags == null ? 43 : $tags.hashCode());
127            return result;
128        }
129
130        @Override
131        public String toString() {
132            return "OkHttpMetricsProperties.ConnectionPoolMetricsProperties(enabled=" + this.isEnabled() + ", namePrefix=" + this.getNamePrefix() + ", tags=" + this.getTags() + ")";
133        }
134    }
135
136    public OkHttpMetricsProperties() {
137    }
138
139    public boolean isEnabled() {
140        return this.enabled;
141    }
142
143    /**
144     * Name for the metrics.
145     */
146    public String getName() {
147        return this.name;
148    }
149
150    /**
151     * Whether to include the {@code host} tag.
152     *
153     * @see OkHttpMetricsEventListener.Builder#includeHostTag(boolean)
154     */
155    public boolean isIncludeHostTag() {
156        return this.includeHostTag;
157    }
158
159    /**
160     * Tag keys for {@link Request#tag()} or {@link Request#tag(Class)}.
161     *
162     * @see OkHttpMetricsEventListener.Builder#requestTagKeys(Iterable)
163     */
164    public List<String> getRequestTagKeys() {
165        return this.requestTagKeys;
166    }
167
168    /**
169     * @see OkHttpMetricsEventListener.Builder#tags(Iterable)
170     */
171    public Map<String, String> getTags() {
172        return this.tags;
173    }
174
175    public ConnectionPoolMetricsProperties getPool() {
176        return this.pool;
177    }
178
179    public void setEnabled(final boolean enabled) {
180        this.enabled = enabled;
181    }
182
183    /**
184     * Name for the metrics.
185     */
186    public void setName(final String name) {
187        this.name = name;
188    }
189
190    /**
191     * Whether to include the {@code host} tag.
192     *
193     * @see OkHttpMetricsEventListener.Builder#includeHostTag(boolean)
194     */
195    public void setIncludeHostTag(final boolean includeHostTag) {
196        this.includeHostTag = includeHostTag;
197    }
198
199    /**
200     * Tag keys for {@link Request#tag()} or {@link Request#tag(Class)}.
201     *
202     * @see OkHttpMetricsEventListener.Builder#requestTagKeys(Iterable)
203     */
204    public void setRequestTagKeys(final List<String> requestTagKeys) {
205        this.requestTagKeys = requestTagKeys;
206    }
207
208    /**
209     * @see OkHttpMetricsEventListener.Builder#tags(Iterable)
210     */
211    public void setTags(final Map<String, String> tags) {
212        this.tags = tags;
213    }
214
215    @Override
216    public boolean equals(final Object o) {
217        if (o == this) return true;
218        if (!(o instanceof OkHttpMetricsProperties)) return false;
219        final OkHttpMetricsProperties other = (OkHttpMetricsProperties) o;
220        if (!other.canEqual((Object) this)) return false;
221        if (this.isEnabled() != other.isEnabled()) return false;
222        if (this.isIncludeHostTag() != other.isIncludeHostTag()) return false;
223        final Object this$name = this.getName();
224        final Object other$name = other.getName();
225        if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
226        final Object this$requestTagKeys = this.getRequestTagKeys();
227        final Object other$requestTagKeys = other.getRequestTagKeys();
228        if (this$requestTagKeys == null ? other$requestTagKeys != null : !this$requestTagKeys.equals(other$requestTagKeys)) return false;
229        final Object this$tags = this.getTags();
230        final Object other$tags = other.getTags();
231        if (this$tags == null ? other$tags != null : !this$tags.equals(other$tags)) return false;
232        final Object this$pool = this.getPool();
233        final Object other$pool = other.getPool();
234        if (this$pool == null ? other$pool != null : !this$pool.equals(other$pool)) return false;
235        return true;
236    }
237
238    protected boolean canEqual(final Object other) {
239        return other instanceof OkHttpMetricsProperties;
240    }
241
242    @Override
243    public int hashCode() {
244        final int PRIME = 59;
245        int result = 1;
246        result = result * PRIME + (this.isEnabled() ? 79 : 97);
247        result = result * PRIME + (this.isIncludeHostTag() ? 79 : 97);
248        final Object $name = this.getName();
249        result = result * PRIME + ($name == null ? 43 : $name.hashCode());
250        final Object $requestTagKeys = this.getRequestTagKeys();
251        result = result * PRIME + ($requestTagKeys == null ? 43 : $requestTagKeys.hashCode());
252        final Object $tags = this.getTags();
253        result = result * PRIME + ($tags == null ? 43 : $tags.hashCode());
254        final Object $pool = this.getPool();
255        result = result * PRIME + ($pool == null ? 43 : $pool.hashCode());
256        return result;
257    }
258
259    @Override
260    public String toString() {
261        return "OkHttpMetricsProperties(enabled=" + this.isEnabled() + ", name=" + this.getName() + ", includeHostTag=" + this.isIncludeHostTag() + ", requestTagKeys=" + this.getRequestTagKeys() + ", tags=" + this.getTags() + ", pool=" + this.getPool() + ")";
262    }
263}