Skip to content

Commit 08d319b

Browse files
Merge pull request #279163 from jeanbisutti/add-java-native-to-add-modify
Add Java native to the add/modify page
2 parents 92fc3fd + 75f3d23 commit 08d319b

File tree

1 file changed

+259
-1
lines changed

1 file changed

+259
-1
lines changed

articles/azure-monitor/app/opentelemetry-add-modify.md

Lines changed: 259 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,28 @@ Telemetry emitted by these Azure SDKs is automatically collected by default:
138138
[//]: # "}"
139139
[//]: # "console.log(str)"
140140

141+
142+
#### [Java Native](#tab/java-native)
143+
144+
Requests for Spring Boot native applications
145+
* Spring Web
146+
* Spring Web MVC
147+
* Spring WebFlux
148+
149+
Dependencies for Spring Boot native applications
150+
* JDBC
151+
* R2DBC
152+
* MongoDB
153+
* Kafka
154+
155+
Metrics
156+
* Micrometer Metrics
157+
158+
Logs for Spring Boot native applications
159+
* Logback
160+
161+
For Quartz native applications, please look at the [Quarkus documentation](https://quarkus.io/guides/opentelemetry).
162+
141163
#### [Node.js](#tab/nodejs)
142164

143165
The following OpenTelemetry Instrumentation libraries are included as part of the Azure Monitor Application Insights Distro. For more information, see [Azure SDK for JavaScript](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/monitor/monitor-opentelemetry/README.md#instrumentation-libraries).
@@ -278,6 +300,10 @@ var metricsProvider = Sdk.CreateMeterProviderBuilder()
278300
### [Java](#tab/java)
279301
You can't extend the Java Distro with community instrumentation libraries. To request that we include another instrumentation library, open an issue on our GitHub page. You can find a link to our GitHub page in [Next Steps](#next-steps).
280302

303+
### [Java Native](#tab/java-native)
304+
305+
You can't use commmunity instrumentation libraries with GraalVM Java native applications.
306+
281307
### [Node.js](#tab/nodejs)
282308

283309
Other OpenTelemetry Instrumentations are available [here](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node) and could be added using TraceHandler in ApplicationInsightsClient.
@@ -520,6 +546,39 @@ public class Program {
520546
}
521547
```
522548

549+
#### [Java Native](#tab/java-native)
550+
551+
1. Inject `OpenTelemetry`
552+
553+
_Spring_
554+
```java
555+
import io.opentelemetry.api.OpenTelemetry;
556+
557+
@Autowired
558+
OpenTelemetry openTelemetry;
559+
```
560+
561+
_Quarkus_
562+
```java
563+
import io.opentelemetry.api.OpenTelemetry;
564+
565+
@Inject
566+
OpenTelemetry openTelemetry;
567+
```
568+
569+
570+
1. Create an histogram
571+
```java
572+
import io.opentelemetry.api.metrics.DoubleHistogram;
573+
import io.opentelemetry.api.metrics.Meter;
574+
575+
Meter meter = openTelemetry.getMeter("OTEL.AzureMonitor.Demo");
576+
DoubleHistogram histogram = meter.histogramBuilder("histogram").build();
577+
histogram.record(1.0);
578+
histogram.record(100.0);
579+
histogram.record(30.0);
580+
```
581+
523582
#### [Node.js](#tab/nodejs)
524583

525584
```javascript
@@ -684,6 +743,47 @@ public class Program {
684743
}
685744
}
686745
```
746+
#### [Java Native](#tab/java-native)
747+
748+
1. Inject `OpenTelemetry`
749+
750+
_Spring_
751+
```java
752+
import io.opentelemetry.api.OpenTelemetry;
753+
754+
@Autowired
755+
OpenTelemetry openTelemetry;
756+
```
757+
758+
_Quarkus_
759+
```java
760+
import io.opentelemetry.api.OpenTelemetry;
761+
762+
@Inject
763+
OpenTelemetry openTelemetry;
764+
```
765+
766+
1. Create the counter
767+
768+
```Java
769+
import io.opentelemetry.api.common.AttributeKey;
770+
import io.opentelemetry.api.common.Attributes;
771+
import io.opentelemetry.api.metrics.LongCounter;
772+
import io.opentelemetry.api.metrics.Meter;
773+
774+
775+
Meter meter = openTelemetry.getMeter("OTEL.AzureMonitor.Demo");
776+
777+
LongCounter myFruitCounter = meter.counterBuilder("MyFruitCounter")
778+
.build();
779+
780+
myFruitCounter.add(1, Attributes.of(AttributeKey.stringKey("name"), "apple", AttributeKey.stringKey("color"), "red"));
781+
myFruitCounter.add(2, Attributes.of(AttributeKey.stringKey("name"), "lemon", AttributeKey.stringKey("color"), "yellow"));
782+
myFruitCounter.add(1, Attributes.of(AttributeKey.stringKey("name"), "lemon", AttributeKey.stringKey("color"), "yellow"));
783+
myFruitCounter.add(2, Attributes.of(AttributeKey.stringKey("name"), "apple", AttributeKey.stringKey("color"), "green"));
784+
myFruitCounter.add(5, Attributes.of(AttributeKey.stringKey("name"), "apple", AttributeKey.stringKey("color"), "red"));
785+
myFruitCounter.add(4, Attributes.of(AttributeKey.stringKey("name"), "lemon", AttributeKey.stringKey("color"), "yellow"));
786+
```
687787

688788
#### [Node.js](#tab/nodejs)
689789

@@ -859,6 +959,41 @@ public class Program {
859959
}
860960
}
861961
```
962+
#### [Java Native](#tab/java-native)
963+
964+
1. Inject `OpenTelemetry`
965+
966+
_Spring_
967+
```java
968+
import io.opentelemetry.api.OpenTelemetry;
969+
970+
@Autowired
971+
OpenTelemetry openTelemetry;
972+
```
973+
974+
_Quarkus_
975+
```java
976+
import io.opentelemetry.api.OpenTelemetry;
977+
978+
@Inject
979+
OpenTelemetry openTelemetry;
980+
```
981+
982+
1. Create a gauge
983+
```Java
984+
import io.opentelemetry.api.common.AttributeKey;
985+
import io.opentelemetry.api.common.Attributes;
986+
import io.opentelemetry.api.metrics.Meter;
987+
988+
Meter meter = openTelemetry.getMeter("OTEL.AzureMonitor.Demo");
989+
990+
meter.gaugeBuilder("gauge")
991+
.buildWithCallback(
992+
observableMeasurement -> {
993+
double randomNumber = Math.floor(Math.random() * 100);
994+
observableMeasurement.record(randomNumber, Attributes.of(AttributeKey.stringKey("testKey"), "testValue"));
995+
});
996+
```
862997

863998
#### [Node.js](#tab/nodejs)
864999

@@ -1047,6 +1182,18 @@ You can use `opentelemetry-api` to update the status of a span and record except
10471182
span.recordException(e);
10481183
```
10491184

1185+
#### [Java Native](#tab/java-native)
1186+
1187+
Set status to `error` and record an exception in your code:
1188+
1189+
```java
1190+
import io.opentelemetry.api.trace.Span;
1191+
import io.opentelemetry.api.trace.StatusCode;
1192+
1193+
Span span = Span.current();
1194+
span.setStatus(StatusCode.ERROR, "errorMessage");
1195+
span.recordException(e);
1196+
```
10501197
#### [Node.js](#tab/nodejs)
10511198

10521199
```javascript
@@ -1252,6 +1399,45 @@ you can add your spans by using the OpenTelemetry API.
12521399
static final Tracer tracer = GlobalOpenTelemetry.getTracer("com.example");
12531400
```
12541401

1402+
1. Create a span, make it current, and then end it:
1403+
1404+
```java
1405+
Span span = tracer.spanBuilder("my first span").startSpan();
1406+
try (Scope ignored = span.makeCurrent()) {
1407+
// do stuff within the context of this
1408+
} catch (Throwable t) {
1409+
span.recordException(t);
1410+
} finally {
1411+
span.end();
1412+
}
1413+
```
1414+
1415+
#### [Java Native](#tab/java-native)
1416+
1417+
1. Inject `OpenTelemetry`
1418+
1419+
_Spring_
1420+
```java
1421+
import io.opentelemetry.api.OpenTelemetry;
1422+
1423+
@Autowired
1424+
OpenTelemetry openTelemetry;
1425+
```
1426+
1427+
_Quarkus_
1428+
```java
1429+
import io.opentelemetry.api.OpenTelemetry;
1430+
@Inject
1431+
OpenTelemetry openTelemetry;
1432+
```
1433+
1. Create a `Tracer`:
1434+
1435+
```java
1436+
import io.opentelemetry.api.trace.Tracer;
1437+
1438+
static final Tracer tracer = openTelemetry.getTracer("com.example");
1439+
```
1440+
12551441
1. Create a span, make it current, and then end it:
12561442

12571443
```java
@@ -1374,6 +1560,18 @@ You can use `opentelemetry-api` to create span events, which populate the `trace
13741560
Span.current().addEvent("eventName");
13751561
```
13761562
1563+
#### [Java Native](#tab/java-native)
1564+
1565+
You can use OpenTelemetry API to create span events, which populate the `traces` table in Application Insights. The string passed in to `addEvent()` is saved to the `message` field within the trace.
1566+
1567+
Add span events in your code:
1568+
1569+
```java
1570+
import io.opentelemetry.api.trace.Span;
1571+
1572+
Span.current().addEvent("eventName");
1573+
```
1574+
13771575
#### [Node.js](#tab/nodejs)
13781576
13791577
Currently unavailable.
@@ -1497,7 +1695,11 @@ telemetryClient.TrackEvent("testEvent");
14971695
} catch (Exception e) {
14981696
telemetryClient.trackException(e);
14991697
}
1500-
```
1698+
1699+
1700+
#### [Java Native](#tab/java-native)
1701+
1702+
It's not possible to send custom telemetry using the Application Insights Classic API in Java Native.
15011703

15021704
#### [Node.js](#tab/nodejs)
15031705

@@ -1726,6 +1928,18 @@ Adding one or more span attributes populates the `customDimensions` field in the
17261928
Span.current().setAttribute(attributeKey, "myvalue1");
17271929
```
17281930

1931+
##### [Java Native](#tab/java-native)
1932+
1933+
Add custom dimensions in your code:
1934+
1935+
```java
1936+
import io.opentelemetry.api.trace.Span;
1937+
import io.opentelemetry.api.common.AttributeKey;
1938+
1939+
AttributeKey attributeKey = AttributeKey.stringKey("mycustomdimension");
1940+
Span.current().setAttribute(attributeKey, "myvalue1");
1941+
```
1942+
17291943
##### [Node.js](#tab/nodejs)
17301944

17311945
```typescript
@@ -1833,6 +2047,10 @@ activity.SetTag("http.client_ip", "<IP Address>");
18332047

18342048
Java automatically populates this field.
18352049

2050+
##### [Java Native](#tab/java-native)
2051+
2052+
This field is automatically populated.
2053+
18362054
##### [Node.js](#tab/nodejs)
18372055

18382056
Use the add [custom property example](#add-a-custom-property-to-a-span), but replace the following lines of code:
@@ -1910,6 +2128,18 @@ Populate the `user ID` field in the `requests`, `dependencies`, or `exceptions`
19102128
Span.current().setAttribute("enduser.id", "myuser");
19112129
```
19122130

2131+
##### [Java Native](#tab/java-native)
2132+
2133+
Populate the `user ID` field in the `requests`, `dependencies`, or `exceptions` table.
2134+
2135+
Set `user_Id` in your code:
2136+
2137+
```java
2138+
import io.opentelemetry.api.trace.Span;
2139+
2140+
Span.current().setAttribute("enduser.id", "myuser");
2141+
```
2142+
19132143
#### [Node.js](#tab/nodejs)
19142144

19152145
Use the add [custom property example](#add-a-custom-property-to-a-span), but replace the following lines of code:
@@ -1960,6 +2190,11 @@ Logback, Log4j, and java.util.logging are [autoinstrumented](#logs). Attaching c
19602190
* [Log4j 2.0 Thread Context](https://logging.apache.org/log4j/2.x/manual/thread-context.html)
19612191
* [Log4j 1.2 MDC](https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/MDC.html)
19622192

2193+
2194+
#### [Java Native](#tab/java-native)
2195+
2196+
For Spring Boot native applications, Logback is instrumented out of the box.
2197+
19632198
#### [Node.js](#tab/nodejs)
19642199

19652200
```typescript
@@ -2095,6 +2330,10 @@ You might use the following ways to filter out telemetry before it leaves your a
20952330

20962331
See [sampling overrides](java-standalone-config.md#sampling-overrides) and [telemetry processors](java-standalone-telemetry-processors.md).
20972332

2333+
### [Java Native](#tab/java-native)
2334+
2335+
It's not possible to filter telemetry in Java Native.
2336+
20982337
### [Node.js](#tab/nodejs)
20992338

21002339
1. Exclude the URL option provided by many HTTP instrumentation libraries.
@@ -2313,6 +2552,18 @@ You can use `opentelemetry-api` to get the trace ID or span ID.
23132552
String spanId = span.getSpanContext().getSpanId();
23142553
```
23152554

2555+
### [Java Native](#tab/java-native)
2556+
2557+
Get the request trace ID and the span ID in your code:
2558+
2559+
```java
2560+
import io.opentelemetry.api.trace.Span;
2561+
2562+
Span span = Span.current();
2563+
String traceId = span.getSpanContext().getTraceId();
2564+
String spanId = span.getSpanContext().getSpanId();
2565+
```
2566+
23162567
### [Node.js](#tab/nodejs)
23172568

23182569
Get the request trace ID and the span ID in your code:
@@ -2369,6 +2620,13 @@ span_id = trace.get_current_span().get_span_context().span_id
23692620
- To enable usage experiences, see [Enable web or browser user monitoring](javascript.md).
23702621
- See the [release notes](https://github.com/microsoft/ApplicationInsights-Java/releases) on GitHub.
23712622
2623+
### [Java Native](#tab/java-native)
2624+
- For details on adding and modifying Azure Monitor OpenTelemetry, see [Add and modify Azure Monitor OpenTelemetry](opentelemetry-add-modify.md).
2625+
- To review the source code, see [Azure Monitor OpenTelemetry Distro in Spring Boot native image Java application](https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/spring/spring-cloud-azure-starter-monitor)
2626+
and [Quarkus OpenTelemetry Exporter for Azure](https://github.com/quarkiverse/quarkus-opentelemetry-exporter/tree/main/quarkus-opentelemetry-exporter-azure).
2627+
- To learn more about OpenTelemetry and its community, see the [OpenTelemetry Java GitHub repository](https://github.com/open-telemetry/opentelemetry-java-instrumentation).
2628+
- See the [release notes](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/spring/spring-cloud-azure-starter-monitor/CHANGELOG.md) on GitHub.
2629+
23722630
### [Node.js](#tab/nodejs)
23732631

23742632
- To review the source code, see the [Azure Monitor OpenTelemetry GitHub repository](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/monitor/monitor-opentelemetry).

0 commit comments

Comments
 (0)