Skip to content

Commit 901868e

Browse files
authored
Merge pull request #99 from faststats-dev/feat/name-version-ua
Add SDK name and version to User-Agent header
2 parents 82b6d1c + 86f0630 commit 901868e

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

build.gradle.kts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ subprojects {
4646
withJavadocJar()
4747
}
4848

49+
val generateFastStatsProperties by tasks.registering {
50+
val outputDir = layout.buildDirectory.dir("generated/resources/faststats")
51+
outputs.dir(outputDir)
52+
doLast {
53+
val file = outputDir.get().file("META-INF/faststats.properties").asFile
54+
file.parentFile.mkdirs()
55+
file.writeText("name=${project.name}\nversion=${project.version}\n")
56+
}
57+
}
58+
59+
sourceSets.main { resources.srcDir(generateFastStatsProperties) }
60+
4961
tasks.compileJava {
5062
options.release.set(javaVersion)
5163
}

core/src/main/java/dev/faststats/core/SimpleMetrics.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ public abstract class SimpleMetrics implements Metrics {
4949
private final URI url;
5050
private final boolean debug;
5151

52+
private final String SDK_NAME;
53+
private final String SDK_VERSION;
54+
55+
{
56+
final var properties = new Properties();
57+
try (final var stream = getClass().getResourceAsStream("/META-INF/faststats.properties")) {
58+
if (stream != null) properties.load(stream);
59+
} catch (final IOException ignored) {
60+
}
61+
this.SDK_NAME = properties.getProperty("name", "unknown");
62+
this.SDK_VERSION = properties.getProperty("version", "unknown");
63+
System.out.println(SDK_NAME + "/" + SDK_VERSION);
64+
}
65+
5266
@Contract(mutates = "io")
5367
@SuppressWarnings("PatternValidation")
5468
protected SimpleMetrics(final Factory<?, ?> factory, final Config config) throws IllegalStateException {
@@ -191,7 +205,7 @@ private boolean submitNow() throws IOException {
191205
.header("Content-Encoding", "gzip")
192206
.header("Content-Type", "application/octet-stream")
193207
.header("Authorization", "Bearer " + getToken())
194-
.header("User-Agent", "FastStats Metrics")
208+
.header("User-Agent", "FastStats Metrics " + getSdkName() + "/" + getSdkVersion())
195209
.timeout(Duration.ofSeconds(3))
196210
.uri(url)
197211
.build();
@@ -227,6 +241,14 @@ private boolean submitNow() throws IOException {
227241
}
228242
}
229243

244+
private String getSdkName() {
245+
return SDK_NAME;
246+
}
247+
248+
private String getSdkVersion() {
249+
return SDK_VERSION;
250+
}
251+
230252
private final String javaVersion = System.getProperty("java.version");
231253
private final String osArch = System.getProperty("os.arch");
232254
private final String osName = System.getProperty("os.name");

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=0.16.0
1+
version=0.17.0

0 commit comments

Comments
 (0)