-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
76 changed files
with
41,481 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,4 @@ | ||
a8ec45c8ea4ba559247b654d01b0d35b21a68865 | ||
33f3224cb40e3fa8c56ddb88962e3a4e9319685d | ||
430a1a0a5dd4efe78e21526c37bec9dbce036401 | ||
|
||
|
||
|
||
|
||
d0129c1095216d5c830900c8a6223ef5d4274de1 | ||
d0129c1095216d5c830900c8a6223ef5d4274de1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...-datadog/src/main/java/com/commercetools/monitoring/datadog/statsd/DatadogMiddleware.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
|
||
package com.commercetools.monitoring.datadog.statsd; | ||
|
||
import static com.commercetools.monitoring.datadog.DatadogInfo.*; | ||
import static java.lang.String.format; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.Function; | ||
|
||
import com.timgroup.statsd.StatsDClient; | ||
|
||
import io.vrap.rmf.base.client.ApiHttpRequest; | ||
import io.vrap.rmf.base.client.ApiHttpResponse; | ||
import io.vrap.rmf.base.client.http.TelemetryMiddleware; | ||
|
||
/** | ||
* <p>The DatadogTelemetry middleware can be used to report outgoing request to commercetools to Datadog. | ||
* This middleware uses Datadog StatsD protocol to submit telemetry data. | ||
* It can be registered as TelemetryMiddleware to the {@link io.vrap.rmf.base.client.ClientBuilder#withTelemetryMiddleware(TelemetryMiddleware) ClientBuilder} | ||
* or the ApiRootBuilder.</p> | ||
* | ||
* {@include.example example.DatadogApiRootBuilderTest#addDatadogTelemetry()} | ||
* | ||
* The middleware adds the following metrics to Datadog: | ||
* <ul> | ||
* <li>commercetools.client.duration: The duration of the request in milliseconds</li> | ||
* <li>commercetools.client.total_requests: The total number of requests</li> | ||
* <li>commercetools.client.error_requests: The total number of requests with a status code greater or equal to 400</li> | ||
* </ul> | ||
* | ||
* <p>The metrics are enriched with the response status code, server address, port and request method.</p> | ||
* | ||
* <p>See also the <a href="https://github.com/commercetools/commercetools-sdk-java-v2/tree/main/examples/spring-datadog-statsd">Spring MVC example application</a> in the examples folder for further details.</p> | ||
*/ | ||
public class DatadogMiddleware implements TelemetryMiddleware { | ||
|
||
private final StatsDClient statsDClient; | ||
|
||
public DatadogMiddleware(final StatsDClient datadogStatsDClient) { | ||
this.statsDClient = datadogStatsDClient; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<ApiHttpResponse<byte[]>> invoke(ApiHttpRequest request, | ||
Function<ApiHttpRequest, CompletableFuture<ApiHttpResponse<byte[]>>> next) { | ||
final Instant start = Instant.now(); | ||
|
||
return next.apply(request).thenApply(response -> { | ||
final List<String> tags = new ArrayList<>(4); | ||
tags.add(format("%s:%s", HTTP_RESPONSE_STATUS_CODE, response.getStatusCode())); | ||
tags.add(format("%s:%s", HTTP_REQUEST_METHOD, request.getMethod().name())); | ||
tags.add(format("%s:%s", SERVER_ADDRESS, request.getUri().getHost())); | ||
if (request.getUri().getPort() > 0) { | ||
tags.add(format("%s:%s", SERVER_PORT, request.getUri().getPort())); | ||
} | ||
|
||
this.statsDClient.recordHistogramValue(PREFIX + "." + CLIENT_DURATION, | ||
Duration.between(start, Instant.now()).toMillis(), tags.toArray(new String[0])); | ||
|
||
this.statsDClient.incrementCounter(PREFIX + "." + CLIENT_REQUEST_TOTAL, tags.toArray(new String[0])); | ||
if (response.getStatusCode() >= 400) { | ||
this.statsDClient.incrementCounter(PREFIX + "." + CLIENT_REQUEST_ERROR, tags.toArray(new String[0])); | ||
} | ||
return response; | ||
}); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
.../src/main/java/com/commercetools/monitoring/datadog/statsd/DatadogResponseSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
|
||
package com.commercetools.monitoring.datadog.statsd; | ||
|
||
import static com.commercetools.monitoring.datadog.DatadogInfo.*; | ||
import static java.lang.String.format; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.JavaType; | ||
import com.timgroup.statsd.StatsDClient; | ||
|
||
import io.vrap.rmf.base.client.ApiHttpResponse; | ||
import io.vrap.rmf.base.client.ResponseSerializer; | ||
|
||
/** | ||
* This serializer uses dogstatsd library to submit metrics to datadog. | ||
* If you are not using statsd, use {@link com.commercetools.monitoring.datadog.DatadogResponseSerializer} to submit metrics to datadog with API. | ||
*/ | ||
public class DatadogResponseSerializer implements ResponseSerializer { | ||
private final ResponseSerializer serializer; | ||
|
||
private final StatsDClient statsDClient; | ||
|
||
public DatadogResponseSerializer(final ResponseSerializer serializer, final StatsDClient datadogStatsDClient) { | ||
this.serializer = serializer; | ||
this.statsDClient = datadogStatsDClient; | ||
} | ||
|
||
@Override | ||
public <O> ApiHttpResponse<O> convertResponse(ApiHttpResponse<byte[]> response, Class<O> outputType) { | ||
Instant start = Instant.now(); | ||
ApiHttpResponse<O> result = serializer.convertResponse(response, outputType); | ||
long durationInMillis = Duration.between(start, Instant.now()).toMillis(); | ||
this.statsDClient.recordHistogramValue(PREFIX + "." + JSON_SERIALIZATION, durationInMillis, | ||
format("%s:%s", RESPONSE_BODY_TYPE, outputType.getCanonicalName())); | ||
return result; | ||
} | ||
|
||
@Override | ||
public <O> ApiHttpResponse<O> convertResponse(ApiHttpResponse<byte[]> response, JavaType outputType) { | ||
Instant start = Instant.now(); | ||
ApiHttpResponse<O> result = serializer.convertResponse(response, outputType); | ||
long durationInMillis = Duration.between(start, Instant.now()).toMillis(); | ||
this.statsDClient.recordHistogramValue(PREFIX + "." + JSON_SERIALIZATION, durationInMillis, | ||
format("%s:%s", RESPONSE_BODY_TYPE, outputType.toString())); | ||
return result; | ||
} | ||
|
||
@Override | ||
public <O> ApiHttpResponse<O> convertResponse(ApiHttpResponse<byte[]> response, TypeReference<O> outputType) { | ||
Instant start = Instant.now(); | ||
ApiHttpResponse<O> result = serializer.convertResponse(response, outputType); | ||
long durationInMillis = Duration.between(start, Instant.now()).toMillis(); | ||
this.statsDClient.recordHistogramValue(PREFIX + "." + JSON_SERIALIZATION, durationInMillis, | ||
format("%s:%s", RESPONSE_BODY_TYPE, outputType.getType().getTypeName())); | ||
return result; | ||
} | ||
|
||
@Override | ||
public byte[] toJsonByteArray(Object value) throws JsonProcessingException { | ||
Instant start = Instant.now(); | ||
byte[] result = serializer.toJsonByteArray(value); | ||
long durationInMillis = Duration.between(start, Instant.now()).toMillis(); | ||
this.statsDClient.recordHistogramValue(PREFIX + "." + JSON_DESERIALIZATION, durationInMillis, | ||
format("%s:%s", REQUEST_BODY_TYPE, value.getClass().getCanonicalName())); | ||
return result; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
HELP.md | ||
.gradle | ||
build/ | ||
!gradle/wrapper/gradle-wrapper.jar | ||
!**/src/main/**/build/ | ||
!**/src/test/**/build/ | ||
**/application.properties | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
bin/ | ||
!**/src/main/**/bin/ | ||
!**/src/test/**/bin/ | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
out/ | ||
!**/src/main/**/out/ | ||
!**/src/test/**/out/ | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
|
||
### VS Code ### | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
17.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Spring MVC example app | ||
|
||
Example to show how Java SDK can be used in a Spring Boot application with Datadog monitoring and dogstatsd protocol. This example uses [java-dogstatsd-client](https://github.com/DataDog/java-dogstatsd-client). | ||
|
||
## Requirements | ||
|
||
- A Composable Commerce Project with a configured [API Client](https://docs.commercetools.com/tutorials/getting-started#creating-an-api-client). | ||
Necessary scopes: `view_published_products`, `manage_orders` | ||
- Your Project must have existing products containing variants with SKUs, and at least one customer, the storefront search endpoint must be active. | ||
- If your Project is currently empty, you can install the [SUNRISE sample data](https://docs.commercetools.com/sdk/sunrise-data). | ||
|
||
## Installation | ||
|
||
1. Clone/Download the example folder. | ||
2. Navigate to the path `spring-datadog-statsd/`. | ||
3. Register the client credentials in environment variables: | ||
`CTP_CLIENT_ID`, `CTP_CLIENT_SECRET`, `CTP_PROJECT_KEY` | ||
4. Add Datadog API key to environment variable as `DD_API_KEY`. If necessary, add `DD_SITE` to environmental variable as well. | ||
|
||
## Using the Spring MVC Example app | ||
|
||
### Start the server | ||
|
||
1. Open a new Terminal. | ||
2. Run `./gradlew bootRun`. | ||
3. The server starts. | ||
|
||
### Navigate the application | ||
|
||
1. Open a new browser window/tab | ||
2. Navigate to [http://localhost:8080/p](http://localhost:8080/p) and a list of products should appear. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
plugins { | ||
id 'java' | ||
id 'org.springframework.boot' version '3.0.4' | ||
id 'io.spring.dependency-management' version '1.1.0' | ||
} | ||
|
||
group = 'com.commercetools.sdk.examples' | ||
version = '0.0.1-SNAPSHOT' | ||
|
||
java { | ||
toolchain { | ||
languageVersion = JavaLanguageVersion.of(17) | ||
} | ||
} | ||
|
||
repositories { | ||
mavenLocal() | ||
mavenCentral() | ||
} | ||
|
||
ext { | ||
versions = [ | ||
commercetools: "17.7.0", | ||
] | ||
} | ||
|
||
dependencies { | ||
implementation "com.commercetools.sdk:commercetools-sdk-java-api:${versions.commercetools}" | ||
implementation "com.commercetools.sdk:commercetools-apachehttp-client:${versions.commercetools}" | ||
implementation "com.commercetools.sdk:commercetools-monitoring-datadog:${versions.commercetools}" | ||
implementation 'org.springframework.boot:spring-boot-starter-actuator' | ||
implementation 'org.springframework.boot:spring-boot-starter-security' | ||
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' | ||
implementation 'org.springframework.boot:spring-boot-starter-web' | ||
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6' | ||
implementation 'com.datadoghq:java-dogstatsd-client:4.3.0' | ||
testImplementation 'org.springframework.boot:spring-boot-starter-test' | ||
testImplementation 'org.springframework.security:spring-security-test' | ||
developmentOnly "org.springframework.boot:spring-boot-devtools" | ||
} | ||
|
||
tasks.named('test') { | ||
useJUnitPlatform() | ||
} | ||
|
Binary file not shown.
7 changes: 7 additions & 0 deletions
7
examples/spring-datadog-statsd/gradle/wrapper/gradle-wrapper.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip | ||
networkTimeout=10000 | ||
validateDistributionUrl=true | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
Oops, something went wrong.