Skip to content

Commit 3acc56c

Browse files
authored
Add trace to response (#61)
1 parent b4d517e commit 3acc56c

File tree

7 files changed

+178
-3
lines changed

7 files changed

+178
-3
lines changed

src/main/java/org/apache/skywalking/banyandb/v1/client/MeasureQueryResponse.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ public class MeasureQueryResponse {
3131
@Getter
3232
private final List<DataPoint> dataPoints;
3333

34+
@Getter
35+
private final Trace trace;
36+
3437
MeasureQueryResponse(BanyandbMeasure.QueryResponse response) {
3538
final List<BanyandbMeasure.DataPoint> dataPointList = response.getDataPointsList();
3639
this.dataPoints = new ArrayList<>(dataPointList.size());
3740
for (final BanyandbMeasure.DataPoint dp : dataPointList) {
3841
dataPoints.add(DataPoint.create(dp));
3942
}
43+
this.trace = Trace.convertFromProto(response.getTrace());
4044
}
4145

4246
/**
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.banyandb.v1.client;
20+
21+
import com.google.protobuf.Timestamp;
22+
import lombok.AccessLevel;
23+
import lombok.Getter;
24+
import lombok.Setter;
25+
26+
import java.util.List;
27+
import java.util.stream.Collectors;
28+
29+
/**
30+
* Span represents the span of a {@link Trace}.
31+
*/
32+
@Getter
33+
@Setter(value = AccessLevel.PRIVATE)
34+
public class Span {
35+
private Timestamp startTime;
36+
private Timestamp endTime;
37+
private boolean error;
38+
private List<Tag> tags;
39+
private String message;
40+
private List<Span> children;
41+
private long duration;
42+
43+
static Span convertSpanFromProto(org.apache.skywalking.banyandb.common.v1.BanyandbCommon.Span protoSpan) {
44+
Span spanBean = new Span();
45+
spanBean.setStartTime(protoSpan.getStartTime());
46+
spanBean.setEndTime(protoSpan.getEndTime());
47+
spanBean.setError(protoSpan.getError());
48+
spanBean.setMessage(protoSpan.getMessage());
49+
spanBean.setDuration(protoSpan.getDuration());
50+
spanBean.setTags(protoSpan.getTagsList().stream()
51+
.map(Tag::convertTagFromProto)
52+
.collect(Collectors.toList()));
53+
spanBean.setChildren(protoSpan.getChildrenList().stream()
54+
.map(Span::convertSpanFromProto)
55+
.collect(Collectors.toList()));
56+
return spanBean;
57+
}
58+
}

src/main/java/org/apache/skywalking/banyandb/v1/client/StreamQueryResponse.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,27 @@
1818

1919
package org.apache.skywalking.banyandb.v1.client;
2020

21-
import java.util.ArrayList;
22-
import java.util.List;
23-
2421
import lombok.Getter;
2522
import org.apache.skywalking.banyandb.stream.v1.BanyandbStream;
2623

24+
import java.util.ArrayList;
25+
import java.util.List;
26+
2727
/**
2828
* StreamQueryResponse represents the stream query result.
2929
*/
3030
public class StreamQueryResponse {
3131
@Getter
3232
private final List<Element> elements;
3333

34+
@Getter
35+
private final Trace trace;
36+
3437
StreamQueryResponse(BanyandbStream.QueryResponse response) {
3538
final List<BanyandbStream.Element> elementsList = response.getElementsList();
3639
elements = new ArrayList<>(elementsList.size());
3740
elementsList.forEach(element -> elements.add(Element.create(element)));
41+
this.trace = Trace.convertFromProto(response.getTrace());
3842
}
3943

4044
/**
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.banyandb.v1.client;
20+
21+
import lombok.AccessLevel;
22+
import lombok.Getter;
23+
import lombok.Setter;
24+
25+
26+
/**
27+
* Tag represents the key-value pair of a tag in a {@link Span}.
28+
*/
29+
@Getter
30+
@Setter(value = AccessLevel.PRIVATE)
31+
public class Tag {
32+
private String key;
33+
private String value;
34+
35+
static Tag convertTagFromProto(org.apache.skywalking.banyandb.common.v1.BanyandbCommon.Tag protoTag) {
36+
Tag tagBean = new Tag();
37+
tagBean.setKey(protoTag.getKey());
38+
tagBean.setValue(protoTag.getValue());
39+
return tagBean;
40+
}
41+
}
42+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.banyandb.v1.client;
20+
21+
import lombok.AccessLevel;
22+
import lombok.Getter;
23+
import lombok.Setter;
24+
25+
import java.util.List;
26+
import java.util.stream.Collectors;
27+
28+
/**
29+
* Trace represents the trace of a request.
30+
*/
31+
@Getter
32+
@Setter(value = AccessLevel.PRIVATE)
33+
public class Trace {
34+
private String traceId;
35+
private List<Span> spans;
36+
private boolean error;
37+
38+
static Trace convertFromProto(org.apache.skywalking.banyandb.common.v1.BanyandbCommon.Trace protoTrace) {
39+
Trace traceBean = new Trace();
40+
traceBean.setTraceId(protoTrace.getTraceId());
41+
traceBean.setError(protoTrace.getError());
42+
traceBean.setSpans(protoTrace.getSpansList().stream()
43+
.map(Span::convertSpanFromProto)
44+
.collect(Collectors.toList()));
45+
return traceBean;
46+
}
47+
}

src/test/java/org/apache/skywalking/banyandb/v1/client/BanyanDBClientMeasureQueryTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.common.collect.Lists;
2323
import com.google.protobuf.Timestamp;
2424
import io.grpc.stub.StreamObserver;
25+
import org.apache.skywalking.banyandb.common.v1.BanyandbCommon;
2526
import org.apache.skywalking.banyandb.measure.v1.BanyandbMeasure;
2627
import org.apache.skywalking.banyandb.measure.v1.MeasureServiceGrpc;
2728
import org.apache.skywalking.banyandb.model.v1.BanyandbModel;
@@ -124,6 +125,10 @@ public void testQuery_responseConversion() {
124125
final String entityIDValue = "entity_id_a";
125126
final Instant now = Instant.now();
126127
final BanyandbMeasure.QueryResponse responseObj = BanyandbMeasure.QueryResponse.newBuilder()
128+
.setTrace(BanyandbCommon.Trace.newBuilder().addSpans(BanyandbCommon.Span.newBuilder()
129+
.setMessage("test")
130+
.addTags(BanyandbCommon.Tag.newBuilder().setKey("b").setValue("a").build())
131+
.build()).build())
127132
.addDataPoints(BanyandbMeasure.DataPoint.newBuilder()
128133
.setTimestamp(Timestamp.newBuilder()
129134
.setSeconds(now.toEpochMilli() / 1000)
@@ -151,6 +156,11 @@ public void testQuery_responseConversion() {
151156
Assert.assertEquals(entityIDValue, resp.getDataPoints().get(0).getTagValue("entity_id"));
152157
Assert.assertEquals(10L,
153158
(Number) resp.getDataPoints().get(0).getFieldValue("total"));
159+
Assert.assertEquals(1, resp.getTrace().getSpans().size());
160+
Assert.assertEquals("test", resp.getTrace().getSpans().get(0).getMessage());
161+
Assert.assertEquals(1, resp.getTrace().getSpans().get(0).getTags().size());
162+
Assert.assertEquals("b", resp.getTrace().getSpans().get(0).getTags().get(0).getKey());
163+
Assert.assertEquals("a", resp.getTrace().getSpans().get(0).getTags().get(0).getValue());
154164
}
155165

156166
@Test

src/test/java/org/apache/skywalking/banyandb/v1/client/BanyanDBClientStreamQueryTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.protobuf.NullValue;
2525
import com.google.protobuf.Timestamp;
2626
import io.grpc.stub.StreamObserver;
27+
import org.apache.skywalking.banyandb.common.v1.BanyandbCommon;
2728
import org.apache.skywalking.banyandb.model.v1.BanyandbModel;
2829
import org.apache.skywalking.banyandb.stream.v1.BanyandbStream;
2930
import org.apache.skywalking.banyandb.stream.v1.StreamServiceGrpc;
@@ -318,6 +319,10 @@ public void testQuery_responseConversion() throws BanyanDBException {
318319
final long duration = 200L;
319320
final Instant now = Instant.now();
320321
final BanyandbStream.QueryResponse responseObj = BanyandbStream.QueryResponse.newBuilder()
322+
.setTrace(BanyandbCommon.Trace.newBuilder().addSpans(BanyandbCommon.Span.newBuilder()
323+
.setMessage("test")
324+
.addTags(BanyandbCommon.Tag.newBuilder().setKey("b").setValue("a").build())
325+
.build()).build())
321326
.addElements(BanyandbStream.Element.newBuilder()
322327
.setElementId(elementId)
323328
.setTimestamp(Timestamp.newBuilder()
@@ -362,6 +367,11 @@ public void testQuery_responseConversion() throws BanyanDBException {
362367
Assert.assertNull(resp.getElements().get(0).getTagValue("mq.broker"));
363368
Assert.assertArrayEquals(binaryData,
364369
resp.getElements().get(0).getTagValue("data_binary"));
370+
Assert.assertEquals(1, resp.getTrace().getSpans().size());
371+
Assert.assertEquals("test", resp.getTrace().getSpans().get(0).getMessage());
372+
Assert.assertEquals(1, resp.getTrace().getSpans().get(0).getTags().size());
373+
Assert.assertEquals("b", resp.getTrace().getSpans().get(0).getTags().get(0).getKey());
374+
Assert.assertEquals("a", resp.getTrace().getSpans().get(0).getTags().get(0).getValue());
365375
}
366376

367377
@Test

0 commit comments

Comments
 (0)