Skip to content
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
1 change: 0 additions & 1 deletion core/src/main/java/tech/ydb/core/grpc/YdbHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ private YdbHeaders() { }
public static ClientInterceptor createMetadataInterceptor(GrpcTransportBuilder builder) {
Metadata extraHeaders = new Metadata();
extraHeaders.put(YdbHeaders.DATABASE, builder.getDatabase());
extraHeaders.put(YdbHeaders.BUILD_INFO, builder.getBuildInfo());
String appName = builder.getApplicationName();
if (appName != null) {
extraHeaders.put(YdbHeaders.APPLICATION_NAME, appName);
Expand Down
7 changes: 5 additions & 2 deletions core/src/main/java/tech/ydb/core/impl/BaseGrpcTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ public abstract class BaseGrpcTransport implements GrpcTransport {
private final AtomicBoolean isClosed = new AtomicBoolean(false);

protected final EndpointRecord serverEndpoint;
private final String buildInfo;

protected BaseGrpcTransport(GrpcTransportBuilder builder) {
this.serverEndpoint = getDiscoveryEndpoint(builder);
this(getDiscoveryEndpoint(builder), builder.getBuildInfo());
}

protected BaseGrpcTransport(EndpointRecord serverEndpoint) {
protected BaseGrpcTransport(EndpointRecord serverEndpoint, String buildInfo) {
this.serverEndpoint = serverEndpoint;
this.buildInfo = buildInfo;
}

protected abstract AuthCallOptions getAuthCallOptions();
Expand Down Expand Up @@ -244,6 +246,7 @@ private static Status deadlineExpiredStatus(MethodDescriptor<?, ?> method, GrpcR

private Metadata makeMetadataFromSettings(GrpcRequestSettings settings, EndpointRecord endpoint) {
Metadata metadata = new Metadata();
metadata.put(YdbHeaders.BUILD_INFO, buildInfo);
String token = getAuthCallOptions().getToken();
if (token != null) {
metadata.put(YdbHeaders.AUTH_TICKET, token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import java.util.concurrent.ScheduledExecutorService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import tech.ydb.core.grpc.GrpcRequestSettings;
import tech.ydb.core.impl.auth.AuthCallOptions;
import tech.ydb.core.impl.pool.EndpointRecord;
Expand All @@ -16,8 +13,6 @@
* @author Aleksandr Gorshenin
*/
public class FixedCallOptionsTransport extends BaseGrpcTransport {
private static final Logger logger = LoggerFactory.getLogger(FixedCallOptionsTransport.class);

private final ScheduledExecutorService scheduler;
private final AuthCallOptions callOptions;
private final String database;
Expand All @@ -27,9 +22,10 @@ public FixedCallOptionsTransport(
ScheduledExecutorService scheduler,
AuthCallOptions callOptions,
String database,
String buildInfo,
EndpointRecord endpoint,
ManagedChannelFactory channelFactory) {
super(endpoint);
super(endpoint, buildInfo);
this.scheduler = scheduler;
this.callOptions = callOptions;
this.database = database;
Expand Down
44 changes: 44 additions & 0 deletions core/src/main/java/tech/ydb/core/impl/Observability.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package tech.ydb.core.impl;

import com.google.common.annotations.VisibleForTesting;

import tech.ydb.core.metrics.Meter;
import tech.ydb.core.tracing.NoopTracer;
import tech.ydb.core.tracing.Tracer;

/**
*
* @author Aleksandr Gorshenin {@literal <alexandr268@ydb.tech>}
*/
public final class Observability {
public static final String TRACING_CHAIN = ";ydb-sdk-tracing/0.1.0";
public static final String METRICS_CHAIN = ";ydb-sdk-metrics/0.1.0";

private static volatile boolean isTracingEnabled = false;
private static volatile boolean isMetricsEnabled = false;

private Observability() {
}

public static void reportTracingUsage(Tracer tracer) {
if (tracer != NoopTracer.getInstance()) {
isTracingEnabled = true;
}
}

public static void reportMetricsUsage(Meter meter) {
if (meter != Meter.NOOP) {
isMetricsEnabled = true;
}
}

static String getDiscoveryBuildInfo(String base) {
return base + (isTracingEnabled ? TRACING_CHAIN : "") + (isMetricsEnabled ? METRICS_CHAIN : "");
}

@VisibleForTesting
static void reset() {
isTracingEnabled = false;
isMetricsEnabled = false;
}
}
15 changes: 13 additions & 2 deletions core/src/main/java/tech/ydb/core/impl/YdbTransportImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public YdbTransportImpl(GrpcTransportBuilder builder) {

this.database = Strings.nullToEmpty(builder.getDatabase());
this.tracer = builder.getTracer();
Observability.reportTracingUsage(this.tracer);

logger.info("Create YDB transport with endpoint {} and {}", serverEndpoint, balancingSettings);

Expand All @@ -54,7 +55,8 @@ public YdbTransportImpl(GrpcTransportBuilder builder) {
channelFactory, builder);
this.channelPool = new GrpcChannelPool(channelFactory, scheduler);
this.endpointPool = new EndpointPool(balancingSettings);
this.discovery = new YdbDiscovery(new DiscoveryHandler(), scheduler, database, discoveryTimeout);
DiscoveryHandler handler = new DiscoveryHandler(builder.getBuildInfo());
this.discovery = new YdbDiscovery(handler, scheduler, database, discoveryTimeout);
}

public void start(GrpcTransportBuilder.InitMode mode) {
Expand Down Expand Up @@ -149,6 +151,12 @@ protected void pessimizeEndpoint(EndpointRecord endpoint, String reason) {
}

private class DiscoveryHandler implements YdbDiscovery.Handler {
private final String baseBuildInfo;

DiscoveryHandler(String buildInfo) {
this.baseBuildInfo = buildInfo;
}

@Override
public Instant instant() {
return Instant.now();
Expand All @@ -167,7 +175,10 @@ public CompletableFuture<Boolean> handleEndpoints(List<EndpointRecord> endpoints

@Override
public GrpcTransport createDiscoveryTransport() {
return new FixedCallOptionsTransport(scheduler, callOptions, database, serverEndpoint, channelFactory);
String buildInfo = Observability.getDiscoveryBuildInfo(baseBuildInfo);
return new FixedCallOptionsTransport(
scheduler, callOptions, database, buildInfo, serverEndpoint, channelFactory
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public AuthCallOptions(

AuthRpcProvider<? super GrpcAuthRpc> authProvider = builder.getAuthProvider();
if (authProvider != null) {
GrpcAuthRpc rpc = new GrpcAuthRpc(endpoints, scheduler, builder.getDatabase(), channelFactory);
String database = builder.getDatabase();
String buildInfo = builder.getBuildInfo();
GrpcAuthRpc rpc = new GrpcAuthRpc(endpoints, scheduler, database, buildInfo, channelFactory);
authIdentity = builder.getAuthProvider().createAuthIdentity(rpc);
} else {
authIdentity = null;
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/tech/ydb/core/impl/auth/GrpcAuthRpc.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,23 @@ public class GrpcAuthRpc {
private final List<EndpointRecord> endpoints;
private final ScheduledExecutorService scheduler;
private final String database;
private final String buildInfo;
private final ManagedChannelFactory channelFactory;
private final AtomicInteger endpointIdx = new AtomicInteger();

public GrpcAuthRpc(
List<EndpointRecord> endpoints,
ScheduledExecutorService scheduler,
String database,
String buildInfo,
ManagedChannelFactory channelFactory) {
if (endpoints == null || endpoints.isEmpty()) {
throw new IllegalStateException("Empty endpoints list for auth rpc");
}
this.endpoints = endpoints;
this.scheduler = scheduler;
this.database = database;
this.buildInfo = buildInfo;
this.channelFactory = channelFactory;
}

Expand Down Expand Up @@ -60,6 +63,7 @@ public GrpcTransport createTransport() {
scheduler,
new AuthCallOptions(),
database,
buildInfo,
endpoints.get(endpointIdx.get()),
channelFactory
);
Expand Down
6 changes: 6 additions & 0 deletions core/src/test/java/tech/ydb/core/impl/MockedCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@

public abstract class MockedCall<ResT, RespT> extends ClientCall<ResT, RespT> {
private final Executor executor;
private volatile Metadata lastMetadata = null;

protected MockedCall(Executor executor) {
this.executor = executor;
}

protected abstract void complete(Listener<RespT> listener);

public Metadata getLastCallMetadata() {
return lastMetadata;
}

@Override
public void start(Listener<RespT> listener, Metadata headers) {
lastMetadata = headers;
executor.execute(() -> complete(listener));
}

Expand Down
49 changes: 49 additions & 0 deletions core/src/test/java/tech/ydb/core/impl/ObservabilityTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package tech.ydb.core.impl;

import org.junit.After;
import org.junit.Assert;
import org.junit.Test;

import tech.ydb.core.metrics.Meter;
import tech.ydb.core.tracing.NoopTracer;
import tech.ydb.core.tracing.Span;
import tech.ydb.core.tracing.SpanKind;

/**
*
* @author Aleksandr Gorshenin {@literal <alexandr268@ydb.tech>}
*/
public class ObservabilityTest {
private static final String BASE = "ydb-java-sdk/1.2.3";

@After
public void reset() {
Observability.reset();
}

@Test
public void baseTest() {
Assert.assertEquals(BASE, Observability.getDiscoveryBuildInfo(BASE));

// noop implementations ignored
Observability.reportMetricsUsage(Meter.NOOP);
Observability.reportTracingUsage(NoopTracer.getInstance());
Assert.assertEquals(BASE, Observability.getDiscoveryBuildInfo(BASE));

Observability.reportMetricsUsage(new Meter() { });
Assert.assertEquals(BASE + ";ydb-sdk-metrics/0.1.0", Observability.getDiscoveryBuildInfo(BASE));

Observability.reportTracingUsage((String spanName, SpanKind spanKind) -> Span.NOOP);
Assert.assertEquals(
BASE + ";ydb-sdk-tracing/0.1.0;ydb-sdk-metrics/0.1.0",
Observability.getDiscoveryBuildInfo(BASE)
);

Observability.reportMetricsUsage(Meter.NOOP);
Observability.reportTracingUsage(NoopTracer.getInstance());
Assert.assertEquals(
BASE + ";ydb-sdk-tracing/0.1.0;ydb-sdk-metrics/0.1.0",
Observability.getDiscoveryBuildInfo(BASE)
);
}
}
Comment thread
alex268 marked this conversation as resolved.
3 changes: 2 additions & 1 deletion core/src/test/java/tech/ydb/core/impl/YdbDiscoveryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ public Instant instant() {
@Override
public GrpcTransport createDiscoveryTransport() {
EndpointRecord discovery = new EndpointRecord("unknown", 1234);
return new FixedCallOptionsTransport(scheduler, new AuthCallOptions(), "/test", discovery, channelFactory);
AuthCallOptions options = new AuthCallOptions();
return new FixedCallOptionsTransport(scheduler, options, "/test", "test_sdk", discovery, channelFactory);
}

@Override
Expand Down
Loading
Loading