Skip to content

Commit

Permalink
feat: implement revised version of the monitored resource type discov…
Browse files Browse the repository at this point in the history
…ery 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.
  • Loading branch information
minherz committed Oct 14, 2021
1 parent 2f76678 commit 0592c96
Show file tree
Hide file tree
Showing 7 changed files with 585 additions and 135 deletions.
Expand Up @@ -69,7 +69,7 @@ public RequestMethod apply(String constant) {
};

private static final StringEnumType<RequestMethod> type =
new StringEnumType(RequestMethod.class, CONSTRUCTOR);
new StringEnumType<RequestMethod>(RequestMethod.class, CONSTRUCTOR);

public static final RequestMethod GET = type.createAndRegister("GET");
public static final RequestMethod HEAD = type.createAndRegister("HEAD");
Expand Down
Expand Up @@ -277,7 +277,7 @@ public Builder setSourceLocation(SourceLocation sourceLocation) {
*
* @see <a href="https://cloud.google.com/logging/docs/view/logs_index">Log Entries and Logs</a>
*/
public Builder setPayload(Payload payload) {
public Builder setPayload(Payload<?> payload) {
this.payload = payload;
return this;
}
Expand Down Expand Up @@ -434,7 +434,7 @@ public SourceLocation getSourceLocation() {
* @see <a href="https://cloud.google.com/logging/docs/view/logs_index">Log Entries and Logs</a>
*/
@SuppressWarnings("unchecked")
public <T extends Payload> T getPayload() {
public <T extends Payload<?>> T getPayload() {
return (T) payload;
}

Expand Down
Expand Up @@ -22,6 +22,7 @@
import com.google.cloud.logging.Logging.WriteOption;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.time.Instant;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -42,14 +43,38 @@
* Cloud Logging severities:
*
* <table summary="Mapping of Java logging level to Cloud Logging severities">
* <tr><th width="50%">Java Level</th><th>Cloud Logging Severity</th></tr>
* <tr><td>SEVERE</td><td>ERROR</td></tr>
* <tr><td>WARNING</td><td>WARNING</td></tr>
* <tr><td>INFO</td><td>INFO</td></tr>
* <tr><td>CONFIG</td><td>INFO</td></tr>
* <tr><td>FINE</td><td>DEBUG</td></tr>
* <tr><td>FINER</td><td>DEBUG</td></tr>
* <tr><td>FINEST</td><td>DEBUG</td></tr>
* <tr>
* <th width="50%">Java Level</th>
* <th>Cloud Logging Severity</th>
* </tr>
* <tr>
* <td>SEVERE</td>
* <td>ERROR</td>
* </tr>
* <tr>
* <td>WARNING</td>
* <td>WARNING</td>
* </tr>
* <tr>
* <td>INFO</td>
* <td>INFO</td>
* </tr>
* <tr>
* <td>CONFIG</td>
* <td>INFO</td>
* </tr>
* <tr>
* <td>FINE</td>
* <td>DEBUG</td>
* </tr>
* <tr>
* <td>FINER</td>
* <td>DEBUG</td>
* </tr>
* <tr>
* <td>FINEST</td>
* <td>DEBUG</td>
* </tr>
* </table>
*
* <p>Original Java logging levels are added as labels (with {@code levelName} and {@code
Expand Down Expand Up @@ -94,9 +119,6 @@
*/
public class LoggingHandler extends Handler {

private static final String HANDLERS_PROPERTY = "handlers";
private static final String ROOT_LOGGER_NAME = "";
private static final String[] NO_HANDLERS = new String[0];
private static final String LEVEL_NAME_KEY = "levelName";
private static final String LEVEL_VALUE_KEY = "levelValue";

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

private volatile Logging logging;

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

this.enhancers.addAll(enhancersParam);

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

if (!baseLevel.equals(level)) {
Expand Down
Expand Up @@ -98,6 +98,7 @@ protected LoggingOptions(Builder builder) {
super(LoggingFactory.class, LoggingRpcFactory.class, builder, new LoggingDefaults());
}

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

@Override
Expand Down

0 comments on commit 0592c96

Please sign in to comment.