Skip to content

Commit

Permalink
docs(examples+getstarted): add java
Browse files Browse the repository at this point in the history
  • Loading branch information
adnanrahic committed Oct 15, 2024
1 parent 3b12a19 commit c1d45e1
Show file tree
Hide file tree
Showing 19 changed files with 333 additions and 12 deletions.
36 changes: 36 additions & 0 deletions docs/docs/getting-started/configure-trace-ingestion.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ export OTEL_EXPORTER_OTLP_ENDPOINT="http://<tracetest-agent>:4318"
node -r ./tracing.js app.js
```

:::note View a code sample
[Visit the example in GitHub, here.](https://github.com/kubeshop/tracetest/tree/main/examples/getting-started/nodejs)
:::

</TabItem>
<TabItem value="python" label="Python">

Expand Down Expand Up @@ -176,6 +180,10 @@ export OTEL_EXPORTER_OTLP_ENDPOINT="http://<tracetest-agent>:4318"
opentelemetry-instrument python app.py
```

:::note View a code sample
[Visit the example in GitHub, here.](https://github.com/kubeshop/tracetest/tree/main/examples/getting-started/python)
:::

</TabItem>
<TabItem value="go" label="Go">

Expand Down Expand Up @@ -314,6 +322,34 @@ export OTEL_EXPORTER_OTLP_ENDPOINT="http://<tracetest-agent>:4318"
go run .
```

:::note View a code sample
[Visit the example in GitHub, here.](https://github.com/kubeshop/tracetest/tree/main/examples/getting-started/go)
:::

</TabItem>
<TabItem value="java" label="Java">

1. Download OpenTelemetry Java Agent

```bash title="Terminal"
curl -L -O https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/opentelemetry-javaagent.jar
```

2. Run with Jar and Include the OpenTelemetry Java Agent

```bash
export OTEL_SERVICE_NAME=my-service-name
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
export OTEL_EXPORTER_OTLP_ENDPOINT="http://<tracetest-agent>:4318"
# export OTEL_EXPORTER_OTLP_HEADERS="x-token=<token>"

java -javaagent:opentelemetry-javaagent.jar -jar /path/to/app.jar
```

:::note View a code sample
[Visit the example in GitHub, here.](https://github.com/kubeshop/tracetest/tree/main/examples/getting-started/java)
:::

</TabItem>
<TabItem value="otelcol" label="OpenTelemetry Collector">

Expand Down
37 changes: 25 additions & 12 deletions examples/getting-started/go/readme.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
# Dependencies

```bash
go get \
github.com/gorilla/mux v1.8.1 \
go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.56.0 \
go.opentelemetry.io/otel v1.31.0 \
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.31.0 \
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.31.0 \
go.opentelemetry.io/otel/sdk v1.31.0 \
go.opentelemetry.io/otel/trace v1.31.0
```
# Step-by-step

1. Install Dependencies

```bash
go get \
github.com/gorilla/mux v1.8.1 \
go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.56.0 \
go.opentelemetry.io/otel v1.31.0 \
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.31.0 \
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.31.0 \
go.opentelemetry.io/otel/sdk v1.31.0 \
go.opentelemetry.io/otel/trace v1.31.0
```

2. Start the App

```bash
export OTEL_SERVICE_NAME=my-service-name
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
export OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4318"
export OTEL_EXPORTER_OTLP_HEADERS="x-tracetest-token=bla"
go run .
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>hello-world-api</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer>
<mainClass>com.example.HelloWorldApi</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Binary file not shown.
50 changes: 50 additions & 0 deletions examples/getting-started/java/hello-world-api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>hello-world-api</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spark dependency -->
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.9.4</version>
</dependency>
</dependencies>

<build>
<plugins>
<!-- Maven Shade Plugin to create a "fat JAR" -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.HelloWorldApi</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example;

import static spark.Spark.*;

public class HelloWorldApi {
public static void main(String[] args) {
// Start a basic HTTP server on port 4567
port(4567);

// Define a simple route that returns "Hello, World!"
get("/hello", (req, res) -> "Hello, World!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}

/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}

/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
artifactId=hello-world-api
groupId=com.example
version=1.0-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com/example/HelloWorldApi.class
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/Users/adnanrahic/Code/kubeshop/tracetest/examples/getting-started/java/hello-world-api/src/main/java/com/example/HelloWorldApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com/example/AppTest.class
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/Users/adnanrahic/Code/kubeshop/tracetest/examples/getting-started/java/hello-world-api/src/test/java/com/example/AppTest.java
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report-3.0.xsd" version="3.0" name="com.example.AppTest" time="0.02" tests="1" errors="0" skipped="0" failures="0">
<properties>
<property name="java.specification.version" value="23"/>
<property name="sun.jnu.encoding" value="UTF-8"/>
<property name="java.class.path" value="/Users/adnanrahic/Code/kubeshop/tracetest/examples/getting-started/java/hello-world-api/target/test-classes:/Users/adnanrahic/Code/kubeshop/tracetest/examples/getting-started/java/hello-world-api/target/classes:/Users/adnanrahic/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar:/Users/adnanrahic/.m2/repository/com/sparkjava/spark-core/2.9.4/spark-core-2.9.4.jar:/Users/adnanrahic/.m2/repository/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/jetty-server/9.4.48.v20220622/jetty-server-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/jetty-http/9.4.48.v20220622/jetty-http-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/jetty-util/9.4.48.v20220622/jetty-util-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/jetty-io/9.4.48.v20220622/jetty-io-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/jetty-webapp/9.4.48.v20220622/jetty-webapp-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/jetty-xml/9.4.48.v20220622/jetty-xml-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/jetty-servlet/9.4.48.v20220622/jetty-servlet-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/jetty-security/9.4.48.v20220622/jetty-security-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/jetty-util-ajax/9.4.48.v20220622/jetty-util-ajax-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/websocket/websocket-server/9.4.48.v20220622/websocket-server-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/websocket/websocket-common/9.4.48.v20220622/websocket-common-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/websocket/websocket-client/9.4.48.v20220622/websocket-client-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/jetty-client/9.4.48.v20220622/jetty-client-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/websocket/websocket-servlet/9.4.48.v20220622/websocket-servlet-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/websocket/websocket-api/9.4.48.v20220622/websocket-api-9.4.48.v20220622.jar:"/>
<property name="java.vm.vendor" value="Oracle Corporation"/>
<property name="sun.arch.data.model" value="64"/>
<property name="java.vendor.url" value="https://java.oracle.com/"/>
<property name="os.name" value="Mac OS X"/>
<property name="java.vm.specification.version" value="23"/>
<property name="sun.java.launcher" value="SUN_STANDARD"/>
<property name="user.country" value="US"/>
<property name="sun.boot.library.path" value="/Library/Java/JavaVirtualMachines/jdk-23.jdk/Contents/Home/lib"/>
<property name="sun.java.command" value="/Users/adnanrahic/Code/kubeshop/tracetest/examples/getting-started/java/hello-world-api/target/surefire/surefirebooter-20241015104917420_3.jar /Users/adnanrahic/Code/kubeshop/tracetest/examples/getting-started/java/hello-world-api/target/surefire 2024-10-15T10-49-17_377-jvmRun1 surefire-20241015104917420_1tmp surefire_0-20241015104917420_2tmp"/>
<property name="http.nonProxyHosts" value="local|*.local|169.254/16|*.169.254/16"/>
<property name="jdk.debug" value="release"/>
<property name="surefire.test.class.path" value="/Users/adnanrahic/Code/kubeshop/tracetest/examples/getting-started/java/hello-world-api/target/test-classes:/Users/adnanrahic/Code/kubeshop/tracetest/examples/getting-started/java/hello-world-api/target/classes:/Users/adnanrahic/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar:/Users/adnanrahic/.m2/repository/com/sparkjava/spark-core/2.9.4/spark-core-2.9.4.jar:/Users/adnanrahic/.m2/repository/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/jetty-server/9.4.48.v20220622/jetty-server-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/jetty-http/9.4.48.v20220622/jetty-http-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/jetty-util/9.4.48.v20220622/jetty-util-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/jetty-io/9.4.48.v20220622/jetty-io-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/jetty-webapp/9.4.48.v20220622/jetty-webapp-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/jetty-xml/9.4.48.v20220622/jetty-xml-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/jetty-servlet/9.4.48.v20220622/jetty-servlet-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/jetty-security/9.4.48.v20220622/jetty-security-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/jetty-util-ajax/9.4.48.v20220622/jetty-util-ajax-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/websocket/websocket-server/9.4.48.v20220622/websocket-server-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/websocket/websocket-common/9.4.48.v20220622/websocket-common-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/websocket/websocket-client/9.4.48.v20220622/websocket-client-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/jetty-client/9.4.48.v20220622/jetty-client-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/websocket/websocket-servlet/9.4.48.v20220622/websocket-servlet-9.4.48.v20220622.jar:/Users/adnanrahic/.m2/repository/org/eclipse/jetty/websocket/websocket-api/9.4.48.v20220622/websocket-api-9.4.48.v20220622.jar:"/>
<property name="sun.cpu.endian" value="little"/>
<property name="user.home" value="/Users/adnanrahic"/>
<property name="user.language" value="en"/>
<property name="java.specification.vendor" value="Oracle Corporation"/>
<property name="java.version.date" value="2024-09-17"/>
<property name="java.home" value="/Library/Java/JavaVirtualMachines/jdk-23.jdk/Contents/Home"/>
<property name="file.separator" value="/"/>
<property name="basedir" value="/Users/adnanrahic/Code/kubeshop/tracetest/examples/getting-started/java/hello-world-api"/>
<property name="java.vm.compressedOopsMode" value="Zero based"/>
<property name="line.separator" value="&#10;"/>
<property name="java.vm.specification.vendor" value="Oracle Corporation"/>
<property name="java.specification.name" value="Java Platform API Specification"/>
<property name="apple.awt.application.name" value="ForkedBooter"/>
<property name="surefire.real.class.path" value="/Users/adnanrahic/Code/kubeshop/tracetest/examples/getting-started/java/hello-world-api/target/surefire/surefirebooter-20241015104917420_3.jar"/>
<property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"/>
<property name="ftp.nonProxyHosts" value="local|*.local|169.254/16|*.169.254/16"/>
<property name="java.runtime.version" value="23+37-2369"/>
<property name="user.name" value="adnanrahic"/>
<property name="stdout.encoding" value="UTF-8"/>
<property name="path.separator" value=":"/>
<property name="os.version" value="14.6.1"/>
<property name="java.runtime.name" value="Java(TM) SE Runtime Environment"/>
<property name="file.encoding" value="UTF-8"/>
<property name="java.vm.name" value="Java HotSpot(TM) 64-Bit Server VM"/>
<property name="localRepository" value="/Users/adnanrahic/.m2/repository"/>
<property name="java.vendor.url.bug" value="https://bugreport.java.com/bugreport/"/>
<property name="java.io.tmpdir" value="/var/folders/dl/rd5s6nq573lcblb_dgfb5y0h0000gn/T/"/>
<property name="java.version" value="23"/>
<property name="user.dir" value="/Users/adnanrahic/Code/kubeshop/tracetest/examples/getting-started/java/hello-world-api"/>
<property name="os.arch" value="aarch64"/>
<property name="java.vm.specification.name" value="Java Virtual Machine Specification"/>
<property name="native.encoding" value="UTF-8"/>
<property name="java.library.path" value="/Users/adnanrahic/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:."/>
<property name="java.vm.info" value="mixed mode, sharing"/>
<property name="stderr.encoding" value="UTF-8"/>
<property name="java.vendor" value="Oracle Corporation"/>
<property name="java.vm.version" value="23+37-2369"/>
<property name="sun.io.unicode.encoding" value="UnicodeBig"/>
<property name="socksNonProxyHosts" value="local|*.local|169.254/16|*.169.254/16"/>
<property name="java.class.version" value="67.0"/>
</properties>
<testcase name="testApp" classname="com.example.AppTest" time="0.001"/>
</testsuite>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-------------------------------------------------------------------------------
Test set: com.example.AppTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.020 s -- in com.example.AppTest
Binary file not shown.
61 changes: 61 additions & 0 deletions examples/getting-started/java/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Step-by-step

1. Create Maven Project

```bash
mvn archetype:generate -DgroupId=com.example -DartifactId=hello-world-api -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
```

2. Enter `hello-world-api` Directory

```bash
cd ./hello-world-api
```

3. Add Spark Dependency in `pom.xml`

```xml
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.9.4</version>
</dependency>
```

4. Install Maven Dependencies and Start App

```bash
mvn clean install
mvn exec:java -Dexec.mainClass="com.example.HelloWorldApi"
```

5. Run with Jar

```bash
mvn clean package
```

```bash
java -jar target/hello-world-api-1.0-SNAPSHOT.jar
```

6. Download OpenTelemetry Java Agent

```bash
curl -L -O https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/opentelemetry-javaagent.jar
```

7. Run with Jar and Include the OpenTelemetry Java Agent

```bash
mvn clean package
```

```bash
export OTEL_SERVICE_NAME=my-service-name
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
export OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4318"
export OTEL_EXPORTER_OTLP_HEADERS="x-token=bla"
java -javaagent:opentelemetry-javaagent.jar -jar target/hello-world-api-1.0-SNAPSHOT.jar
```

0 comments on commit c1d45e1

Please sign in to comment.