Skip to content

chore: Composite Tracer #3163

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

Merged
merged 3 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@ If you are using Maven without the BOM, add this to your dependencies:
If you are using Gradle 5.x or later, add this to your dependencies:

```Groovy
implementation platform('com.google.cloud:libraries-bom:26.40.0')
implementation platform('com.google.cloud:libraries-bom:26.41.0')

implementation 'com.google.cloud:google-cloud-spanner'
```
If you are using Gradle without BOM, add this to your dependencies:

```Groovy
implementation 'com.google.cloud:google-cloud-spanner:6.68.1'
implementation 'com.google.cloud:google-cloud-spanner:6.69.0'
```

If you are using SBT, add this to your dependencies:

```Scala
libraryDependencies += "com.google.cloud" % "google-cloud-spanner" % "6.68.1"
libraryDependencies += "com.google.cloud" % "google-cloud-spanner" % "6.69.0"
```
<!-- {x-version-update-end} -->

Expand Down Expand Up @@ -687,7 +687,7 @@ Java is a registered trademark of Oracle and/or its affiliates.
[kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/java-spanner/java11.html
[stability-image]: https://img.shields.io/badge/stability-stable-green
[maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-spanner.svg
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-spanner/6.68.1
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-spanner/6.69.0
[authentication]: https://github.com/googleapis/google-cloud-java#authentication
[auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes
[predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* 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.spanner;

import com.google.api.core.InternalApi;
import com.google.api.gax.tracing.ApiTracer;
import com.google.api.gax.tracing.BaseApiTracer;
import com.google.api.gax.tracing.MetricsTracer;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.List;
import org.threeten.bp.Duration;

@InternalApi
public class CompositeTracer extends BaseApiTracer {
private final List<ApiTracer> children;

public CompositeTracer(List<ApiTracer> children) {
this.children = ImmutableList.copyOf(children);
}

@Override
public Scope inScope() {
final List<Scope> childScopes = new ArrayList<>(children.size());

for (ApiTracer child : children) {
childScopes.add(child.inScope());
}

return new Scope() {
@Override
public void close() {
for (Scope childScope : childScopes) {
childScope.close();
}
}
};
}

@Override
public void operationSucceeded() {
for (ApiTracer child : children) {
child.operationSucceeded();
}
}

@Override
public void operationCancelled() {
for (ApiTracer child : children) {
child.operationCancelled();
}
}

@Override
public void operationFailed(Throwable error) {
for (ApiTracer child : children) {
child.operationFailed(error);
}
}

@Override
public void connectionSelected(String id) {
for (ApiTracer child : children) {
child.connectionSelected(id);
}
}

@Override
public void attemptStarted(int attemptNumber) {
for (ApiTracer child : children) {
child.attemptStarted(null, attemptNumber);
}
}

@Override
public void attemptStarted(Object request, int attemptNumber) {
for (ApiTracer child : children) {
child.attemptStarted(request, attemptNumber);
}
}

@Override
public void attemptSucceeded() {
for (ApiTracer child : children) {
child.attemptSucceeded();
}
}

@Override
public void attemptCancelled() {
for (ApiTracer child : children) {
child.attemptCancelled();
}
}

@Override
public void attemptFailed(Throwable error, Duration delay) {
for (ApiTracer child : children) {
child.attemptFailed(error, delay);
}
}

@Override
public void attemptFailedRetriesExhausted(Throwable error) {
for (ApiTracer child : children) {
child.attemptFailedRetriesExhausted(error);
}
}

@Override
public void attemptPermanentFailure(Throwable error) {
for (ApiTracer child : children) {
child.attemptPermanentFailure(error);
}
}

@Override
public void lroStartFailed(Throwable error) {
for (ApiTracer child : children) {
child.lroStartFailed(error);
}
}

@Override
public void lroStartSucceeded() {
for (ApiTracer child : children) {
child.lroStartSucceeded();
}
}

@Override
public void responseReceived() {
for (ApiTracer child : children) {
child.responseReceived();
}
}

@Override
public void requestSent() {
for (ApiTracer child : children) {
child.requestSent();
}
}

@Override
public void batchRequestSent(long elementCount, long requestSize) {
for (ApiTracer child : children) {
child.batchRequestSent(elementCount, requestSize);
}
}

public void addAttributes(String key, String value) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where/when is this method called? It does not override any methods (at least it is not marked as such), and I can't find anything in this PR that seems to call it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be required for MetricsTracer

Added in advance

for (ApiTracer child : children) {
if (child instanceof MetricsTracer) {
MetricsTracer metricsTracer = (MetricsTracer) child;
metricsTracer.addAttributes(key, value);
}
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.spanner;

import com.google.api.core.InternalApi;
import com.google.api.gax.tracing.ApiTracer;
import com.google.api.gax.tracing.ApiTracerFactory;
import com.google.api.gax.tracing.BaseApiTracerFactory;
import com.google.api.gax.tracing.SpanName;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.List;

@InternalApi
public class CompositeTracerFactory extends BaseApiTracerFactory {

private final List<ApiTracerFactory> apiTracerFactories;

public CompositeTracerFactory(List<ApiTracerFactory> apiTracerFactories) {
this.apiTracerFactories = ImmutableList.copyOf(apiTracerFactories);
}

@Override
public ApiTracer newTracer(ApiTracer parent, SpanName spanName, OperationType operationType) {
List<ApiTracer> children = new ArrayList<>(apiTracerFactories.size());

for (ApiTracerFactory factory : apiTracerFactories) {
children.add(factory.newTracer(parent, spanName, operationType));
}
return new CompositeTracer(children);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
Expand Down Expand Up @@ -1621,8 +1623,12 @@ public OpenTelemetry getOpenTelemetry() {

@Override
public ApiTracerFactory getApiTracerFactory() {
List<ApiTracerFactory> apiTracerFactories = new ArrayList();
// Prefer any direct ApiTracerFactory that might have been set on the builder.
return MoreObjects.firstNonNull(super.getApiTracerFactory(), getDefaultApiTracerFactory());
apiTracerFactories.add(
MoreObjects.firstNonNull(super.getApiTracerFactory(), getDefaultApiTracerFactory()));

return new CompositeTracerFactory(apiTracerFactories);
}

private ApiTracerFactory getDefaultApiTracerFactory() {
Expand Down
Loading
Loading