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