|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 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 com.google.cloud.logging; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | + |
| 21 | +import com.google.common.base.MoreObjects; |
| 22 | +import com.google.logging.v2.LogMetric; |
| 23 | + |
| 24 | +import java.io.Serializable; |
| 25 | +import java.util.Objects; |
| 26 | + |
| 27 | +/** |
| 28 | + * Google Cloud Logging metrics describe logs-based metric. The value of the metric is the number of |
| 29 | + * log entries that match a logs filter (see {@link #filter()}). |
| 30 | + * |
| 31 | + * @see <a href="https://cloud.google.com/logging/docs/view/logs_based_metrics">Logs-based Metrics |
| 32 | + * </a> |
| 33 | + */ |
| 34 | +public class MetricInfo implements Serializable { |
| 35 | + |
| 36 | + private static final long serialVersionUID = 666208243838820325L; |
| 37 | + |
| 38 | + private final String name; |
| 39 | + private final String description; |
| 40 | + private final String filter; |
| 41 | + |
| 42 | + /** |
| 43 | + * A builder for {@code MetricInfo} objects. |
| 44 | + */ |
| 45 | + public abstract static class Builder { |
| 46 | + |
| 47 | + /** |
| 48 | + * Sets the name of the metric. Example: {@code severe-errors}. Metric identifiers are |
| 49 | + * limited to 1000 characters and can include only the following characters: {@code A-Z}, |
| 50 | + * {@code a-z}, {@code 0-9}, and the special characters {@code _-.,+!*',()%/\}. The |
| 51 | + * forward-slash character ({@code /}) denotes a hierarchy of name pieces, and it cannot be the |
| 52 | + * first character of the name. |
| 53 | + */ |
| 54 | + public abstract Builder name(String name); |
| 55 | + |
| 56 | + /** |
| 57 | + * Sets an optional description for this metric. Used for documentation purpose. |
| 58 | + */ |
| 59 | + public abstract Builder description(String description); |
| 60 | + |
| 61 | + /** |
| 62 | + * Sets an advanced logs filter. The value of the metric is the number of log entries that match |
| 63 | + * this filter. Example: {@code logName=projects/my-projectid/logs/syslog AND severity>=ERROR}. |
| 64 | + * |
| 65 | + * @see <a href="https://cloud.google.com/logging/docs/view/advanced_filters">Advanced Log |
| 66 | + * Filters</a> |
| 67 | + */ |
| 68 | + public abstract Builder filter(String filter); |
| 69 | + |
| 70 | + /** |
| 71 | + * Creates a {@code MetricInfo} object for this builder. |
| 72 | + */ |
| 73 | + public abstract MetricInfo build(); |
| 74 | + } |
| 75 | + |
| 76 | + static final class BuilderImpl extends Builder { |
| 77 | + |
| 78 | + private String name; |
| 79 | + private String description; |
| 80 | + private String filter; |
| 81 | + |
| 82 | + BuilderImpl(String name, String filter) { |
| 83 | + this.name = name; |
| 84 | + this.filter = filter; |
| 85 | + } |
| 86 | + |
| 87 | + BuilderImpl(MetricInfo metric) { |
| 88 | + this.name = metric.name; |
| 89 | + this.description = metric.description; |
| 90 | + this.filter = metric.filter; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public Builder name(String name) { |
| 95 | + this.name = name; |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public Builder description(String description) { |
| 101 | + this.description = description; |
| 102 | + return this; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public Builder filter(String filter) { |
| 107 | + this.filter = filter; |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public MetricInfo build() { |
| 113 | + return new MetricInfo(this); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + MetricInfo(BuilderImpl builder) { |
| 118 | + this.name = checkNotNull(builder.name); |
| 119 | + this.filter = checkNotNull(builder.filter); |
| 120 | + this.description = builder.description; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Returns the name of the metric. Example: {@code severe-errors}. Metric identifiers are |
| 125 | + * limited to 1000 characters and can include only the following characters: {@code A-Z}, |
| 126 | + * {@code a-z}, {@code 0-9}, and the special characters {@code _-.,+!*',()%/\}. The |
| 127 | + * forward-slash character ({@code /}) denotes a hierarchy of name pieces, and it cannot be the |
| 128 | + * first character of the name. |
| 129 | + */ |
| 130 | + public String name() { |
| 131 | + return name; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Returns an optional description for this metric. Used for documentation purpose. |
| 136 | + */ |
| 137 | + public String description() { |
| 138 | + return description; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Returns an advanced logs filter. The value of the metric is the number of log entries that |
| 143 | + * match this filter. Example: |
| 144 | + * {@code logName=projects/my-projectid/logs/syslog AND severity>=ERROR}. |
| 145 | + * |
| 146 | + * @see <a href="https://cloud.google.com/logging/docs/view/advanced_filters">Advanced Log |
| 147 | + * Filters</a> |
| 148 | + */ |
| 149 | + public String filter() { |
| 150 | + return filter; |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public String toString() { |
| 155 | + return MoreObjects.toStringHelper(this) |
| 156 | + .add("name", name) |
| 157 | + .add("description", description) |
| 158 | + .add("filter", filter) |
| 159 | + .toString(); |
| 160 | + } |
| 161 | + |
| 162 | + final boolean baseEquals(MetricInfo metricInfo) { |
| 163 | + return Objects.equals(name, metricInfo.name) |
| 164 | + && Objects.equals(description, metricInfo.description) |
| 165 | + && Objects.equals(filter, metricInfo.filter); |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public boolean equals(Object obj) { |
| 170 | + if (obj == this) { |
| 171 | + return true; |
| 172 | + } |
| 173 | + if (obj == null || !(obj.getClass().equals(MetricInfo.class))) { |
| 174 | + return false; |
| 175 | + } |
| 176 | + return baseEquals((MetricInfo) obj); |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public int hashCode() { |
| 181 | + return Objects.hash(name, description, filter); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Returns a builder for this {@code MetricInfo} object. |
| 186 | + */ |
| 187 | + public Builder toBuilder() { |
| 188 | + return new BuilderImpl(this); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Returns a builder for {@code MetricInfo} objects given the name of the metric and its filter. |
| 193 | + */ |
| 194 | + public static Builder builder(String name, String filter) { |
| 195 | + return new BuilderImpl(name, filter); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Creates a {@code MetricInfo} object given the name of the metric and its filter. |
| 200 | + */ |
| 201 | + public static MetricInfo of(String name, String filter) { |
| 202 | + return new BuilderImpl(name, filter).build(); |
| 203 | + } |
| 204 | + |
| 205 | + LogMetric toPb() { |
| 206 | + LogMetric.Builder builder = LogMetric.newBuilder() |
| 207 | + .setName(name) |
| 208 | + .setFilter(filter); |
| 209 | + if (description != null) { |
| 210 | + builder.setDescription(description); |
| 211 | + } |
| 212 | + return builder.build(); |
| 213 | + } |
| 214 | + |
| 215 | + static MetricInfo fromPb(LogMetric metricPb) { |
| 216 | + Builder builder = builder(metricPb.getName(), metricPb.getFilter()); |
| 217 | + if (!metricPb.getDescription().equals("")) { |
| 218 | + builder.description(metricPb.getDescription()); |
| 219 | + } |
| 220 | + return builder.build(); |
| 221 | + } |
| 222 | +} |
0 commit comments