Skip to content

Commit 398d7e8

Browse files
authored
Merge pull request #26 from Dynatrace/add_sample_for_1.8
2 parents 5b51be3 + 9e631bd commit 398d7e8

File tree

4 files changed

+212
-1
lines changed

4 files changed

+212
-1
lines changed

samples/README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Sample applications for OneAgent SDK for Java
1+
# Sample applications for OneAgent SDK for Java
22

33
Sample applications showing how to use Dynatrace OneAgent SDK for Java to create custom specific PurePaths and service calls.
44

@@ -18,6 +18,7 @@ Sample applications showing how to use Dynatrace OneAgent SDK for Java to create
1818
- run `mvn package` in root directory of the desired sample
1919

2020
### Run RemoteCall sample application
21+
2122
This Application shows how to trace remote calls and tag them. To run this sample you need to start a server and client sample application - both with Dynatrace OneAgent injected.
2223

2324
- Server: `mvn -pl remotecall-server exec:exec`
@@ -28,6 +29,7 @@ Check your Dynatrace environment for newly created service like that:
2829
![remotecall-server](img/remotecall-service.png)
2930

3031
### Run InProcessLinking sample application
32+
3133
This Application shows how to in-process-linking and custom service attributes are being used. To run this sample you need to create a custom service for your tenant - and of course Dynatrace OneAgent must be installed.
3234

3335
- ensure you have custom service for method `startAsyncOperation` in class `com.dynatrace.oneagent.sdk.samples.inprocesslinking.InProcessLinkingApp`
@@ -37,18 +39,29 @@ Check your Dynatrace environment for newly created services like that:
3739
![in-process-linking-service](img/in-process-linking-service.png)
3840

3941
### Run WebRequest sample application
42+
4043
This Application shows how to trace outgoing- and incoming webrequests. To run this sample you just go into the sample directory and run the sample by typing:
4144

4245
- run sample: `mvn exec:exec`
4346

4447
### Run Messaging sample application
48+
4549
This Application shows how to trace outgoing, receiving and processing of incoming messages. To run this sample you just go into the sample directory and run the sample by typing:
4650

4751
- run sample: `mvn exec:exec`
4852

4953
### Run DatabaseRequest sample application
54+
5055
This Application shows how to trace database requests. To run this sample you just go into the sample directory and run the sample by typing:
5156

5257
- run sample: `mvn exec:exec`
5358

5459
To run this sample you need to create a custom service for your tenant. See source of sample app for details.
60+
61+
### Run custom service sample application
62+
63+
This Application shows how to trace a custom service.
64+
To run this sample, go into the sample directory and run the sample by typing:
65+
66+
- run sample: `mvn exec:exec`
67+

