Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POTEL 57 - Spring Boot 2 OTel samples #3879

Merged
merged 8 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions .github/workflows/system-tests-backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ jobs:
agent-auto-init: [ "true" ]
include:
- sample: "sentry-samples-spring-boot"
- sample: "sentry-samples-spring-boot-opentelemetry-noagent"
- sample: "sentry-samples-spring-boot-opentelemetry"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we also want to have a test with agent-auto-init: "false" like for spring boot 3?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah

agent: "1"
agent-auto-init: "true"
- sample: "sentry-samples-spring-boot-opentelemetry"
agent: "1"
agent-auto-init: "false"
- sample: "sentry-samples-spring-boot-webflux-jakarta"
- sample: "sentry-samples-spring-boot-webflux"
- sample: "sentry-samples-spring-boot-jakarta-opentelemetry-noagent"
Expand Down
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ apiValidation {
"sentry-samples-spring",
"sentry-samples-spring-jakarta",
"sentry-samples-spring-boot",
"sentry-samples-spring-boot-opentelemetry",
"sentry-samples-spring-boot-opentelemetry-noagent",
"sentry-samples-spring-boot-jakarta",
"sentry-samples-spring-boot-jakarta-opentelemetry",
"sentry-samples-spring-boot-jakarta-opentelemetry-noagent",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ tasks.register<Test>("systemTest").configure {
group = "verification"
description = "Runs the System tests"

// maxParallelForks = Runtime.getRuntime().availableProcessors() / 2
outputs.upToDateWhen { false }

maxParallelForks = 1

// Cap JVM args per test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.sentry.samples.spring.boot.jakarta;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.annotations.WithSpan;
import io.sentry.ISpan;
Expand All @@ -20,22 +20,18 @@
@RequestMapping("/person/")
public class PersonController {
private final PersonService personService;
private final OpenTelemetry openTelemetry;
private final Tracer tracer;
private static final Logger LOGGER = LoggerFactory.getLogger(PersonController.class);

public PersonController(PersonService personService, OpenTelemetry openTelemetry) {
public PersonController(PersonService personService, Tracer tracer) {
this.personService = personService;
this.openTelemetry = openTelemetry;
this.tracer = tracer;
}

@GetMapping("{id}")
@WithSpan("personSpanThroughOtelAnnotation")
Person person(@PathVariable Long id) {
Span span =
openTelemetry
.getTracer("tracerForSpringBootDemo")
.spanBuilder("spanCreatedThroughOtelApi")
.startSpan();
Span span = tracer.spanBuilder("spanCreatedThroughOtelApi").startSpan();
try (final @NotNull Scope spanScope = span.makeCurrent()) {
ISpan currentSpan = Sentry.getSpan();
ISpan sentrySpan = currentSpan.startChild("spanCreatedThroughSentryApi");
Expand All @@ -52,11 +48,7 @@ Person person(@PathVariable Long id) {

@PostMapping
Person create(@RequestBody Person person) {
Span span =
openTelemetry
.getTracer("tracerForSpringBootDemo")
.spanBuilder("spanCreatedThroughOtelApi")
.startSpan();
Span span = tracer.spanBuilder("spanCreatedThroughOtelApi").startSpan();
try (final @NotNull Scope spanScope = span.makeCurrent()) {
ISpan sentrySpan = Sentry.getSpan().startChild("spanCreatedThroughSentryApi");
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,3 @@ spring.graphql.websocket.path=/graphql
spring.quartz.job-store-type=memory

otel.propagators=tracecontext,baggage,sentry
otel.resource.attributes.deployment.environment=dev
otel.resource.attributes.service.name=cart
otel.resource.attributes.service.namespace=shop

logging.level.org.springframework: DEBUG
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ dependencies {
implementation(projects.sentryLogback)
implementation(projects.sentryGraphql22)
implementation(projects.sentryQuartz)
implementation(Config.Libs.springBoot3StarterOpenTelemetry)
implementation(projects.sentryOpentelemetry.sentryOpentelemetryBootstrap)
implementation(projects.sentryOpentelemetry.sentryOpentelemetryAgentcustomization)
implementation(Config.Libs.OpenTelemetry.otelSdk)

// database query tracing
implementation(projects.sentryJdbc)
Expand Down Expand Up @@ -85,7 +83,8 @@ tasks.register<Test>("systemTest").configure {
group = "verification"
description = "Runs the System tests"

// maxParallelForks = Runtime.getRuntime().availableProcessors() / 2
outputs.upToDateWhen { false }

maxParallelForks = 1

// Cap JVM args per test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package io.sentry.samples.spring.boot.jakarta;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.annotations.WithSpan;
import io.sentry.ISpan;
import io.sentry.Sentry;
import org.jetbrains.annotations.NotNull;
Expand All @@ -20,24 +19,17 @@
@RequestMapping("/person/")
public class PersonController {
private final PersonService personService;
private final OpenTelemetry openTelemetry;
private final Tracer tracer;
private static final Logger LOGGER = LoggerFactory.getLogger(PersonController.class);

public PersonController(PersonService personService, OpenTelemetry openTelemetry) {
public PersonController(PersonService personService, Tracer tracer) {
this.personService = personService;
this.openTelemetry = openTelemetry;
this.tracer = tracer;
}

@GetMapping("{id}")
@WithSpan("personSpanThroughOtelAnnotation")
Person person(@PathVariable Long id) {
ISpan annotationSpan = Sentry.getSpan();
System.out.println(annotationSpan);
Span span =
openTelemetry
.getTracer("tracerForSpringBootDemo")
.spanBuilder("spanCreatedThroughOtelApi")
.startSpan();
Span span = tracer.spanBuilder("spanCreatedThroughOtelApi").startSpan();
try (final @NotNull Scope spanScope = span.makeCurrent()) {
ISpan currentSpan = Sentry.getSpan();
ISpan sentrySpan = currentSpan.startChild("spanCreatedThroughSentryApi");
Expand All @@ -54,11 +46,7 @@ Person person(@PathVariable Long id) {

@PostMapping
Person create(@RequestBody Person person) {
Span span =
openTelemetry
.getTracer("tracerForSpringBootDemo")
.spanBuilder("spanCreatedThroughOtelApi")
.startSpan();
Span span = tracer.spanBuilder("spanCreatedThroughOtelApi").startSpan();
try (final @NotNull Scope spanScope = span.makeCurrent()) {
ISpan sentrySpan = Sentry.getSpan().startChild("spanCreatedThroughSentryApi");
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.sentry.samples.spring.boot.jakarta;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Scope;
import io.sentry.ISpan;
import io.sentry.Sentry;
Expand All @@ -22,26 +22,19 @@ public class TodoController {
private final RestTemplate restTemplate;
private final WebClient webClient;
private final RestClient restClient;
private final OpenTelemetry openTelemetry;
private final Tracer tracer;

public TodoController(
RestTemplate restTemplate,
WebClient webClient,
RestClient restClient,
OpenTelemetry openTelemetry) {
RestTemplate restTemplate, WebClient webClient, RestClient restClient, Tracer tracer) {
this.restTemplate = restTemplate;
this.webClient = webClient;
this.restClient = restClient;
this.openTelemetry = openTelemetry;
this.tracer = tracer;
}

@GetMapping("/todo/{id}")
Todo todo(@PathVariable Long id) {
Span otelSpan =
openTelemetry
.getTracer("tracerForSpringBootDemo")
.spanBuilder("todoSpanOtelApi")
.startSpan();
Span otelSpan = tracer.spanBuilder("todoSpanOtelApi").startSpan();
try (final @NotNull Scope spanScope = otelSpan.makeCurrent()) {
ISpan sentrySpan = Sentry.getSpan().startChild("todoSpanSentryApi");
try {
Expand Down Expand Up @@ -74,11 +67,7 @@ Todo todoWebClient(@PathVariable Long id) {

@GetMapping("/todo-restclient/{id}")
Todo todoRestClient(@PathVariable Long id) {
Span span =
openTelemetry
.getTracer("tracerForSpringBootDemo")
.spanBuilder("todoRestClientSpanOtelApi")
.startSpan();
Span span = tracer.spanBuilder("todoRestClientSpanOtelApi").startSpan();
try (final @NotNull Scope spanScope = span.makeCurrent()) {
ISpan sentrySpan = Sentry.getSpan().startChild("todoRestClientSpanSentryApi");
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,3 @@ spring.datasource.password=
spring.graphql.graphiql.enabled=true
spring.graphql.websocket.path=/graphql
spring.quartz.job-store-type=memory

otel.propagators=tracecontext,baggage,sentry
otel.resource.attributes.deployment.environment=dev
otel.resource.attributes.service.name=cart
otel.resource.attributes.service.namespace=shop

logging.level.org.springframework: DEBUG
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ tasks.register<Test>("systemTest").configure {
group = "verification"
description = "Runs the System tests"

outputs.upToDateWhen { false }

maxParallelForks = 1

// Cap JVM args per test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Sentry Sample Spring Boot

Sample application showing how to use Sentry with [Spring boot](http://spring.io/projects/spring-boot).

## How to run?

To see events triggered in this sample application in your Sentry dashboard, go to `src/main/resources/application.properties` and replace the test DSN with your own DSN.

Then, execute a command from the module directory:

```
../../gradlew bootRun
```

Make an HTTP request that will trigger events:

```
curl -XPOST --user user:password http://localhost:8080/person/ -H "Content-Type:application/json" -d '{"firstName":"John","lastName":"Smith"}'
```


## GraphQL

The following queries can be used to test the GraphQL integration.

### Greeting
```
{
greeting(name: "crash")
}
```

### Greeting with variables

```
query GreetingQuery($name: String) {
greeting(name: $name)
}
```
variables:
```
{
"name": "crash"
}
```

### Project

```
query ProjectQuery($slug: ID!) {
project(slug: $slug) {
slug
name
repositoryUrl
status
}
}
```
variables:
```
{
"slug": "statuscrash"
}
```

### Mutation

```
mutation AddProjectMutation($slug: ID!) {
addProject(slug: $slug)
}
```
variables:
```
{
"slug": "nocrash",
"name": "nocrash"
}
```

### Subscription

```
subscription SubscriptionNotifyNewTask($slug: ID!) {
notifyNewTask(projectSlug: $slug) {
id
name
assigneeId
assignee {
id
name
}
}
}
```
variables:
```
{
"slug": "crash"
}
```

### Data loader

```
query TasksAndAssigneesQuery($slug: ID!) {
tasks(projectSlug: $slug) {
id
name
assigneeId
assignee {
id
name
}
}
}
```
variables:
```
{
"slug": "crash"
}
```
Loading
Loading