Skip to content

Commit

Permalink
Added script logging enabled API to CosmosStoredProcedureRequestOptio…
Browse files Browse the repository at this point in the history
…ns (#12917)

* Added script logging enabled API to CosmosStoredProcedureRequestOptions

* Fixed java 8 compilation issue

* Updated getScriptLog API to decode before returning the response

* Added default value to java docs
  • Loading branch information
kushagraThapar authored Jul 8, 2020
1 parent c4b3c17 commit 2f4f39f
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ public String getResponseAsString() {
* @return the output string from the stored procedure console.log() statements.
*/
public String getScriptLog() {
return this.response.getResponseHeaders().get(HttpConstants.HttpHeaders.SCRIPT_LOG_RESULTS);
String scriptLog = this.response.getResponseHeaders().get(HttpConstants.HttpHeaders.SCRIPT_LOG_RESULTS);
return Utils.decodeAsUTF8String(scriptLog);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
import io.netty.buffer.ByteBuf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
Expand All @@ -46,6 +49,9 @@
* This is meant to be internally used only by our sdk.
*/
public class Utils {

private final static Logger logger = LoggerFactory.getLogger(Utils.class);

private static final int ONE_KB = 1024;
private static final ZoneId GMT_ZONE_ID = ZoneId.of("GMT");
public static final Base64.Encoder Base64Encoder = Base64.getEncoder();
Expand Down Expand Up @@ -94,6 +100,18 @@ public static String encodeBase64String(byte[] binaryData) {
return encodedString;
}

public static String decodeAsUTF8String(String inputString) {
if (inputString == null || inputString.isEmpty()) {
return inputString;
}
try {
return URLDecoder.decode(inputString, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
logger.warn("Error while decoding input string", e);
return inputString;
}
}

/**
* Checks whether the specified link is Name based or not
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public final class CosmosStoredProcedureRequestOptions {
private String sessionToken;
private String ifMatchETag;
private String ifNoneMatchETag;
private boolean scriptLoggingEnabled;

/**
* Gets the If-Match (ETag) associated with the request in the Azure Cosmos DB service.
Expand Down Expand Up @@ -115,13 +116,40 @@ public CosmosStoredProcedureRequestOptions setSessionToken(String sessionToken)
return this;
}

/**
* Gets whether Javascript stored procedure logging is enabled for the current request in the Azure Cosmos DB database
* service or not.
*
* Default value is false
*
* @return true if Javascript stored procedure logging is enabled
*/
public boolean isScriptLoggingEnabled() {
return scriptLoggingEnabled;
}

/**
* Sets whether Javascript stored procedure logging is enabled for the current request in the Azure Cosmos DB database
* service or not.
*
* Default value is false
*
* @param scriptLoggingEnabled true if stored procedure Javascript logging is enabled
* @return the CosmosStoredProcedureRequestOptions.
*/
public CosmosStoredProcedureRequestOptions setScriptLoggingEnabled(boolean scriptLoggingEnabled) {
this.scriptLoggingEnabled = scriptLoggingEnabled;
return this;
}

RequestOptions toRequestOptions() {
RequestOptions requestOptions = new RequestOptions();
requestOptions.setIfMatchETag(getIfMatchETag());
requestOptions.setIfNoneMatchETag(getIfNoneMatchETag());
requestOptions.setConsistencyLevel(getConsistencyLevel());
requestOptions.setPartitionKey(partitionKey);
requestOptions.setSessionToken(sessionToken);
requestOptions.setScriptLoggingEnabled(scriptLoggingEnabled);
return requestOptions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
Expand Down Expand Up @@ -148,19 +150,59 @@ public void deleteStoredProcedure() throws Exception {
}
@Test(groups = {"simple"}, timeOut = TIMEOUT)
public void executeStoredProcedure() throws Exception {
CosmosStoredProcedureProperties sproc = new CosmosStoredProcedureProperties(
CosmosStoredProcedureProperties storedProcedure = new CosmosStoredProcedureProperties(
UUID.randomUUID().toString(),
"function() {var x = 10;}"
);

CosmosStoredProcedureResponse response = container.getScripts().createStoredProcedure(sproc);
"function() {" +
" var mytext = \"x\";" +
" var myval = 1;" +
" try {" +
" console.log(\"The value of %s is %s.\", mytext, myval);" +
" getContext().getResponse().setBody(\"Success!\");" +
" }" +
" catch(err) {" +
" getContext().getResponse().setBody(\"inline err: [\" + err.number + \"] \" + err);" +
" }" +
"}");

container.getScripts().createStoredProcedure(storedProcedure);
CosmosStoredProcedureRequestOptions options = new CosmosStoredProcedureRequestOptions();
options.setPartitionKey(PartitionKey.NONE);
CosmosStoredProcedureResponse executeResponse = container.getScripts()
.getStoredProcedure(sproc.getId())
.getStoredProcedure(storedProcedure.getId())
.execute(null, options);

assertThat(executeResponse.getActivityId()).isNotEmpty();
assertThat(executeResponse.getScriptLog()).isNull();
}

@Test(groups = "simple", timeOut = TIMEOUT)
public void executeStoredProcedureWithScriptLoggingEnabled() throws Exception {
// Create a stored procedure
CosmosStoredProcedureProperties storedProcedure = new CosmosStoredProcedureProperties(
UUID.randomUUID().toString(),
"function() {" +
" var mytext = \"x\";" +
" var myval = 1;" +
" try {" +
" console.log(\"The value of %s is %s.\", mytext, myval);" +
" getContext().getResponse().setBody(\"Success!\");" +
" }" +
" catch(err) {" +
" getContext().getResponse().setBody(\"inline err: [\" + err.number + \"] \" + err);" +
" }" +
"}");

container.getScripts().createStoredProcedure(storedProcedure);
CosmosStoredProcedureRequestOptions options = new CosmosStoredProcedureRequestOptions();
options.setScriptLoggingEnabled(true);
options.setPartitionKey(PartitionKey.NONE);

CosmosStoredProcedureResponse executeResponse = container.getScripts()
.getStoredProcedure(storedProcedure.getId())
.execute(null, options);

String logResult = "The value of x is 1.";
assertThat(executeResponse.getScriptLog()).isEqualTo(logResult);
}

@Test(groups = {"simple"}, timeOut = TIMEOUT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.testng.annotations.Test;
import reactor.core.publisher.Mono;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -92,6 +94,37 @@ public void executeStoredProcedure() throws Exception {
assertThat(result).isEqualTo("\"0123456789\"");
}

@Test(groups = "simple", timeOut = TIMEOUT)
public void executeStoredProcedureWithScriptLoggingEnabled() throws Exception {
// Create a stored procedure
CosmosStoredProcedureProperties storedProcedure = new CosmosStoredProcedureProperties(
UUID.randomUUID().toString(),
"function() {" +
" var mytext = \"x\";" +
" var myval = 1;" +
" try {" +
" console.log(\"The value of %s is %s.\", mytext, myval);" +
" getContext().getResponse().setBody(\"Success!\");" +
" }" +
" catch(err) {" +
" getContext().getResponse().setBody(\"inline err: [\" + err.number + \"] \" + err);" +
" }" +
"}");

createdCollection.getScripts().createStoredProcedure(storedProcedure).block();
CosmosStoredProcedureRequestOptions options = new CosmosStoredProcedureRequestOptions();
options.setScriptLoggingEnabled(true);
options.setPartitionKey(PartitionKey.NONE);

CosmosStoredProcedureResponse executeResponse = createdCollection.getScripts()
.getStoredProcedure(storedProcedure.getId())
.execute(null, options).block();

String logResult = "The value of x is 1.";
assert executeResponse != null;
assertThat(executeResponse.getScriptLog()).isEqualTo(logResult);
}

@BeforeClass(groups = { "simple" }, timeOut = SETUP_TIMEOUT)
public void before_StoredProcedureUpsertReplaceTest() {
client = getClientBuilder().buildAsyncClient();
Expand Down

0 comments on commit 2f4f39f

Please sign in to comment.