samples/custom-service/pom.xml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.dynatrace.oneagent.sdk.samples.customservice</groupId>
8+
<artifactId>custom-service-sample</artifactId>
9+
<version>1.8.0</version>
10+
<packaging>jar</packaging>
11+
12+
<dependencies>
13+
<dependency>
14+
<groupId>com.dynatrace.oneagent.sdk.java</groupId>
15+
<artifactId>oneagent-sdk</artifactId>
16+
<version>1.8.0</version>
17+
<scope>compile</scope>
18+
</dependency>
19+
</dependencies>
20+
21+
<url>https://github.com/Dynatrace/OneAgent-SDK-Java</url>
22+
<name>Dynatrace OneAgent SDK Java custom service sample</name>
23+
<organization>
24+
<name>Dynatrace</name>
25+
<url>http://www.dynatrace.com</url>
26+
</organization>
27+
28+
<properties>
29+
<maven.compiler.source>1.6</maven.compiler.source>
30+
<maven.compiler.target>1.6</maven.compiler.target>
31+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
32+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
33+
34+
<!-- uncomment and replace url/tenant/token in case of manual agent injection -->
35+
<!--
36+
<agent.lib>path_to_installed_one_agent\agent\lib64\oneagentjava.dll</agent.lib>
37+
<agent.server>https://url.to.tenant.com</agent.server>
38+
<agent.tenant>tenantUUID</agent.tenant>
39+
<agent.tenantToken>tenantToken</agent.tenantToken>
40+
<agent.options>,debugOneAgentSdkJava=true</agent.options>
41+
<agent.agentpath>-agentpath:"${agent.lib}=name=SdkSample,server=${agent.server}"${agent.options},tenant=${agent.tenant},tenantToken=${agent.tenantToken}</agent.agentpath>
42+
-->
43+
</properties>
44+
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-jar-plugin</artifactId>
50+
<version>2.4</version>
51+
<configuration>
52+
<archive>
53+
<manifest>
54+
<addClasspath>true</addClasspath>
55+
<mainClass>com.dynatrace.oneagent.sdk.samples.customservice.CustomServiceApp</mainClass>
56+
</manifest>
57+
</archive>
58+
</configuration>
59+
</plugin>
60+
<plugin>
61+
<groupId>org.codehaus.mojo</groupId>
62+
<artifactId>exec-maven-plugin</artifactId>
63+
<version>1.6.0</version>
64+
<executions>
65+
<execution>
66+
<goals>
67+
<goal>exec</goal>
68+
</goals>
69+
</execution>
70+
</executions>
71+
<configuration>
72+
<executable>java</executable>
73+
<arguments>
74+
<argument>${agent.agentpath}</argument>
75+
<argument>-classpath</argument>
76+
<classpath/>
77+
<mainClass>com.dynatrace.oneagent.sdk.samples.customservice.CustomServiceApp</mainClass>
78+
</arguments>
79+
</configuration>
80+
</plugin>
81+
</plugins>
82+
</build>
83+
84+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.dynatrace.oneagent.sdk.samples.customservice;
2+
3+
import com.dynatrace.oneagent.sdk.OneAgentSDKFactory;
4+
import com.dynatrace.oneagent.sdk.api.CustomServiceTracer;
5+
import com.dynatrace.oneagent.sdk.api.OneAgentSDK;
6+
7+
/**
8+
* Sample application that shows how a custom service should be traced.
9+
*
10+
* @author wolfgang.ziegler@dynatrace.com
11+
*
12+
*/
13+
public class CustomServiceApp {
14+
15+
private final OneAgentSDK oneAgentSdk;
16+
17+
static CustomServiceApp instance;
18+
19+
private CustomServiceApp() {
20+
oneAgentSdk = OneAgentSDKFactory.createInstance();
21+
oneAgentSdk.setLoggingCallback(new StdErrLoggingCallback());
22+
switch (oneAgentSdk.getCurrentState()) {
23+
case ACTIVE:
24+
System.out.println("SDK is active and capturing.");
25+
break;
26+
case PERMANENTLY_INACTIVE:
27+
System.err.println(
28+
"SDK is PERMANENTLY_INACTIVE; Probably no OneAgent injected or OneAgent is incompatible with SDK.");
29+
break;
30+
case TEMPORARILY_INACTIVE:
31+
System.err.println(
32+
"SDK is TEMPORARILY_INACTIVE; OneAgent has been deactivated - check OneAgent configuration.");
33+
break;
34+
default:
35+
System.err.println("SDK is in unknown state.");
36+
break;
37+
}
38+
instance = this;
39+
}
40+
41+
public static void main(String args[]) {
42+
System.out.println("*************************************************************");
43+
System.out.println("** Running custom service request sample **");
44+
System.out.println("*************************************************************");
45+
try {
46+
CustomServiceApp app = new CustomServiceApp();
47+
48+
app.traceCustomService();
49+
50+
System.out.println("sample application stopped. sleeping a while, so OneAgent is able to send data to server ...");
51+
Thread.sleep(15000 * 3); // we have to wait - so OneAgent is able to send data to server
52+
} catch (Exception e) {
53+
System.err.println("custom service request sample failed: " + e.getMessage());
54+
e.printStackTrace();
55+
System.exit(-1);
56+
}
57+
}
58+
59+
private void traceCustomService() throws Exception {
60+
String serviceMethod = "onTimer";
61+
String serviceName = "PeriodicCleanupTask";
62+
CustomServiceTracer tracer = oneAgentSdk.traceCustomService(serviceMethod, serviceName);
63+
tracer.start();
64+
try {
65+
doOtherThings();
66+
} catch (Exception e) {
67+
tracer.error(e.getMessage());
68+
throw e;
69+
} finally {
70+
tracer.end();
71+
}
72+
}
73+
74+
private void doOtherThings() {
75+
// ...
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.dynatrace.oneagent.sdk.samples.customservice;
2+
3+
/*
4+
* Copyright 2021 Dynatrace LLC
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import com.dynatrace.oneagent.sdk.api.LoggingCallback;
20+
21+
/**
22+
* Implementation of OneAgent Logging Callback. Just printing output messages to
23+
* std err.
24+
*/
25+
public class StdErrLoggingCallback implements LoggingCallback {
26+
27+
@Override
28+
public void error(String message) {
29+
System.err.println("[OneAgent SDK ERROR]: " + message);
30+
}
31+
32+
@Override
33+
public void warn(String message) {
34+
System.err.println("[OneAgent SDK WARNING]: " + message);
35+
}
36+
37+
}

0 commit comments

Comments
 (0)