report sdk version in user-agent (#810)

* report sdk version in user-agent

Signed-off-by: addjuarez <addiajuarez@gmail.com>

* make private

Signed-off-by: addjuarez <addiajuarez@gmail.com>

* close input

Signed-off-by: addjuarez <addiajuarez@gmail.com>

* style

Signed-off-by: addjuarez <addiajuarez@gmail.com>

* debug

Signed-off-by: addjuarez <addiajuarez@gmail.com>

* typo

Signed-off-by: addjuarez <addiajuarez@gmail.com>

* use try resource block

Signed-off-by: addjuarez <addiajuarez@gmail.com>

* fix exception

Signed-off-by: addjuarez <addiajuarez@gmail.com>

* newline

Signed-off-by: addjuarez <addiajuarez@gmail.com>

* use defaults

Signed-off-by: addjuarez <addiajuarez@gmail.com>

* Use getSdkVersion()

Signed-off-by: addjuarez <addiajuarez@gmail.com>

Signed-off-by: addjuarez <addiajuarez@gmail.com>
This commit is contained in:
addjuarez 2022-11-21 00:07:02 -06:00 committed by GitHub
parent 622844b30a
commit bc9de9fa7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 91 additions and 3 deletions

View File

@ -49,4 +49,5 @@ public final class ActorUtils {
ActorType actorTypeAnnotation = node.getAnnotation(ActorType.class);
return actorTypeAnnotation != null ? actorTypeAnnotation.name() : actorClass.getSimpleName();
}
}

View File

@ -16,6 +16,7 @@ package io.dapr.actors.client;
import io.dapr.client.DaprApiProtocol;
import io.dapr.client.DaprHttpBuilder;
import io.dapr.config.Properties;
import io.dapr.utils.Version;
import io.dapr.v1.DaprGrpc;
import io.grpc.Channel;
import io.grpc.ManagedChannel;
@ -102,7 +103,10 @@ public class ActorClient implements AutoCloseable {
throw new IllegalArgumentException("Invalid port.");
}
return ManagedChannelBuilder.forAddress(Properties.SIDECAR_IP.get(), port).usePlaintext().build();
return ManagedChannelBuilder.forAddress(Properties.SIDECAR_IP.get(), port)
.usePlaintext()
.userAgent(Version.getSdkVersion())
.build();
}
/**

View File

@ -20,6 +20,7 @@ import io.dapr.client.DaprHttpBuilder;
import io.dapr.config.Properties;
import io.dapr.serializer.DaprObjectSerializer;
import io.dapr.serializer.DefaultObjectSerializer;
import io.dapr.utils.Version;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import reactor.core.publisher.Mono;
@ -338,7 +339,10 @@ public class ActorRuntime implements Closeable {
throw new IllegalStateException("Invalid port.");
}
return ManagedChannelBuilder.forAddress(Properties.SIDECAR_IP.get(), port).usePlaintext().build();
return ManagedChannelBuilder.forAddress(Properties.SIDECAR_IP.get(), port)
.usePlaintext()
.userAgent(Version.getSdkVersion())
.build();
}
/**

View File

@ -25,6 +25,12 @@
</properties>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.0</version>
<type>maven-plugin</type>
</dependency>
<dependency>
<groupId>io.dapr</groupId>
<artifactId>dapr-sdk-autogen</artifactId>
@ -106,6 +112,22 @@
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/sdk_version.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>**/sdk_version.properties</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>

View File

@ -16,6 +16,7 @@ package io.dapr.client;
import io.dapr.config.Properties;
import io.dapr.serializer.DaprObjectSerializer;
import io.dapr.serializer.DefaultObjectSerializer;
import io.dapr.utils.Version;
import io.dapr.v1.DaprGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
@ -158,7 +159,7 @@ public class DaprClientBuilder {
throw new IllegalArgumentException("Invalid port.");
}
ManagedChannel channel = ManagedChannelBuilder.forAddress(
Properties.SIDECAR_IP.get(), port).usePlaintext().build();
Properties.SIDECAR_IP.get(), port).usePlaintext().userAgent(Version.getSdkVersion()).build();
Closeable closeableChannel = () -> {
if (channel != null && !channel.isShutdown()) {
channel.shutdown();
@ -176,4 +177,5 @@ public class DaprClientBuilder {
private DaprClient buildDaprClientHttp() {
return new DaprClientHttp(this.daprHttpBuilder.build(), this.objectSerializer, this.stateSerializer);
}
}

View File

@ -18,6 +18,7 @@ import io.dapr.client.domain.Metadata;
import io.dapr.config.Properties;
import io.dapr.exceptions.DaprError;
import io.dapr.exceptions.DaprException;
import io.dapr.utils.Version;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl;
@ -304,6 +305,8 @@ public class DaprHttp implements AutoCloseable {
if (daprApiToken != null) {
requestBuilder.addHeader(Headers.DAPR_API_TOKEN, daprApiToken);
}
requestBuilder.addHeader(Headers.DAPR_USER_AGENT, Version.getSdkVersion());
if (headers != null) {
Optional.ofNullable(headers.entrySet()).orElse(Collections.emptySet()).stream()

View File

@ -27,4 +27,9 @@ class Headers {
* Token for authentication from Application to Dapr runtime.
*/
static final String DAPR_API_TOKEN = "dapr-api-token";
/**
* Header for Api Logging User-Agent.
*/
static final String DAPR_USER_AGENT = "User-Agent";
}

View File

@ -0,0 +1,46 @@
/*
* Copyright 2022 The Dapr Authors
* 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 io.dapr.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public final class Version {
private static String sdkVersion = null;
/**
* Retrieves sdk version from resources.
*
* @return String version of sdk.
*/
public static String getSdkVersion() {
if (sdkVersion != null) {
return sdkVersion;
}
try (InputStream input = Version.class.getResourceAsStream("/sdk_version.properties");) {
Properties properties = new Properties();
properties.load(input);
sdkVersion = "dapr-sdk-java/v" + properties.getProperty("sdk_version", "unknown");
} catch (IOException e) {
sdkVersion = "unknown";
}
return sdkVersion;
}
}

View File

@ -0,0 +1 @@
sdk_version=${project.version}