|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 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 | +package com.google.cloud.logging; |
| 17 | + |
| 18 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | + |
| 20 | +import com.google.common.base.Function; |
| 21 | +import com.google.common.base.MoreObjects; |
| 22 | +import com.google.logging.v2.LogExclusion; |
| 23 | +import com.google.protobuf.Timestamp; |
| 24 | +import java.util.Objects; |
| 25 | + |
| 26 | +/** |
| 27 | + * Specifies a set of log entries that are not to be stored in Logging. If your GCP resource |
| 28 | + * receives a large volume of logs, you can use exclusions to reduce your chargeable logs. |
| 29 | + * Exclusions are processed after log sinks, so you can export log entries before they are excluded. |
| 30 | + * Note that organization-level and folder-level exclusions don't apply to child resources, and that |
| 31 | + * you can't exclude audit log entries. |
| 32 | + */ |
| 33 | +public class Exclusion { |
| 34 | + |
| 35 | + static final Function<LogExclusion, Exclusion> FROM_PROTOBUF_FUNCTION = |
| 36 | + new Function<LogExclusion, Exclusion>() { |
| 37 | + @Override |
| 38 | + public Exclusion apply(LogExclusion exclusionPb) { |
| 39 | + return exclusionPb != null ? Exclusion.fromProtobuf(exclusionPb) : null; |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + static final Function<Exclusion, LogExclusion> TO_PROTOBUF_FUNCTION = |
| 44 | + new Function<Exclusion, LogExclusion>() { |
| 45 | + @Override |
| 46 | + public LogExclusion apply(Exclusion exclusion) { |
| 47 | + return exclusion != null ? exclusion.toProtobuf() : null; |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + private String name; |
| 52 | + private String description; |
| 53 | + private String filter; |
| 54 | + private boolean disabled; |
| 55 | + private Timestamp createTime; |
| 56 | + private Timestamp updateTime; |
| 57 | + |
| 58 | + /** A builder for {@code Exclusion} objects. */ |
| 59 | + public static class Builder { |
| 60 | + |
| 61 | + private String name; |
| 62 | + private String description; |
| 63 | + private String filter; |
| 64 | + private boolean disabled; |
| 65 | + private Timestamp createTime; |
| 66 | + private Timestamp updateTime; |
| 67 | + |
| 68 | + private Builder(String name, String filter) { |
| 69 | + this.name = checkNotNull(name); |
| 70 | + this.filter = checkNotNull(filter); |
| 71 | + } |
| 72 | + |
| 73 | + private Builder(Exclusion exclusion) { |
| 74 | + this.name = exclusion.name; |
| 75 | + this.description = exclusion.description; |
| 76 | + this.filter = exclusion.filter; |
| 77 | + this.disabled = exclusion.disabled; |
| 78 | + this.createTime = exclusion.createTime; |
| 79 | + this.updateTime = exclusion.updateTime; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * [Required] A client-assigned identifier, such as "load-balancer-exclusion". Identifiers are |
| 84 | + * limited to 100 characters and can include only letters, digits, underscores, hyphens, and |
| 85 | + * periods. First character has to be alphanumeric. |
| 86 | + */ |
| 87 | + public Builder setName(String name) { |
| 88 | + this.name = checkNotNull(name); |
| 89 | + return this; |
| 90 | + } |
| 91 | + |
| 92 | + /** [Optional] A description of this exclusion. */ |
| 93 | + public Builder setDescription(String description) { |
| 94 | + this.description = description; |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * [Required] An advanced logs filter that matches the log entries to be excluded. By using the |
| 100 | + * sample function, you can exclude less than 100% of the matching log entries. |
| 101 | + */ |
| 102 | + public Builder setFilter(String filter) { |
| 103 | + this.filter = checkNotNull(filter); |
| 104 | + return this; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * [Optional] If set to True, then this exclusion is disabled and it does not exclude any log |
| 109 | + * entries. |
| 110 | + */ |
| 111 | + public Builder setDisabled(boolean disabled) { |
| 112 | + this.disabled = disabled; |
| 113 | + return this; |
| 114 | + } |
| 115 | + |
| 116 | + /** [Output only] The creation timestamp of the exclusion. */ |
| 117 | + public Builder setCreateTime(Timestamp createTime) { |
| 118 | + this.createTime = createTime; |
| 119 | + return this; |
| 120 | + } |
| 121 | + |
| 122 | + /** [Output only] The last update timestamp of the exclusion. */ |
| 123 | + public Builder setUpdateTime(Timestamp updateTime) { |
| 124 | + this.updateTime = updateTime; |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
| 128 | + /** Creates a {@code Exclusion} object. */ |
| 129 | + public Exclusion build() { |
| 130 | + return new Exclusion(this); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + Exclusion(Builder builder) { |
| 135 | + this.name = checkNotNull(builder.name); |
| 136 | + this.description = builder.description; |
| 137 | + this.filter = checkNotNull(builder.filter); |
| 138 | + this.disabled = builder.disabled; |
| 139 | + this.createTime = builder.createTime; |
| 140 | + this.updateTime = builder.updateTime; |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public String toString() { |
| 145 | + return MoreObjects.toStringHelper(this) |
| 146 | + .add("name", name) |
| 147 | + .add("description", description) |
| 148 | + .add("filter", filter) |
| 149 | + .add("disabled", disabled) |
| 150 | + .add("createTime", createTime) |
| 151 | + .add("updateTime", updateTime) |
| 152 | + .toString(); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public boolean equals(Object o) { |
| 157 | + if (this == o) { |
| 158 | + return true; |
| 159 | + } |
| 160 | + if (o == null || getClass() != o.getClass()) { |
| 161 | + return false; |
| 162 | + } |
| 163 | + Exclusion exclusion = (Exclusion) o; |
| 164 | + return disabled == exclusion.disabled |
| 165 | + && Objects.equals(name, exclusion.name) |
| 166 | + && Objects.equals(description, exclusion.description) |
| 167 | + && Objects.equals(filter, exclusion.filter) |
| 168 | + && Objects.equals(createTime, exclusion.createTime) |
| 169 | + && Objects.equals(updateTime, exclusion.updateTime); |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public int hashCode() { |
| 174 | + return Objects.hash(name, description, filter, disabled, createTime, updateTime); |
| 175 | + } |
| 176 | + |
| 177 | + /** Returns the name of log exclusion. */ |
| 178 | + public String getName() { |
| 179 | + return name; |
| 180 | + } |
| 181 | + |
| 182 | + /** Returns an optional description of an exclusion. Used for documentation purpose. */ |
| 183 | + public String getDescription() { |
| 184 | + return description; |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Returns an advanced logs filter. Example: {@code resource.type=gcs_bucket severity<ERROR |
| 189 | + * sample(insertId, 0.99)}. |
| 190 | + * |
| 191 | + * @see <a href="https://cloud.google.com/logging/docs/view/advanced-queries">Advanced Log |
| 192 | + * Filters</a> |
| 193 | + */ |
| 194 | + public String getFilter() { |
| 195 | + return filter; |
| 196 | + } |
| 197 | + |
| 198 | + /** If set to True, then this exclusion is disabled and it does not exclude any log entries. */ |
| 199 | + public boolean isDisabled() { |
| 200 | + return disabled; |
| 201 | + } |
| 202 | + |
| 203 | + /** Returns the creation timestamp of the exclusion. */ |
| 204 | + public Timestamp getCreateTime() { |
| 205 | + return createTime; |
| 206 | + } |
| 207 | + |
| 208 | + /** Returns the last update timestamp of the exclusion. */ |
| 209 | + public Timestamp getUpdateTime() { |
| 210 | + return updateTime; |
| 211 | + } |
| 212 | + |
| 213 | + /** Returns a builder for this {@code Exclusion} object. */ |
| 214 | + public Builder toBuilder() { |
| 215 | + return new Builder(this); |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * Returns a builder for {@code Exclusion} objects given the name of the exclusion and its filter. |
| 220 | + */ |
| 221 | + public static Builder newBuilder(String name, String filter) { |
| 222 | + return new Builder(name, filter); |
| 223 | + } |
| 224 | + |
| 225 | + /** Creates a {@code Exclusion} object given the name of the exclusion and its filter. */ |
| 226 | + public static Exclusion of(String name, String filter) { |
| 227 | + return new Builder(name, filter).build(); |
| 228 | + } |
| 229 | + |
| 230 | + LogExclusion toProtobuf() { |
| 231 | + LogExclusion.Builder builder = |
| 232 | + LogExclusion.newBuilder().setName(name).setFilter(filter).setDisabled(disabled); |
| 233 | + if (description != null) { |
| 234 | + builder.setDescription(description); |
| 235 | + } |
| 236 | + if (createTime != null) { |
| 237 | + builder.setCreateTime(createTime); |
| 238 | + } |
| 239 | + if (updateTime != null) { |
| 240 | + builder.setUpdateTime(updateTime); |
| 241 | + } |
| 242 | + return builder.build(); |
| 243 | + } |
| 244 | + |
| 245 | + static Exclusion fromProtobuf(LogExclusion exclusionPb) { |
| 246 | + Exclusion.Builder builder = newBuilder(exclusionPb.getName(), exclusionPb.getFilter()); |
| 247 | + builder.setDisabled(exclusionPb.getDisabled()); |
| 248 | + if (!exclusionPb.getDescription().equals("")) { |
| 249 | + builder.setDescription(exclusionPb.getDescription()); |
| 250 | + } |
| 251 | + if (exclusionPb.hasCreateTime()) { |
| 252 | + builder.setCreateTime(exclusionPb.getCreateTime()); |
| 253 | + } |
| 254 | + if (exclusionPb.hasUpdateTime()) { |
| 255 | + builder.setUpdateTime(exclusionPb.getUpdateTime()); |
| 256 | + } |
| 257 | + return builder.build(); |
| 258 | + } |
| 259 | +} |
0 commit comments