001package io.freefair.spring.okhttp.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        public String getNamePrefix() {
067            return this.namePrefix;
068        }
069
070        public Map<String, String> getTags() {
071            return this.tags;
072        }
073
074        public void setEnabled(final boolean enabled) {
075            this.enabled = enabled;
076        }
077
078        public void setNamePrefix(final String namePrefix) {
079            this.namePrefix = namePrefix;
080        }
081
082        public void setTags(final Map<String, String> tags) {
083            this.tags = tags;
084        }
085
086        @Override
087        public boolean equals(final Object o) {
088            if (o == this) return true;
089            if (!(o instanceof OkHttpMetricsProperties.ConnectionPoolMetricsProperties)) return false;
090            final OkHttpMetricsProperties.ConnectionPoolMetricsProperties other = (OkHttpMetricsProperties.ConnectionPoolMetricsProperties) o;
091            if (!other.canEqual((Object) this)) return false;
092            if (this.isEnabled() != other.isEnabled()) return false;
093            final Object this$namePrefix = this.getNamePrefix();
094            final Object other$namePrefix = other.getNamePrefix();
095            if (this$namePrefix == null ? other$namePrefix != null : !this$namePrefix.equals(other$namePrefix)) return false;
096            final Object this$tags = this.getTags();
097            final Object other$tags = other.getTags();
098            if (this$tags == null ? other$tags != null : !this$tags.equals(other$tags)) return false;
099            return true;
100        }
101
102        protected boolean canEqual(final Object other) {
103            return other instanceof OkHttpMetricsProperties.ConnectionPoolMetricsProperties;
104        }
105
106        @Override
107        public int hashCode() {
108            final int PRIME = 59;
109            int result = 1;
110            result = result * PRIME + (this.isEnabled() ? 79 : 97);
111            final Object $namePrefix = this.getNamePrefix();
112            result = result * PRIME + ($namePrefix == null ? 43 : $namePrefix.hashCode());
113            final Object $tags = this.getTags();
114            result = result * PRIME + ($tags == null ? 43 : $tags.hashCode());
115            return result;
116        }
117
118        @Override
119        public String toString() {
120            return "OkHttpMetricsProperties.ConnectionPoolMetricsProperties(enabled=" + this.isEnabled() + ", namePrefix=" + this.getNamePrefix() + ", tags=" + this.getTags() + ")";
121        }
122    }
123
124    public OkHttpMetricsProperties() {
125    }
126
127    public boolean isEnabled() {
128        return this.enabled;
129    }
130
131    /**
132     * Name for the metrics.
133     */
134    public String getName() {
135        return this.name;
136    }
137
138    /**
139     * Whether to include the {@code host} tag.
140     *
141     * @see OkHttpMetricsEventListener.Builder#includeHostTag(boolean)
142     */
143    public boolean isIncludeHostTag() {
144        return this.includeHostTag;
145    }
146
147    /**
148     * Tag keys for {@link Request#tag()} or {@link Request#tag(Class)}.
149     *
150     * @see OkHttpMetricsEventListener.Builder#requestTagKeys(Iterable)
151     */
152    public List<String> getRequestTagKeys() {
153        return this.requestTagKeys;
154    }
155
156    /**
157     * @see OkHttpMetricsEventListener.Builder#tags(Iterable)
158     */
159    public Map<String, String> getTags() {
160        return this.tags;
161    }
162
163    public ConnectionPoolMetricsProperties getPool() {
164        return this.pool;
165    }
166
167    public void setEnabled(final boolean enabled) {
168        this.enabled = enabled;
169    }
170
171    /**
172     * Name for the metrics.
173     */
174    public void setName(final String name) {
175        this.name = name;
176    }
177
178    /**
179     * Whether to include the {@code host} tag.
180     *
181     * @see OkHttpMetricsEventListener.Builder#includeHostTag(boolean)
182     */
183    public void setIncludeHostTag(final boolean includeHostTag) {
184        this.includeHostTag = includeHostTag;
185    }
186
187    /**
188     * Tag keys for {@link Request#tag()} or {@link Request#tag(Class)}.
189     *
190     * @see OkHttpMetricsEventListener.Builder#requestTagKeys(Iterable)
191     */
192    public void setRequestTagKeys(final List<String> requestTagKeys) {
193        this.requestTagKeys = requestTagKeys;
194    }
195
196    /**
197     * @see OkHttpMetricsEventListener.Builder#tags(Iterable)
198     */
199    public void setTags(final Map<String, String> tags) {
200        this.tags = tags;
201    }
202
203    @Override
204    public boolean equals(final Object o) {
205        if (o == this) return true;
206        if (!(o instanceof OkHttpMetricsProperties)) return false;
207        final OkHttpMetricsProperties other = (OkHttpMetricsProperties) o;
208        if (!other.canEqual((Object) this)) return false;
209        if (this.isEnabled() != other.isEnabled()) return false;
210        if (this.isIncludeHostTag() != other.isIncludeHostTag()) return false;
211        final Object this$name = this.getName();
212        final Object other$name = other.getName();
213        if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
214        final Object this$requestTagKeys = this.getRequestTagKeys();
215        final Object other$requestTagKeys = other.getRequestTagKeys();
216        if (this$requestTagKeys == null ? other$requestTagKeys != null : !this$requestTagKeys.equals(other$requestTagKeys)) return false;
217        final Object this$tags = this.getTags();
218        final Object other$tags = other.getTags();
219        if (this$tags == null ? other$tags != null : !this$tags.equals(other$tags)) return false;
220        final Object this$pool = this.getPool();
221        final Object other$pool = other.getPool();
222        if (this$pool == null ? other$pool != null : !this$pool.equals(other$pool)) return false;
223        return true;
224    }
225
226    protected boolean canEqual(final Object other) {
227        return other instanceof OkHttpMetricsProperties;
228    }
229
230    @Override
231    public int hashCode() {
232        final int PRIME = 59;
233        int result = 1;
234        result = result * PRIME + (this.isEnabled() ? 79 : 97);
235        result = result * PRIME + (this.isIncludeHostTag() ? 79 : 97);
236        final Object $name = this.getName();
237        result = result * PRIME + ($name == null ? 43 : $name.hashCode());
238        final Object $requestTagKeys = this.getRequestTagKeys();
239        result = result * PRIME + ($requestTagKeys == null ? 43 : $requestTagKeys.hashCode());
240        final Object $tags = this.getTags();
241        result = result * PRIME + ($tags == null ? 43 : $tags.hashCode());
242        final Object $pool = this.getPool();
243        result = result * PRIME + ($pool == null ? 43 : $pool.hashCode());
244        return result;
245    }
246
247    @Override
248    public String toString() {
249        return "OkHttpMetricsProperties(enabled=" + this.isEnabled() + ", name=" + this.getName() + ", includeHostTag=" + this.isIncludeHostTag() + ", requestTagKeys=" + this.getRequestTagKeys() + ", tags=" + this.getTags() + ", pool=" + this.getPool() + ")";
250    }
251}