Skip to content

Commit 5d21186

Browse files
committed
Add Java native to the add/modify page
1 parent ae7b8bc commit 5d21186

File tree

1 file changed

+249
-0
lines changed

1 file changed

+249
-0
lines changed

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

Lines changed: 249 additions & 0 deletions
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.
@@ -1726,6 +1924,18 @@ Adding one or more span attributes populates the `customDimensions` field in the
17261924
Span.current().setAttribute(attributeKey, "myvalue1");
17271925
```
17281926

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

17311941
```typescript
@@ -1833,6 +2043,10 @@ activity.SetTag("http.client_ip", "<IP Address>");
18332043

18342044
Java automatically populates this field.
18352045

2046+
##### [Java Native](#tab/java-native)
2047+
2048+
This field is automatically populated.
2049+
18362050
##### [Node.js](#tab/nodejs)
18372051

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

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

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

2189+
2190+
#### [Java Native](#tab/java-native)
2191+
2192+
For Spring Boot native applications, Logback is instrumented out of the box.
2193+
19632194
#### [Node.js](#tab/nodejs)
19642195

19652196
```typescript
@@ -2313,6 +2544,17 @@ You can use `opentelemetry-api` to get the trace ID or span ID.
23132544
String spanId = span.getSpanContext().getSpanId();
23142545
```
23152546

2547+
### [Java Native](#tab/java-native)
2548+
2549+
Get the request trace ID and the span ID in your code:
2550+
2551+
```java
2552+
import io.opentelemetry.api.trace.Span;
2553+
2554+
Span span = Span.current();
2555+
String traceId = span.getSpanContext().getTraceId();
2556+
String spanId = span.getSpanContext().getSpanId();
2557+
```
23162558
### [Node.js](#tab/nodejs)
23172559

23182560
Get the request trace ID and the span ID in your code:
@@ -2369,6 +2611,13 @@ span_id = trace.get_current_span().get_span_context().span_id
23692611
- To enable usage experiences, see [Enable web or browser user monitoring](javascript.md).
23702612
- See the [release notes](https://github.com/microsoft/ApplicationInsights-Java/releases) on GitHub.
23712613
2614+
### [Java Native](#tab/java-native)
2615+
- For details on adding and modifying Azure Monitor OpenTelemetry, see [Add and modify Azure Monitor OpenTelemetry](opentelemetry-add-modify.md).
2616+
- 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)
2617+
and [Quarkus OpenTelemetry Exporter for Azure](https://github.com/quarkiverse/quarkus-opentelemetry-exporter/tree/main/quarkus-opentelemetry-exporter-azure).
2618+
- To learn more about OpenTelemetry and its community, see the [OpenTelemetry Java GitHub repository](https://github.com/open-telemetry/opentelemetry-java-instrumentation).
2619+
- 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.
2620+
23722621
### [Node.js](#tab/nodejs)
23732622

23742623
- 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)