Skip to content

Commit

Permalink
Merge pull request #1937 from Bit-Quill/cole/user-agent-manifest-fix
Browse files Browse the repository at this point in the history
Improved user agent handling of cases where gremlin version is not available
  • Loading branch information
vkagamlyk authored Jan 14, 2023
2 parents 3992be1 + bcee628 commit 9fde73d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ image::https://raw.githubusercontent.com/apache/tinkerpop/master/docs/static/ima
* Prevented fast `NoHostAvailableException` in favor of more direct exceptions when borrowing connections from the `ConnectionPool`.
* Fixed an issue in Go and Python GLVs where modifying per request settings to override request_id's was not working correctly.
* Fixed incorrect implementation for `GraphTraversalSource.With` in `gremlin-go`.
* Changed Gremlin.version() to return `"VersionNotFound"` if the version is missing from the manifest.
* Fixed a case sensitivity issue when comparing request UUIDs in `gremlin-javascript`.
==== Bugs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,21 @@ public final class Gremlin {
private static String version;

static {
version = Manifests.read("version");
try {
version = Manifests.read("version");
}
catch (Exception e) {
version = "VersionNotFound";
}
}

private Gremlin() {
}

/**
* Get the current version of tinkerpop. Will return "VersionNotFound" if there are any issues finding
* the version. This typically would be the result of the version being missing from the manifest file.
*/
public static String version() {
return version;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.apache.tinkerpop.gremlin.driver;

import org.apache.tinkerpop.gremlin.util.Gremlin;
import javax.naming.NamingException;

public class UserAgent {

Expand All @@ -35,21 +34,39 @@ public class UserAgent {
public static final String USER_AGENT;

static {
String applicationName = "";
try {
applicationName = ((String)(new javax.naming.InitialContext().lookup("java:app/AppName"))).replace(' ', '_');
} catch (NamingException e) {
applicationName = "NotAvailable";
};
final String applicationName = getAttributeOrDefault(()->{ return (String)(new javax.naming.InitialContext().lookup("java:app/AppName")); });

String glvVersion = getAttributeOrDefault(()->{ return Gremlin.version(); });
if(glvVersion.equals("VersionNotFound")) {
glvVersion = "NotAvailable";
}

final String javaVersion = getAttributeOrDefault(()->{ return System.getProperty("java.version", "NotAvailable"); });

final String osName = getAttributeOrDefault(()->{ return System.getProperty("os.name", "NotAvailable"); });

final String osVersion = getAttributeOrDefault(()->{ return System.getProperty("os.version", "NotAvailable"); });

final String glvVersion = Gremlin.version().replace(' ', '_');
final String javaVersion = System.getProperty("java.version", "NotAvailable").replace(' ', '_');
final String osName = System.getProperty("os.name", "NotAvailable").replace(' ', '_');
final String osVersion = System.getProperty("os.version", "NotAvailable").replace(' ', '_');
final String cpuArch = System.getProperty("os.arch", "NotAvailable").replace(' ', '_');
final String cpuArch = getAttributeOrDefault(()->{ return System.getProperty("os.arch", "NotAvailable"); });

USER_AGENT = String.format("%s Gremlin-Java.%s %s %s.%s %s",
applicationName, glvVersion, javaVersion,
osName, osVersion, cpuArch);
}

private static String getAttributeOrDefault(ThrowingSupplier<String> supplier){
String ret = "NotAvailable";
try {
ret = supplier.get().replace(' ', '_');
}
catch (Exception e) {
// No action taken, default value of "NotAvailable" will be used if supplier fails
}
return ret;
}

@FunctionalInterface
private interface ThrowingSupplier<T> {
T get() throws Exception;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

const crypto = require('crypto');
const os = require('os');
const gremlinVersion = require(__dirname + '/../package.json').version;
const { readFileSync } = require('fs');

exports.toLong = function toLong(value) {
return new Long(value);
Expand Down Expand Up @@ -84,7 +84,12 @@ exports.ImmutableMap = ImmutableMap;

function generateUserAgent() {
const applicationName = (process.env.npm_package_name || 'NotAvailable').replace('_', ' ');
const driverVersion = gremlinVersion.replace('_', ' ');
let driverVersion;
try {
driverVersion = JSON.parse(readFileSync(__dirname + '/../package.json')).version.replace('_', ' ');
} catch (e) {
driverVersion = 'NotAvailable';
}
const runtimeVersion = process.version.replace(' ', '_');
const osName = os.platform().replace(' ', '_');
const osVersion = os.release().replace(' ', '_');
Expand Down

0 comments on commit 9fde73d

Please sign in to comment.