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

report sdk version in user-agent #810

Merged
merged 11 commits into from
Nov 21, 2022
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: 1 addition & 0 deletions sdk-actors/src/main/java/io/dapr/actors/ActorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ public static String findActorTypeName(Class<?> actorClass) {
ActorType actorTypeAnnotation = node.getAnnotation(ActorType.class);
return actorTypeAnnotation != null ? actorTypeAnnotation.name() : actorClass.getSimpleName();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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;
Expand Down Expand Up @@ -102,7 +103,10 @@ private static ManagedChannel buildManagedChannel(DaprApiProtocol apiProtocol) {
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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
Expand Down Expand Up @@ -338,7 +339,10 @@ private static ManagedChannel buildManagedChannel() {
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();
}

/**
Expand Down
22 changes: 22 additions & 0 deletions sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
4 changes: 3 additions & 1 deletion sdk/src/main/java/io/dapr/client/DaprClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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;
Expand Down Expand Up @@ -158,7 +159,7 @@ private DaprClient buildDaprClientGrpc() {
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();
Expand All @@ -176,4 +177,5 @@ private DaprClient buildDaprClientGrpc() {
private DaprClient buildDaprClientHttp() {
return new DaprClientHttp(this.daprHttpBuilder.build(), this.objectSerializer, this.stateSerializer);
}

}
3 changes: 3 additions & 0 deletions sdk/src/main/java/io/dapr/client/DaprHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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;
Expand Down Expand Up @@ -304,6 +305,8 @@ private CompletableFuture<Response> doInvokeApi(String method,
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()
Expand Down
5 changes: 5 additions & 0 deletions sdk/src/main/java/io/dapr/client/Headers.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
46 changes: 46 additions & 0 deletions sdk/src/main/java/io/dapr/utils/Version.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
1 change: 1 addition & 0 deletions sdk/src/main/resources/sdk_version.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sdk_version=${project.version}