-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adding TraceUtil interface and its implementation to enable Tra…
…cing controls via DatastoreOptions (#1431) * Adding EnabledTraceUtil, DisabledTraceUtil and TraceUtilTest * Annotating DatastoreOpenTelemetryOptions to be transient as they're not serializable * Adding google-auth-library-credentials dependency due to https://github.com/googleapis/java-datastore/actions/runs/8944472794/job/24571458116?pr=1431
- Loading branch information
1 parent
5a5dfdf
commit 98cc64e
Showing
10 changed files
with
938 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...oud-datastore/src/main/java/com/google/cloud/datastore/DatastoreOpenTelemetryOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright 2024 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.datastore; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
public class DatastoreOpenTelemetryOptions { | ||
private final boolean enabled; | ||
private final @Nullable OpenTelemetry openTelemetry; | ||
|
||
DatastoreOpenTelemetryOptions(Builder builder) { | ||
this.enabled = builder.enabled; | ||
this.openTelemetry = builder.openTelemetry; | ||
} | ||
|
||
public boolean isEnabled() { | ||
return enabled; | ||
} | ||
|
||
@Nullable | ||
public OpenTelemetry getOpenTelemetry() { | ||
return openTelemetry; | ||
} | ||
|
||
@Nonnull | ||
public DatastoreOpenTelemetryOptions.Builder toBuilder() { | ||
return new DatastoreOpenTelemetryOptions.Builder(this); | ||
} | ||
|
||
@Nonnull | ||
public static DatastoreOpenTelemetryOptions.Builder newBuilder() { | ||
return new DatastoreOpenTelemetryOptions.Builder(); | ||
} | ||
|
||
public static class Builder { | ||
|
||
private boolean enabled; | ||
|
||
@Nullable private OpenTelemetry openTelemetry; | ||
|
||
private Builder() { | ||
enabled = false; | ||
openTelemetry = null; | ||
} | ||
|
||
private Builder(DatastoreOpenTelemetryOptions options) { | ||
this.enabled = options.enabled; | ||
this.openTelemetry = options.openTelemetry; | ||
} | ||
|
||
@Nonnull | ||
public DatastoreOpenTelemetryOptions build() { | ||
return new DatastoreOpenTelemetryOptions(this); | ||
} | ||
|
||
/** | ||
* Sets whether tracing should be enabled. | ||
* | ||
* @param enabled Whether tracing should be enabled. | ||
*/ | ||
@Nonnull | ||
public DatastoreOpenTelemetryOptions.Builder setTracingEnabled(boolean enabled) { | ||
this.enabled = enabled; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the {@link OpenTelemetry} to use with this Datastore instance. If telemetry collection | ||
* is enabled, but an `OpenTelemetry` is not provided, the Datastore SDK will attempt to use the | ||
* `GlobalOpenTelemetry`. | ||
* | ||
* @param openTelemetry The OpenTelemetry that should be used by this Datastore instance. | ||
*/ | ||
@Nonnull | ||
public DatastoreOpenTelemetryOptions.Builder setOpenTelemetry( | ||
@Nonnull OpenTelemetry openTelemetry) { | ||
this.openTelemetry = openTelemetry; | ||
return this; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
...cloud-datastore/src/main/java/com/google/cloud/datastore/telemetry/DisabledTraceUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright 2024 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.datastore.telemetry; | ||
|
||
import com.google.api.core.ApiFunction; | ||
import com.google.api.core.ApiFuture; | ||
import com.google.api.core.InternalApi; | ||
import io.grpc.ManagedChannelBuilder; | ||
import java.util.Map; | ||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Tracing utility implementation, used to stub out tracing instrumentation when tracing is | ||
* disabled. | ||
*/ | ||
@InternalApi | ||
public class DisabledTraceUtil implements TraceUtil { | ||
|
||
static class Span implements TraceUtil.Span { | ||
@Override | ||
public void end() {} | ||
|
||
@Override | ||
public void end(Throwable error) {} | ||
|
||
@Override | ||
public <T> void endAtFuture(ApiFuture<T> futureValue) {} | ||
|
||
@Override | ||
public TraceUtil.Span addEvent(String name) { | ||
return this; | ||
} | ||
|
||
@Override | ||
public TraceUtil.Span addEvent(String name, Map<String, Object> attributes) { | ||
return this; | ||
} | ||
|
||
@Override | ||
public TraceUtil.Span setAttribute(String key, int value) { | ||
return this; | ||
} | ||
|
||
@Override | ||
public TraceUtil.Span setAttribute(String key, String value) { | ||
return this; | ||
} | ||
|
||
@Override | ||
public Scope makeCurrent() { | ||
return new Scope(); | ||
} | ||
} | ||
|
||
static class Context implements TraceUtil.Context { | ||
@Override | ||
public Scope makeCurrent() { | ||
return new Scope(); | ||
} | ||
} | ||
|
||
static class Scope implements TraceUtil.Scope { | ||
@Override | ||
public void close() {} | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder> getChannelConfigurator() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Span startSpan(String spanName) { | ||
return new Span(); | ||
} | ||
|
||
@Override | ||
public TraceUtil.Span startSpan(String spanName, TraceUtil.Context parent) { | ||
return new Span(); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public TraceUtil.Span currentSpan() { | ||
return new Span(); | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public TraceUtil.Context currentContext() { | ||
return new Context(); | ||
} | ||
} |
Oops, something went wrong.