Skip to content

Commit f2627ff

Browse files
fix/spans mapper for open telemetry
1 parent a34b2a8 commit f2627ff

4 files changed

Lines changed: 39 additions & 44 deletions

File tree

.idea/workspace.xml

Lines changed: 10 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

traceo-sdk-opentelemetry/src/main/java/com/traceo/sdk/OpenTelemetryMapper.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.opentelemetry.sdk.metrics.data.*;
66
import io.opentelemetry.sdk.trace.data.SpanData;
77

8+
import java.text.DecimalFormat;
89
import java.util.*;
910
import java.util.stream.Collectors;
1011

@@ -30,15 +31,8 @@ public static List<TraceoMetric> mapOpenTelemetryMetricToTraceo(Collection<Metri
3031

3132
for (MetricData metricData : metrics) {
3233
List<TraceoMetric> points = getPoints(metricData);
33-
34-
List<TraceoMetric> traceoMetrics = points.stream().map(traceoMetric -> new TraceoMetric(
35-
metricData.getName(),
36-
traceoMetric.getType(),
37-
traceoMetric.getUnixTimestamp(),
38-
traceoMetric.getValue()
39-
)).collect(Collectors.toList());
40-
41-
result.addAll(traceoMetrics);
34+
points.forEach((e) -> e.setName(metricData.getName()));
35+
result.addAll(points);
4236
}
4337

4438
return result;

traceo-sdk-opentelemetry/src/main/java/com/traceo/sdk/TraceoSpan.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.opentelemetry.sdk.trace.data.SpanData;
44

5+
import java.math.BigDecimal;
56
import java.util.ArrayList;
67
import java.util.HashMap;
78
import java.util.List;
@@ -25,9 +26,9 @@ public class TraceoSpan {
2526

2627
private String serviceName;
2728

28-
private long startEpochNanos;
29+
private BigDecimal startEpochNanos;
2930

30-
private long endEpochNanos;
31+
private BigDecimal endEpochNanos;
3132

3233
private Map<String, String> attributes = new HashMap<>();
3334

@@ -38,15 +39,26 @@ public TraceoSpan() {}
3839
public TraceoSpan(SpanData otelSpan) {
3940
this.spanId = otelSpan.getSpanId();
4041
this.traceId = otelSpan.getTraceId();
41-
this.parentSpanId = otelSpan.getParentSpanId();
42+
43+
if (!otelSpan.getParentSpanId().equals("0000000000000000")) {
44+
this.parentSpanId = otelSpan.getParentSpanId();
45+
}
46+
4247
this.name = otelSpan.getName();
4348
this.kind = otelSpan.getKind().name();
44-
this.startEpochNanos = otelSpan.getStartEpochNanos();
45-
this.endEpochNanos = otelSpan.getEndEpochNanos();
49+
this.startEpochNanos = parseLongToBigDecimal(otelSpan.getStartEpochNanos());
50+
this.endEpochNanos = parseLongToBigDecimal(otelSpan.getEndEpochNanos());
4651
this.status = otelSpan.getStatus().getStatusCode().name();
4752
this.statusMessage = otelSpan.getStatus().getDescription();
4853
}
4954

55+
private BigDecimal parseLongToBigDecimal(long epochNanos) {
56+
BigDecimal originalValue = new BigDecimal(epochNanos);
57+
BigDecimal divisor = new BigDecimal("1e9");
58+
59+
return originalValue.divide(divisor);
60+
}
61+
5062
public String getName() {
5163
return name;
5264
}
@@ -111,19 +123,19 @@ public void setServiceName(String serviceName) {
111123
this.serviceName = serviceName;
112124
}
113125

114-
public long getStartEpochNanos() {
126+
public BigDecimal getStartEpochNanos() {
115127
return startEpochNanos;
116128
}
117129

118-
public void setStartEpochNanos(long startEpochNanos) {
130+
public void setStartEpochNanos(BigDecimal startEpochNanos) {
119131
this.startEpochNanos = startEpochNanos;
120132
}
121133

122-
public long getEndEpochNanos() {
134+
public BigDecimal getEndEpochNanos() {
123135
return endEpochNanos;
124136
}
125137

126-
public void setEndEpochNanos(long endEpochNanos) {
138+
public void setEndEpochNanos(BigDecimal endEpochNanos) {
127139
this.endEpochNanos = endEpochNanos;
128140
}
129141

traceo-sdk-opentelemetry/src/test/java/com/traceo/sdk/TraceoMetricsExporterTest.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class TraceoMetricsExporterTest extends TestCase {
3636
@Before
3737
public void setUp() {
3838
ClientOptions clientOptions = NoOpClientBuilder.standard()
39-
.withApiKey("xxx")
39+
.withApiKey("tr_5d1c3709-119f-4fb9-b930-3e5b3a88cac7")
4040
.withHost("https://webhook.site/488164a0-5467-4621-9212-2974cc3eebc7")
4141
.build();
4242

@@ -78,10 +78,10 @@ public void testOtelMetrics() {
7878
measurement.record(123);
7979
counter.add(1);
8080
counter.add(1);
81-
counter.add(1);
81+
counter.add(13);
8282

8383
try {
84-
Thread.sleep(2000); // Adjust the duration based on your exporter's flushing interval
84+
Thread.sleep(2000);
8585
} catch (InterruptedException e) {
8686
e.printStackTrace();
8787
}
@@ -92,46 +92,37 @@ public void testOtelSpans() throws InterruptedException {
9292
Span parentSpan = tracer.spanBuilder("parent-span").startSpan();
9393

9494
try {
95-
// Simulate some work in the parent span
9695
parentSpan
9796
.addEvent("Parent span event 1")
9897
.setAttribute("atrybut eventu", "hahahah");
9998

100-
// Start a child span within the parent span
10199
Span childSpan1 = tracer
102100
.spanBuilder("child-span-1")
103101
.addLink(parentSpan.getSpanContext())
104102
.setAttribute("jakiś atrybut", "co to za atrybut?")
105103
.startSpan();
106104

107105
try {
108-
// Simulate some work in the child span
109106
childSpan1.addEvent("Child span 1 event 1", Attributes.of(AttributeKey.stringKey("AttributeKey"), "stringKey"));
110107
} finally {
111-
// End the child span
112108
childSpan1.end();
113109
}
114110

115-
// Start another child span within the parent span
116111
Span childSpan2 = tracer
117112
.spanBuilder("child-span-2")
118113
.addLink(parentSpan.getSpanContext())
119114
.startSpan();
120115

121116
try {
122-
// Simulate some work in the child span
123117
childSpan2.addEvent("Child span 2 event 1");
124118
} finally {
125-
// End the child span
126119
childSpan2.end();
127120
}
128121
} finally {
129-
// End the parent span
130122
parentSpan.end();
131-
132-
Thread.sleep(5000); // Adjust the duration based on your exporter's flushing interval
133-
134123
}
124+
125+
Thread.sleep(5000);
135126
}
136127

137128
@After

0 commit comments

Comments
 (0)