Permalink
Browse files

JCBC-1205: Adopt new tracing formatting.

This changeset adapts the timeout exception formatting to align
with the latest SDK-RFC changes (towards a terse json format).

Change-Id: I7f41835164f1ed56e26f88d0b09225473f311704
Reviewed-on: http://review.couchbase.org/94769
Reviewed-by: Michael Nitschinger <michael@nitschinger.at>
Tested-by: Michael Nitschinger <michael@nitschinger.at>
  • Loading branch information...
daschl committed May 25, 2018
1 parent ccdd03c commit a657e5240cfd54447c712e1a118c2ad7157aa03b
@@ -23,17 +23,22 @@
import com.couchbase.client.core.message.CouchbaseRequest;
import com.couchbase.client.core.message.CouchbaseResponse;
import com.couchbase.client.core.message.analytics.AnalyticsRequest;
+import com.couchbase.client.core.message.config.ConfigRequest;
import com.couchbase.client.core.message.kv.BinaryRequest;
import com.couchbase.client.core.message.query.QueryRequest;
import com.couchbase.client.core.message.search.SearchRequest;
import com.couchbase.client.core.message.view.ViewRequest;
import com.couchbase.client.core.tracing.ThresholdLogReporter;
+import com.couchbase.client.core.utils.DefaultObjectMapper;
+import com.couchbase.client.deps.com.fasterxml.jackson.core.JsonProcessingException;
import com.couchbase.client.java.env.CouchbaseEnvironment;
import io.opentracing.Scope;
import io.opentracing.Span;
import rx.Observable;
import rx.functions.Func1;
+import java.util.HashMap;
+import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
@@ -58,32 +63,59 @@
return ex;
}
- public static String formatTimeout(CouchbaseRequest request, long timeout) {
+ /**
+ * This method take the given request and produces the correct additional timeout
+ * information according to the RFC.
+ */
+ static String formatTimeout(final CouchbaseRequest request, final long timeout) {
+ Map<String, Object> fieldMap = new HashMap<String, Object>();
+ fieldMap.put("t", timeout);
+
if (request != null) {
- return "localId: " + request.lastLocalId() + ", opId: " + request.operationId() + ", local: "
- + request.lastLocalSocket()
- + ", remote: " + request.lastRemoteSocket() + ", timeout: " + timeout + "us";
- } else {
- return "localId: unknown, opId: unknown, local: unknown, remote: unknown, timeout: " + timeout + "us";
+ fieldMap.put("s", formatServiceType(request));
+ putIfNotNull(fieldMap, "i", request.operationId());
+ putIfNotNull(fieldMap, "b", request.bucket());
+ putIfNotNull(fieldMap, "c", request.lastLocalId());
+ putIfNotNull(fieldMap, "l", request.lastLocalSocket());
+ putIfNotNull(fieldMap, "r", request.lastRemoteSocket());
+ }
+
+ try {
+ return DefaultObjectMapper.writeValueAsString(fieldMap);
+ } catch (JsonProcessingException e) {
+ LOGGER.warn("Could not format timeout information for request " + request, e);
+ return null;
}
}
/**
- * Maps the given request to the proper service identifier.
+ * Helper method to avoid ugly if/else blocks in {@link #formatTimeout(CouchbaseRequest, long)}.
*/
- private static String inferOperationPrefix(final CouchbaseRequest request) {
+ private static void putIfNotNull(final Map<String, Object> map, final String key, final Object value) {
+ if (value != null) {
+ map.put(key, value);
+ }
+ }
+
+ /**
+ * Helper method to turn the request into the proper string service type.
+ */
+ private static String formatServiceType(final CouchbaseRequest request) {
if (request instanceof BinaryRequest) {
- return ThresholdLogReporter.SERVICE_KV + ":";
+ return ThresholdLogReporter.SERVICE_KV;
} else if (request instanceof QueryRequest) {
- return ThresholdLogReporter.SERVICE_N1QL + ":";
- } else if (request instanceof SearchRequest) {
- return ThresholdLogReporter.SERVICE_FTS + ":";
- } else if (request instanceof AnalyticsRequest) {
- return ThresholdLogReporter.SERVICE_ANALYTICS + ":";
+ return ThresholdLogReporter.SERVICE_N1QL;
} else if (request instanceof ViewRequest) {
- return ThresholdLogReporter.SERVICE_VIEW + ":";
+ return ThresholdLogReporter.SERVICE_VIEW;
+ } else if (request instanceof AnalyticsRequest) {
+ return ThresholdLogReporter.SERVICE_ANALYTICS;
+ } else if (request instanceof SearchRequest) {
+ return ThresholdLogReporter.SERVICE_FTS;
+ } else if (request instanceof ConfigRequest) {
+ // Shouldn't be user visible, but just for completeness sake.
+ return "config";
} else {
- return "";
+ return "unknown";
}
}
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2018 Couchbase, Inc.
+ *
+ * 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 com.couchbase.client.java.bucket.api;
+
+import com.couchbase.client.core.message.kv.GetRequest;
+import com.couchbase.client.java.document.json.JsonObject;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Verifies the functionality of {@link Utils}.
+ */
+public class UtilsTest {
+
+ @Test
+ public void shouldFormatTimeout() {
+ GetRequest request = new GetRequest("key", "bucket");
+
+ // {"b":"bucket","s":"kv","t":1000,"i":"0x0"}
+ String result = Utils.formatTimeout(request, 1000);
+
+ JsonObject converted = JsonObject.fromJson(result);
+ assertEquals("bucket", converted.getString("b"));
+ assertEquals("kv", converted.getString("s"));
+ assertEquals(1000, (int) converted.getInt("t"));
+ assertTrue(converted.getString("i").startsWith("0x"));
+ assertFalse(converted.containsKey("c"));
+ assertFalse(converted.containsKey("l"));
+ assertFalse(converted.containsKey("r"));
+
+ request.lastLocalId("0E637F38FA73001A/FFFFFFFFFA809609");
+ request.lastLocalSocket("127.0.0.1:60119");
+ request.lastRemoteSocket("127.0.0.1:11210");
+
+ // {"b":"bucket","r":"127.0.0.1:11210","s":"kv","c":"0E637F38FA73001A/FFFFFFFFFA809609",
+ // "t":1000,"i":"0x0","l":"127.0.0.1:60119"}
+ result = Utils.formatTimeout(request, 1000);
+ converted = JsonObject.fromJson(result);
+
+ assertEquals("0E637F38FA73001A/FFFFFFFFFA809609", converted.getString("c"));
+ assertEquals("127.0.0.1:11210", converted.getString("r"));
+ assertEquals("127.0.0.1:60119", converted.getString("l"));
+ }
+
+}

0 comments on commit a657e52

Please sign in to comment.