Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 4 additions & 4 deletions sdk/cosmos/azure-spring-data-cosmos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ For reactive repository support, use `@EnableReactiveCosmosRepositories`

### Response Diagnostics String and Query Metrics
2.2.x supports Response Diagnostics String and Query Metrics.
Set `populateQueryMetrics` flag to true in application.properties to enable query metrics.
Set `queryMetricsEnabled` flag to true in application.properties to enable query metrics.
In addition to setting the flag, implement `ResponseDiagnosticsProcessor` to log diagnostics information.
<!-- embedme src/samples/java/com/azure/cosmos/AppConfiguration.java#L21-L65 -->

Expand All @@ -145,16 +145,16 @@ public class AppConfiguration extends AbstractCosmosConfiguration {
@Value("${azure.cosmosdb.database}")
private String dbName;

@Value("${azure.cosmosdb.populateQueryMetrics}")
private boolean populateQueryMetrics;
@Value("${azure.cosmosdb.queryMetricsEnabled}")
private boolean queryMetricsEnabled;

private CosmosKeyCredential cosmosKeyCredential;

public CosmosDBConfig getConfig() {
this.cosmosKeyCredential = new CosmosKeyCredential(key);
CosmosDBConfig cosmosdbConfig = CosmosDBConfig.builder(uri,
this.cosmosKeyCredential, dbName).build();
cosmosdbConfig.setPopulateQueryMetrics(populateQueryMetrics);
cosmosdbConfig.setQueryMetricEnabled(queryMetricsEnabled);
cosmosdbConfig.setResponseDiagnosticsProcessor(new ResponseDiagnosticsProcessorImplementation());
return cosmosdbConfig;
}
Expand Down
35 changes: 18 additions & 17 deletions sdk/cosmos/azure-spring-data-cosmos/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,48 +42,41 @@
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.6.RELEASE</version> <!-- {x-version-update;org.springframework:spring-web;external_dependency} -->
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.6.RELEASE</version> <!-- {x-version-update;org.springframework:spring-beans;external_dependency} -->
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.6.RELEASE</version> <!-- {x-version-update;org.springframework:spring-context;external_dependency} -->
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.6.RELEASE</version> <!-- {x-version-update;org.springframework:spring-tx;external_dependency} -->
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.3.0.RELEASE</version> <!-- {x-version-update;org.springframework.data:spring-data-commons;external_dependency} -->
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.2.6.RELEASE</version> <!-- {x-version-update;org.springframework:spring-expression;external_dependency} -->
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<groupId>com.azure</groupId>
<artifactId>azure-cosmos</artifactId>
<version>3.7.3</version> <!-- {x-version-update;com.microsoft.azure:azure-cosmos;external_dependency} -->
<version>4.3.0-beta.1</version> <!-- {x-version-update;com.azure:azure-cosmos;current} -->
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
Expand All @@ -104,18 +97,21 @@
<artifactId>json</artifactId>
<version>20140107</version> <!-- {x-version-update;org.json:json;external_dependency} -->
</dependency>

<dependency>
<groupId>org.javatuples</groupId>
<artifactId>javatuples</artifactId>
<version>1.2</version> <!-- {x-version-update;org.javatuples:javatuples;external_dependency} -->
</dependency>

<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version> <!-- {x-version-update;javax.annotation:javax.annotation-api;external_dependency} -->
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version> <!-- {x-version-update;org.apache.commons:commons-lang3;external_dependency} -->
</dependency>

<!-- Test -->
<dependency>
Expand Down Expand Up @@ -148,26 +144,29 @@
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<version>3.3.5.RELEASE</version> <!-- {x-version-update;io.projectreactor:reactor-test;external_dependency} -->
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version> <!-- {x-version-update;org.slf4j:slf4j-simple;external_dependency} -->
<scope>test</scope>
</dependency>
<!-- Added this provided dependency to include necessary annotations used by "reactor-core".
Without this dependency, javadoc throws a warning as it cannot find enum When.MAYBE
which is used in @Nullable annotation in reactor core classes.

Similar provided dependency exits for "azure-core" as well.
-->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version> <!-- {x-version-update;com.google.code.gson:gson;external_dependency} -->
<scope>test</scope>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version> <!-- {x-version-update;com.google.code.findbugs:jsr305;external_dependency} -->
<scope>provided</scope>
</dependency>
</dependencies>

Expand Down Expand Up @@ -205,6 +204,8 @@
<include>org.json:json:[20140107]</include> <!-- {x-include-update;org.json:json;external_dependency} -->
<include>com.fasterxml.jackson.module:jackson-module-parameter-names:[2.10.0]</include> <!-- {x-include-update;com.fasterxml.jackson.module:jackson-module-parameter-names;external_dependency} -->
<include>javax.annotation:javax.annotation-api:[1.3.2]</include> <!-- {x-include-update;javax.annotation:javax.annotation-api;external_dependency} -->
<include>org.apache.commons:commons-lang3:[3.8.1]</include> <!-- {x-include-update;org.apache.commons:commons-lang3;external_dependency} -->
<include>com.google.code.findbugs:jsr305:[3.0.2]</include> <!-- {x-include-update;com.google.code.findbugs:jsr305;external_dependency} -->

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need com.google.code.findbugs:jsr305 in the bannedDependency includes. This particular entry was removed from this file as part of the same PR that fixed in the inheritance.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure @JimSuplizio - will get rid of it from bannedDependencies includes.

</includes>
</bannedDependencies>
</rules>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@
// Licensed under the MIT License.
package com.azure.spring.data.cosmos;

import com.azure.data.cosmos.IndexingMode;
import com.azure.cosmos.models.IndexingMode;

/**
* Constants class of CosmosDB properties
*/
public final class Constants {

public static final String DEFAULT_COLLECTION_NAME = "";
public static final boolean DEFAULT_INDEXINGPOLICY_AUTOMATIC = true;
public static final IndexingMode DEFAULT_INDEXINGPOLICY_MODE = IndexingMode.CONSISTENT;
public static final String DEFAULT_CONTAINER_NAME = "";
public static final boolean DEFAULT_INDEXING_POLICY_AUTOMATIC = true;
public static final IndexingMode DEFAULT_INDEXING_POLICY_MODE = IndexingMode.CONSISTENT;
public static final String DEFAULT_REPOSITORY_IMPLEMENT_POSTFIX = "Impl";
public static final int DEFAULT_TIME_TO_LIVE = -1; // Indicates never expire
public static final boolean DEFAULT_AUTO_CREATE_CONTAINER = true;

public static final String ID_PROPERTY_NAME = "id";

public static final String COSMOSDB_MODULE_NAME = "cosmosdb";
public static final String COSMOSDB_MODULE_PREFIX = "cosmosdb";
public static final String COSMOS_MODULE_NAME = "cosmosDb";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the brand name is Cosmos not CosmosDB.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I am pushing an update for these. thanks.

public static final String COSMOS_MODULE_PREFIX = "cosmosDb";
public static final String COSMOS_MAPPING_CONTEXT = "cosmosMappingContext";

public static final String USER_AGENT_SUFFIX = "spring-data/";

public static final String OBJECTMAPPER_BEAN_NAME = "cosmosdbObjectMapper";
public static final String OBJECT_MAPPER_BEAN_NAME = "cosmosDbObjectMapper";

public static final String ISO_8601_COMPATIBLE_DATE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss:SSSXXX";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.spring.data.cosmos;

import com.azure.cosmos.CosmosAsyncClient;
import com.azure.cosmos.CosmosClient;
import com.azure.cosmos.CosmosClientBuilder;
import com.azure.spring.data.cosmos.common.MacAddress;
import com.azure.spring.data.cosmos.common.PropertyLoader;
import com.azure.spring.data.cosmos.common.TelemetrySender;
import com.azure.spring.data.cosmos.config.CosmosDBConfig;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.NonNull;
import org.springframework.util.Assert;

import javax.annotation.PostConstruct;
import java.lang.reflect.Field;

/**
* Factory class for CosmosDb to create client
*/
public class CosmosDBFactory {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a public class? I wonder if it should be CosmosDBFactory or CosmosFactory. the same comment applies to all other public surface area, should we go with CosmosDB or Cosmos on the public surface.

our brand name is Cosmos

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, changing all CosmosDB to Cosmos in next commit on this PR.


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

private final CosmosDBConfig config;

private static final boolean IS_TELEMETRY_ALLOWED =
PropertyLoader.isApplicationTelemetryAllowed();

private static final String USER_AGENT_SUFFIX =
Constants.USER_AGENT_SUFFIX + PropertyLoader.getProjectVersion();

private String getUserAgentSuffix() {
String suffix = ";" + USER_AGENT_SUFFIX;

if (IS_TELEMETRY_ALLOWED || config.isAllowTelemetry()) {
suffix += ";" + MacAddress.getHashMac();
}

return suffix;
}

/**
* Validate config and initialization
*
* @param cosmosDBConfig cosmosDBConfig
*/
public CosmosDBFactory(@NonNull CosmosDBConfig cosmosDBConfig) {
validateConfig(cosmosDBConfig);

this.config = cosmosDBConfig;
}

/**
* To create a CosmosAsyncClient
*
* @return CosmosClient
*/
public CosmosAsyncClient getCosmosAsyncClient() {
final CosmosClientBuilder cosmosClientBuilderFromConfig =
getCosmosClientBuilderFromConfig(config);
return cosmosClientBuilderFromConfig.buildAsyncClient();
}

/**
* To create a CosmosClient
*
* @return CosmosSyncClient
*/
public CosmosClient getCosmosSyncClient() {
final CosmosClientBuilder cosmosClientBuilderFromConfig =
getCosmosClientBuilderFromConfig(config);
return cosmosClientBuilderFromConfig.buildClient();
}

private CosmosClientBuilder getCosmosClientBuilderFromConfig(CosmosDBConfig cosmosDBConfig) {
final CosmosClientBuilder cosmosClientBuilder = cosmosDBConfig.getCosmosClientBuilder();
cosmosClientBuilder.contentResponseOnWriteEnabled(true);
final String userAgentSuffixValue = getUserAgentSuffixValue(cosmosClientBuilder);
final String userAgentSuffix = getUserAgentSuffix() + userAgentSuffixValue;

return cosmosDBConfig.getCosmosClientBuilder().userAgentSuffix(userAgentSuffix);
}

private String getUserAgentSuffixValue(CosmosClientBuilder cosmosClientBuilder) {
final Field userAgentSuffix = FieldUtils.getDeclaredField(CosmosClientBuilder.class,
"userAgentSuffix", true);
try {
return (String)userAgentSuffix.get(cosmosClientBuilder);
} catch (IllegalAccessException e) {
logger.error("Error occurred while getting userAgentSuffix from CosmosClientBuilder",
e);
}
return "";
}

private void validateConfig(@NonNull CosmosDBConfig config) {
Assert.hasText(config.getDatabase(), "cosmosDb database should have text!");
}

@PostConstruct
private void sendTelemetry() {
// If any one of them is enabled, send telemetry data
if (IS_TELEMETRY_ALLOWED || config.isAllowTelemetry()) {
final TelemetrySender sender = new TelemetrySender();

sender.send(this.getClass().getSimpleName());
}
}

/**
* To get config object of cosmosDb
*
* @return CosmosDBConfig
*/
public CosmosDBConfig getConfig() {
return config;
}
}
Loading