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: Add destination property into LogEntry #720

Merged
merged 11 commits into from Oct 24, 2021
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -58,13 +58,13 @@ implementation 'com.google.cloud:google-cloud-logging'
If you are using Gradle without BOM, add this to your dependencies

```Groovy
implementation 'com.google.cloud:google-cloud-logging:3.2.0'
implementation 'com.google.cloud:google-cloud-logging:3.3.0'
```

If you are using SBT, add this to your dependencies

```Scala
libraryDependencies += "com.google.cloud" % "google-cloud-logging" % "3.2.0"
libraryDependencies += "com.google.cloud" % "google-cloud-logging" % "3.3.0"
```

## Authentication
Expand Down
@@ -0,0 +1,129 @@
/*
* Copyright 2021 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
*
* https://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.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.logging.v2.LogName;
import java.util.Map;

/**
* Class for specifying resource name of the log to which this log entry belongs (see 'logName'
* parameter in https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry)
*/
public final class LogDestinationName extends Option {

enum DestinationType implements Option.OptionType {
PROJECT,
FOLDER,
ORGANIZATION,
BILLINGACCOUNT;

@SuppressWarnings("unchecked")
<T> T get(Map<Option.OptionType, ?> options) {
return (T) options.get(this);
}
}

private LogDestinationName(Option.OptionType option, Object value) {
super(option, value);
checkArgument(!checkNotNull(value).toString().trim().isEmpty());
}

/**
* Returns an option which sets and validates project ID resource name for log entries.
*
* @param id corresponds to PROJECT_ID token in 'logName' field described in
* https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry
*/
public static LogDestinationName project(String id) {
return new LogDestinationName(DestinationType.PROJECT, id);
}

/**
* Returns an option which sets and validates project ID resource name for log entries.
*
* @param id corresponds to FOLDER_ID token in 'logName' field described in
* https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry
*/
public static LogDestinationName folder(String id) {
return new LogDestinationName(DestinationType.FOLDER, id);
}

/**
* Returns an option which sets and validates project ID resource name for log entries.
*
* @param id corresponds to ORGANIZATION_ID token in 'logName' field described in
* https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry
*/
public static LogDestinationName organization(String id) {
return new LogDestinationName(DestinationType.ORGANIZATION, id);
}

/**
* Returns an option which sets and validates project ID resource name for log entries.
*
* @param id corresponds to BILLING_ACCOUNT_ID token in 'logName' field described in
* https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry
*/
public static LogDestinationName billingAccount(String id) {
return new LogDestinationName(DestinationType.BILLINGACCOUNT, id);
}

/** Creates a {@code LogEntry} object for given log ID. */
public LogName toLogName(String logId) {
if (logId == null) {
return null;
}

switch ((DestinationType) getOptionType()) {
case PROJECT:
return LogName.ofProjectLogName(getValue().toString(), logId);
minherz marked this conversation as resolved.
Show resolved Hide resolved

case FOLDER:
return LogName.ofFolderLogName(getValue().toString(), logId);

case ORGANIZATION:
return LogName.ofOrganizationLogName(getValue().toString(), logId);

case BILLINGACCOUNT:
return LogName.ofBillingAccountLogName(getValue().toString(), logId);
}

return null;
}

/** Creates a {@code LogDestinationName} object from given {@code LogName}. */
public static LogDestinationName fromLogName(LogName logName) {
if (logName == null) {
return null;
}

if (logName.getProject() != null) {
return project(logName.getProject());
} else if (logName.getBillingAccount() != null) {
return billingAccount(logName.getBillingAccount());
} else if (logName.getFolder() != null) {
return folder(logName.getFolder());
} else if (logName.getOrganization() != null) {
return organization(logName.getOrganization());
}

return null;
}
}
Expand Up @@ -66,6 +66,7 @@ public LogEntry apply(com.google.logging.v2.LogEntry pb) {
private final boolean traceSampled;
private final SourceLocation sourceLocation;
private final Payload<?> payload;
private final LogDestinationName destination;

/** A builder for {@code LogEntry} objects. */
public static class Builder {
Expand All @@ -84,6 +85,7 @@ public static class Builder {
private boolean traceSampled;
private SourceLocation sourceLocation;
private Payload<?> payload;
private LogDestinationName destination;

Builder(Payload<?> payload) {
this.payload = payload;
Expand All @@ -104,6 +106,7 @@ public static class Builder {
this.traceSampled = entry.traceSampled;
this.sourceLocation = entry.sourceLocation;
this.payload = entry.payload;
this.destination = entry.destination;
}

/**
Expand Down Expand Up @@ -282,6 +285,12 @@ public Builder setPayload(Payload<?> payload) {
return this;
}

/** Sets the log path destination name type associated with the log entry. */
public Builder setDestination(LogDestinationName destination) {
this.destination = destination;
return this;
}

/** Creates a {@code LogEntry} object for this builder. */
public LogEntry build() {
return new LogEntry(this);
Expand All @@ -303,6 +312,7 @@ public LogEntry build() {
this.traceSampled = builder.traceSampled;
this.sourceLocation = builder.sourceLocation;
this.payload = builder.payload;
this.destination = builder.destination;
}

/**
Expand Down Expand Up @@ -438,6 +448,16 @@ public <T extends Payload<?>> T getPayload() {
return (T) payload;
}

/**
* Returns the log path destination name type associated with log entry. By default, project name
* based destination is used.
*
* @see <a href="https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry">logName</a>
*/
public LogDestinationName getDestination() {
return destination;
}

@Override
public int hashCode() {
return Objects.hash(
Expand All @@ -454,7 +474,8 @@ public int hashCode() {
getSpanId(),
traceSampled,
sourceLocation,
payload);
payload,
destination);
}

@Override
Expand All @@ -479,7 +500,8 @@ public boolean equals(Object obj) {
&& Objects.equals(getSpanId(), other.getSpanId())
&& Objects.equals(traceSampled, other.traceSampled)
&& Objects.equals(sourceLocation, other.sourceLocation)
&& Objects.equals(payload, other.payload);
&& Objects.equals(payload, other.payload)
&& Objects.equals(destination, other.destination);
}

@Override
Expand All @@ -499,6 +521,7 @@ public String toString() {
.add("traceSampled", traceSampled)
.add("sourceLocation", sourceLocation)
.add("payload", payload)
.add("destination", destination)
.toString();
}

Expand All @@ -510,8 +533,13 @@ public Builder toBuilder() {
com.google.logging.v2.LogEntry toPb(String projectId) {
com.google.logging.v2.LogEntry.Builder builder = payload.toPb();
builder.putAllLabels(labels);

if (logName != null) {
builder.setLogName(LogName.ofProjectLogName(projectId, logName).toString());
if (destination == null) {
builder.setLogName(LogName.ofProjectLogName(projectId, logName).toString());
} else {
builder.setLogName(destination.toLogName(logName).toString());
}
}
if (resource != null) {
builder.setResource(resource.toPb());
Expand Down Expand Up @@ -570,7 +598,12 @@ static LogEntry fromPb(com.google.logging.v2.LogEntry entryPb) {
builder.setLabels(entryPb.getLabelsMap());
builder.setSeverity(Severity.fromPb(entryPb.getSeverity()));
if (!entryPb.getLogName().equals("")) {
builder.setLogName(LogName.parse(entryPb.getLogName()).getLog());
LogName name = LogName.parse(entryPb.getLogName());
builder.setLogName(name.getLog());
LogDestinationName resource = LogDestinationName.fromLogName(name);
if (resource != null) {
losalex marked this conversation as resolved.
Show resolved Hide resolved
builder.setDestination(LogDestinationName.fromLogName(name));
}
}
if (!entryPb.getResource().equals(com.google.api.MonitoredResource.getDefaultInstance())) {
builder.setResource(MonitoredResource.fromPb(entryPb.getResource()));
Expand Down