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

Rework getting stated java #598

Merged
merged 4 commits into from
Jun 4, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The [Getting Started Guides](./getting-started-guides/README.md) demonstrate how

* [.NET](./getting-started-guides/dotnet)
* [Go](./getting-started-guides/go)
* Java ([uninstrumented](./getting-started-guides/java/uninstrumented) / [instrumented](./getting-started-guides/java/instrumented))
* [Java](./getting-started-guides/java)
* [JavaScript (Node.js)](./getting-started-guides/javascript)
* [Python](./getting-started-guides/python)
* [Ruby](./getting-started-guides/ruby)
Expand Down
14 changes: 14 additions & 0 deletions getting-started-guides/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ services:
- OTEL_RESOURCE_ATTRIBUTES=service.instance.id=123
ports:
- 8080
java:
build: java
environment:
- OTEL_EXPORTER_OTLP_ENDPOINT
- OTEL_EXPORTER_OTLP_HEADERS
- OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT=4095
- OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE=DELTA
- OTEL_EXPERIMENTAL_EXPORTER_OTLP_RETRY_ENABLED=true
- OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION=BASE2_EXPONENTIAL_BUCKET_HISTOGRAM
- OTEL_EXPORTER_OTLP_COMPRESSION=gzip
- OTEL_SERVICE_NAME=getting-started-java
- OTEL_EXPERIMENTAL_RESOURCE_DISABLED_KEYS=process.command_args
ports:
- 8080
javascript:
build: javascript
environment:
Expand Down
20 changes: 20 additions & 0 deletions getting-started-guides/java/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# syntax=docker/dockerfile:1

FROM eclipse-temurin:17 as build

COPY . /source

WORKDIR /source

RUN ./gradlew bootJar

FROM eclipse-temurin:17 as final

WORKDIR /app

COPY --from=build /source/build/libs/getting-started-java.jar .
COPY --from=build /source/build/agent/opentelemetry-javaagent.jar .

ENTRYPOINT ["java", "-javaagent:opentelemetry-javaagent.jar", "-jar", "getting-started-java.jar"]

EXPOSE 8080
42 changes: 42 additions & 0 deletions getting-started-guides/java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Getting Started Guide - Java

This is a simple application instrumented
with [OpenTelemetry Java's](https://github.com/open-telemetry/opentelemetry-java) [automatic instrumentation javaagent](https://opentelemetry.io/docs/languages/java/automatic/).
It demonstrates how to configure OpenTelemetry Java to send data to New Relic.

## Requirements

* Java JDK 17+, due to the use of Spring Boot 3; [Java 8+ otherwise](https://github.com/open-telemetry/opentelemetry-java/blob/main/VERSIONING.md#language-version-compatibility)
* [A New Relic account](https://one.newrelic.com/)
* [A New Relic license key](https://docs.newrelic.com/docs/apis/intro-apis/new-relic-api-keys/#license-key)

## Running the application

1. Set the following environment variables to configure OpenTelemetry to send
data to New Relic:

```shell
export OTEL_EXPORTER_OTLP_ENDPOINT=https://otlp.nr-data.net
export OTEL_EXPORTER_OTLP_HEADERS=api-key=<your_license_key>
export OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT=4095
export OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE=DELTA
export OTEL_EXPERIMENTAL_EXPORTER_OTLP_RETRY_ENABLED=true
export OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION=BASE2_EXPONENTIAL_BUCKET_HISTOGRAM
export OTEL_EXPORTER_OTLP_COMPRESSION=gzip
export OTEL_SERVICE_NAME=getting-started-java
export OTEL_EXPERIMENTAL_RESOURCE_DISABLED_KEYS=process.command_args
```

* If your account is based in the EU, set the endpoint to: [https://otlp.eu01.nr-data.net](https://otlp.eu01.nr-data.net)

2. Run the application with the following command and open
[http://localhost:8080/fibonacci?n=1](http://localhost:8080/fibonacci?n=1)
in your web browser to ensure it is working.

```shell
./gradlew bootRun
```

3. Experiment with providing different values for `n` in the query string.
Valid values are between 1 and 90. Values outside this range cause an error
which will show up in New Relic.
77 changes: 58 additions & 19 deletions getting-started-guides/java/build.gradle
Original file line number Diff line number Diff line change
@@ -1,25 +1,64 @@
import org.springframework.boot.gradle.plugin.SpringBootPlugin

plugins {
id 'com.diffplug.spotless' apply false
id 'java'
id 'com.diffplug.spotless' version '6.25.0'
id 'org.springframework.boot' version '3.2.5'
}

subprojects {
pluginManager.withPlugin('java') {
apply plugin: 'com.diffplug.spotless'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}

java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
spotless {
java {
googleJavaFormat()
}
}

spotless {
java {
googleJavaFormat()
}
}
repositories {
mavenCentral()
}

repositories {
mavenCentral()
}
}
}
configurations {
// Create a new configuration to hold the otel java agent
agent
}

dependencies {
implementation platform(SpringBootPlugin.BOM_COORDINATES)
implementation 'org.springframework.boot:spring-boot-starter-web'

implementation("io.opentelemetry:opentelemetry-api:1.38.0")

// Add OpenTelemetry java agent to the "agent" configuration we previously defined
agent("io.opentelemetry.javaagent:opentelemetry-javaagent:2.3.0")
}

tasks.register("copyAgent", Copy) {
from(configurations.agent.singleFile)
into(layout.buildDirectory.dir('agent'))
rename("opentelemetry-javaagent-.*\\.jar", "opentelemetry-javaagent.jar")
}

bootJar {
archiveBaseName.set('getting-started-java')
// Before running, copy the agent to a reliable place in the build dir
dependsOn("copyAgent")
}

bootRun {
// Before running, copy the agent to a reliable place in the build dir
dependsOn("copyAgent")

mainClass.set 'com.example.demo.Application'

def agentPath = project.buildDir.toString() + "/agent/opentelemetry-javaagent.jar"
jvmArgs = [
// Set the opentelemetry-java-instrumentation agent as the javaagent
"-javaagent:${agentPath}"
]

}
40 changes: 0 additions & 40 deletions getting-started-guides/java/instrumented/README.md

This file was deleted.

33 changes: 0 additions & 33 deletions getting-started-guides/java/instrumented/build.gradle

This file was deleted.

14 changes: 0 additions & 14 deletions getting-started-guides/java/instrumented/call-app.ps1

This file was deleted.

21 changes: 0 additions & 21 deletions getting-started-guides/java/instrumented/call-app.sh

This file was deleted.

7 changes: 0 additions & 7 deletions getting-started-guides/java/instrumented/load-generator.ps1

This file was deleted.

10 changes: 0 additions & 10 deletions getting-started-guides/java/instrumented/load-generator.sh

This file was deleted.

This file was deleted.

11 changes: 0 additions & 11 deletions getting-started-guides/java/settings.gradle

This file was deleted.

Loading