Skip to content

Commit bcd3d3d

Browse files
[Internal]Fixing test regressions introduced by recent PR adding new Otel attributes (Azure#45826)
* Fixing Otel attributes recently added * Update CHANGELOG.md * Update sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/TracerUnderTest.java Co-authored-by: Copilot <[email protected]> * Update sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/CosmosTracerTest.java Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 5791da2 commit bcd3d3d

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/CosmosTracerTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,26 @@ private void verifyOTelTracerAttributes(
14811481

14821482
assertThat(attributes.get("azure.cosmosdb.operation.request_charge")).isNotNull();
14831483
assertThat(attributes.get("azure.cosmosdb.operation.request_charge")).isInstanceOf(Double.class);
1484-
assertThat(attributes.get("azure.cosmosdb.operation.request_charge")).isEqualTo(Double.valueOf(ctx.getTotalRequestCharge()));
1484+
1485+
boolean isRequestChargeMismatch =
1486+
(double)attributes.get("azure.cosmosdb.operation.request_charge") != 0d
1487+
|| Double.valueOf(ctx.getTotalRequestCharge()) == 0d;
1488+
1489+
if (isRequestChargeMismatch) {
1490+
assertThat(attributes.get("azure.cosmosdb.operation.request_charge"))
1491+
.isEqualTo(Double.valueOf(ctx.getTotalRequestCharge()));
1492+
} else {
1493+
// in the tests we usually use Flux.blockFirst() - which also cancels the Flux.
1494+
// Flux cancellation is also evident as an OTel event (which would have 0 request charge)
1495+
// to validate that previously the correct event - with right RU charge - had been traced we use the
1496+
// last (most recently) collected sibling Spans for the assertion.
1497+
assertThat(mockTracer.getAllCollectedSiblingSpans()).isNotNull();
1498+
TracerUnderTest.SpanRecord[] siblingSpans = mockTracer.getAllCollectedSiblingSpans();
1499+
assertThat(siblingSpans).hasAtLeastOneElementOfType(TracerUnderTest.SpanRecord.class);
1500+
TracerUnderTest.SpanRecord lastSiblingSpan = siblingSpans[siblingSpans.length - 1];
1501+
assertThat(lastSiblingSpan.getAttributes().get("azure.cosmosdb.operation.request_charge"))
1502+
.isEqualTo(Double.valueOf(ctx.getTotalRequestCharge()));
1503+
}
14851504

14861505
assertThat(attributes.get("azure.cosmosdb.response.sub_status_code")).isNotNull();
14871506
assertThat(attributes.get("azure.cosmosdb.response.sub_status_code")).isInstanceOf(Integer.class);

sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/TracerUnderTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,18 @@ public synchronized void setAttribute(String key, String value, Context context)
139139
this.currentSpan.attributes.put(key, value);
140140
}
141141

142+
@Override
143+
public synchronized void setAttribute(String key, long value, Context context) {
144+
assertThat(this.currentSpan).isNotNull();
145+
this.currentSpan.attributes.put(key, value);
146+
}
147+
148+
@Override
149+
public synchronized void setAttribute(String key, Object value, Context context) {
150+
assertThat(this.currentSpan).isNotNull();
151+
this.currentSpan.attributes.put(key, value);
152+
}
153+
142154
@Override
143155
public synchronized void addEvent(String name, Map<String, Object> attributes, OffsetDateTime timestamp, Context context) {
144156
assertThat(this.currentSpan).isNotNull();
@@ -156,6 +168,10 @@ public SpanRecord getCurrentSpan() {
156168
return this.currentSpan;
157169
}
158170

171+
public SpanRecord[] getAllCollectedSiblingSpans() {
172+
return this.collectedSiblingSpans.toArray(new SpanRecord[0]);
173+
}
174+
159175
public Collection<EventRecord> getEventsOfAllCollectedSiblingSpans() {
160176
ArrayList<EventRecord> events = new ArrayList<>(this.currentSpan.events);
161177
LOGGER.info("getEventsOfAllCollectedSiblingSpans: {} {} - Siblings {}", this.currentSpan, this.currentSpan.events.size(), this.collectedSiblingSpans.size());

sdk/cosmos/azure-cosmos/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
### 4.72.0-beta.1 (Unreleased)
44

55
#### Features Added
6-
* Added `azure.cosmosdb.operation.request_charge` and `azure.cosmosdb.response.sub_status_code` trace attributes. - [PR 45753](https://github.com/Azure/azure-sdk-for-java/pull/45753)
6+
* Added `azure.cosmosdb.operation.request_charge` and `azure.cosmosdb.response.sub_status_code` trace attributes. - [PR 45753](https://github.com/Azure/azure-sdk-for-java/pull/45753) and [PR 45826](https://github.com/Azure/azure-sdk-for-java/pull/45826)
77

88
#### Breaking Changes
99

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/DiagnosticsProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1540,7 +1540,7 @@ public void endSpan(CosmosDiagnosticsContext cosmosCtx, Context context, boolean
15401540
context);
15411541
tracer.setAttribute(
15421542
"azure.cosmosdb.response.sub_status_code",
1543-
cosmosCtx.getSubStatusCode(),
1543+
(Integer)cosmosCtx.getSubStatusCode(),
15441544
context);
15451545
tracer.setAttribute(
15461546
"db.cosmosdb.request_charge",

0 commit comments

Comments
 (0)