Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement revised version of the monitored resource type discovery and metadata population #708

Merged
merged 4 commits into from Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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() {
minherz marked this conversation as resolved.
Show resolved Hide resolved
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