Skip to content

Commit 0592c96

Browse files
authored
feat: implement revised version of the monitored resource type discovery and metadata population (#708)
* chore: remove compiler warnings * chore: finalize changes to resource type discovery - add caching mechanism for results. - implement updated heuristics for resource type and labels population. * chore: add unit testing for MonitoredResourceUtil - refactor MonitoredResourceUtil code to allow mocking getenv() and getAttribute(). - unit test each resource type with extra test for GAE FE and custom params.
1 parent 2f76678 commit 0592c96

File tree

7 files changed

+585
-135
lines changed

7 files changed

+585
-135
lines changed

google-cloud-logging/src/main/java/com/google/cloud/logging/HttpRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public RequestMethod apply(String constant) {
6969
};
7070

7171
private static final StringEnumType<RequestMethod> type =
72-
new StringEnumType(RequestMethod.class, CONSTRUCTOR);
72+
new StringEnumType<RequestMethod>(RequestMethod.class, CONSTRUCTOR);
7373

7474
public static final RequestMethod GET = type.createAndRegister("GET");
7575
public static final RequestMethod HEAD = type.createAndRegister("HEAD");

google-cloud-logging/src/main/java/com/google/cloud/logging/LogEntry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ public Builder setSourceLocation(SourceLocation sourceLocation) {
277277
*
278278
* @see <a href="https://cloud.google.com/logging/docs/view/logs_index">Log Entries and Logs</a>
279279
*/
280-
public Builder setPayload(Payload payload) {
280+
public Builder setPayload(Payload<?> payload) {
281281
this.payload = payload;
282282
return this;
283283
}
@@ -434,7 +434,7 @@ public SourceLocation getSourceLocation() {
434434
* @see <a href="https://cloud.google.com/logging/docs/view/logs_index">Log Entries and Logs</a>
435435
*/
436436
@SuppressWarnings("unchecked")
437-
public <T extends Payload> T getPayload() {
437+
public <T extends Payload<?>> T getPayload() {
438438
return (T) payload;
439439
}
440440

google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingHandler.java

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.cloud.logging.Logging.WriteOption;
2323
import com.google.common.collect.ImmutableList;
2424
import com.google.common.collect.ImmutableMap;
25+
import java.time.Instant;
2526
import java.util.Collections;
2627
import java.util.LinkedList;
2728
import java.util.List;
@@ -42,14 +43,38 @@
4243
* Cloud Logging severities:
4344
*
4445
* <table summary="Mapping of Java logging level to Cloud Logging severities">
45-
* <tr><th width="50%">Java Level</th><th>Cloud Logging Severity</th></tr>
46-
* <tr><td>SEVERE</td><td>ERROR</td></tr>
47-
* <tr><td>WARNING</td><td>WARNING</td></tr>
48-
* <tr><td>INFO</td><td>INFO</td></tr>
49-
* <tr><td>CONFIG</td><td>INFO</td></tr>
50-
* <tr><td>FINE</td><td>DEBUG</td></tr>
51-
* <tr><td>FINER</td><td>DEBUG</td></tr>
52-
* <tr><td>FINEST</td><td>DEBUG</td></tr>
46+
* <tr>
47+
* <th width="50%">Java Level</th>
48+
* <th>Cloud Logging Severity</th>
49+
* </tr>
50+
* <tr>
51+
* <td>SEVERE</td>
52+
* <td>ERROR</td>
53+
* </tr>
54+
* <tr>
55+
* <td>WARNING</td>
56+
* <td>WARNING</td>
57+
* </tr>
58+
* <tr>
59+
* <td>INFO</td>
60+
* <td>INFO</td>
61+
* </tr>
62+
* <tr>
63+
* <td>CONFIG</td>
64+
* <td>INFO</td>
65+
* </tr>
66+
* <tr>
67+
* <td>FINE</td>
68+
* <td>DEBUG</td>
69+
* </tr>
70+
* <tr>
71+
* <td>FINER</td>
72+
* <td>DEBUG</td>
73+
* </tr>
74+
* <tr>
75+
* <td>FINEST</td>
76+
* <td>DEBUG</td>
77+
* </tr>
5378
* </table>
5479
*
5580
* <p>Original Java logging levels are added as labels (with {@code levelName} and {@code
@@ -94,9 +119,6 @@
94119
*/
95120
public class LoggingHandler extends Handler {
96121

97-
private static final String HANDLERS_PROPERTY = "handlers";
98-
private static final String ROOT_LOGGER_NAME = "";
99-
private static final String[] NO_HANDLERS = new String[0];
100122
private static final String LEVEL_NAME_KEY = "levelName";
101123
private static final String LEVEL_VALUE_KEY = "levelValue";
102124

@@ -105,8 +127,10 @@ public class LoggingHandler extends Handler {
105127

106128
private volatile Logging logging;
107129

108-
// Logs with the same severity with the base could be more efficiently sent to Cloud.
109-
// Defaults to level of the handler or Level.FINEST if the handler is set to Level.ALL.
130+
// Logs with the same severity with the base could be more efficiently sent to
131+
// Cloud.
132+
// Defaults to level of the handler or Level.FINEST if the handler is set to
133+
// Level.ALL.
110134
// Currently there is no way to modify the base level, see
111135
// https://github.com/googleapis/google-cloud-java/issues/1740 .
112136
private final Level baseLevel;
@@ -204,7 +228,8 @@ public LoggingHandler(
204228

205229
this.enhancers.addAll(enhancersParam);
206230

207-
// In the following line getResourceEnhancers() never returns null (@NotNull attribute)
231+
// In the following line getResourceEnhancers() never returns null (@NotNull
232+
// attribute)
208233
List<LoggingEnhancer> loggingEnhancers = MonitoredResourceUtil.getResourceEnhancers();
209234
this.enhancers.addAll(loggingEnhancers);
210235
} catch (Exception ex) {
@@ -219,8 +244,10 @@ public void publish(LogRecord record) {
219244
if (!isLoggable(record)) {
220245
return;
221246
}
222-
// HACK warning: this logger doesn't work like normal loggers; the log calls are issued
223-
// from another class instead of by itself, so it can't be configured off like normal
247+
// HACK warning: this logger doesn't work like normal loggers; the log calls are
248+
// issued
249+
// from another class instead of by itself, so it can't be configured off like
250+
// normal
224251
// loggers. We have to check the source class name instead.
225252
if ("io.netty.handler.codec.http2.Http2FrameLogger".equals(record.getSourceClassName())) {
226253
return;
@@ -246,7 +273,7 @@ private LogEntry logEntryFor(LogRecord record) throws Exception {
246273
Level level = record.getLevel();
247274
LogEntry.Builder builder =
248275
LogEntry.newBuilder(Payload.StringPayload.of(payload))
249-
.setTimestamp(record.getMillis())
276+
.setTimestamp(Instant.ofEpochMilli(record.getMillis()))
250277
.setSeverity(severityFor(level));
251278

252279
if (!baseLevel.equals(level)) {

google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingOptions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ protected LoggingOptions(Builder builder) {
9898
super(LoggingFactory.class, LoggingRpcFactory.class, builder, new LoggingDefaults());
9999
}
100100

101+
@SuppressWarnings("serial")
101102
private static class LoggingDefaults implements ServiceDefaults<Logging, LoggingOptions> {
102103

103104
@Override

0 commit comments

Comments
 (0)