wankai123 commented on code in PR #13719:
URL: https://github.com/apache/skywalking/pull/13719#discussion_r2870798213


##########
oap-server/server-query-plugin/traceql-plugin/src/main/java/org/apache/skywalking/oap/query/traceql/handler/ZipkinTraceQLApiHandler.java:
##########
@@ -0,0 +1,381 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.oap.query.traceql.handler;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.linecorp.armeria.common.HttpData;
+import com.linecorp.armeria.common.HttpResponse;
+import com.linecorp.armeria.common.HttpStatus;
+import com.linecorp.armeria.common.MediaType;
+import com.linecorp.armeria.common.ResponseHeaders;
+import io.grafana.tempo.tempopb.TraceByIDResponse;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import org.apache.commons.codec.DecoderException;
+import org.apache.skywalking.oap.query.traceql.converter.ZipkinOTLPConverter;
+import org.apache.skywalking.oap.query.traceql.entity.OtlpTraceResponse;
+import org.apache.skywalking.oap.query.traceql.entity.SearchResponse;
+import org.apache.skywalking.oap.query.traceql.entity.TagNamesResponse;
+import org.apache.skywalking.oap.query.traceql.entity.TagNamesV2Response;
+import org.apache.skywalking.oap.query.traceql.entity.TagValuesResponse;
+import org.apache.skywalking.oap.query.traceql.parser.TraceQLQueryParams;
+import org.apache.skywalking.oap.query.traceql.parser.TraceQLQueryParser;
+import org.apache.skywalking.oap.query.zipkin.ZipkinQueryConfig;
+import org.apache.skywalking.oap.query.zipkin.handler.ZipkinQueryHandler;
+import org.apache.skywalking.oap.server.core.CoreModule;
+import org.apache.skywalking.oap.server.core.analysis.manual.searchtag.TagType;
+import org.apache.skywalking.oap.server.core.query.TagAutoCompleteQueryService;
+import org.apache.skywalking.oap.server.core.query.enumeration.Step;
+import org.apache.skywalking.oap.server.core.query.input.Duration;
+import org.apache.skywalking.oap.server.library.module.ModuleManager;
+import org.apache.skywalking.oap.server.library.util.CollectionUtils;
+import org.apache.skywalking.oap.server.library.util.StringUtil;
+import org.joda.time.DateTime;
+import zipkin2.Span;
+import zipkin2.storage.QueryRequest;
+
+public class ZipkinTraceQLApiHandler extends TraceQLApiHandler {
+    private final ZipkinQueryHandler zipkinQueryHandler;
+    private final ZipkinQueryConfig zipkinQueryConfig;
+    private final TagAutoCompleteQueryService tagAutoCompleteQueryService;
+
+    public ZipkinTraceQLApiHandler(ModuleManager moduleManager) {
+        super();
+        this.tagAutoCompleteQueryService = moduleManager.find(CoreModule.NAME)
+                                                        .provider()
+                                                        
.getService(TagAutoCompleteQueryService.class);
+        this.zipkinQueryConfig = new ZipkinQueryConfig();
+        this.zipkinQueryHandler = new ZipkinQueryHandler(zipkinQueryConfig, 
moduleManager);
+    }
+
+    @Override
+    protected HttpResponse queryTraceImpl(String traceId,
+                                          Optional<String> accept) throws 
IOException, DecoderException {
+        List<Span> zipkinTrace = zipkinQueryHandler.getTraceById(traceId);
+
+        // Step 1: Convert Zipkin spans to Protobuf (primary format) using 
converter
+        TraceByIDResponse protoResponse = 
ZipkinOTLPConverter.convertToProtobuf(zipkinTrace);
+
+        // Step 2: Return based on Accept header
+        if (accept.isPresent() && 
accept.get().contains("application/protobuf")) {
+            return buildProtobufHttpResponse(protoResponse);
+        } else {
+            // Convert protobuf to JSON for default response
+            return buildJsonHttpResponseFromProtobuf(protoResponse);
+        }
+    }
+
+    @Override
+    protected HttpResponse searchImpl(Optional<String> query,
+                                      Optional<String> tags,
+                                      Optional<String> minDuration,
+                                      Optional<String> maxDuration,
+                                      Optional<Integer> limit,
+                                      Optional<Long> start,
+                                      Optional<Long> end,
+                                      Optional<Integer> spss) throws 
IOException {
+        QueryRequest.Builder queryRequestBuilder = QueryRequest.newBuilder();
+
+        // Set end timestamp (convert from seconds to milliseconds)
+        long endTsMillis = end.isPresent() ? end.get() * 1000 : 
System.currentTimeMillis();
+        queryRequestBuilder.endTs(endTsMillis);
+
+        // Calculate lookback
+        long lookbackMillis;
+        if (start.isPresent()) {
+            long startTsMillis = start.get() * 1000;
+            lookbackMillis = endTsMillis - startTsMillis;
+        } else {
+            lookbackMillis = zipkinQueryConfig.getLookback();
+        }
+        queryRequestBuilder.lookback(lookbackMillis);
+
+        Duration duration = new Duration();
+        duration.setStep(Step.SECOND);
+        DateTime endTime = new DateTime(endTsMillis);
+        DateTime startTime = 
endTime.minus(org.joda.time.Duration.millis(lookbackMillis));
+        duration.setStart(startTime.toString("yyyy-MM-dd HHmmss"));
+        duration.setEnd(endTime.toString("yyyy-MM-dd HHmmss"));
+
+        if (query.isPresent() && !query.get().isEmpty()) {
+            TraceQLQueryParams traceQLParams = 
TraceQLQueryParser.extractParams(query.get());
+
+            // Apply TraceQL parameters
+            if (StringUtil.isNotBlank(traceQLParams.getServiceName())) {
+                
queryRequestBuilder.serviceName(traceQLParams.getServiceName());
+            }
+            if (StringUtil.isNotBlank(traceQLParams.getSpanName())) {
+                queryRequestBuilder.spanName(traceQLParams.getSpanName());
+            }
+
+            // Use duration from TraceQL
+            if (traceQLParams.getMinDuration() != null) {
+                
queryRequestBuilder.minDuration(traceQLParams.getMinDuration());
+            } else if (minDuration.isPresent()) {
+                
queryRequestBuilder.minDuration(parseDurationToMicros(minDuration.get()));
+            }
+
+            if (traceQLParams.getMaxDuration() != null) {
+                
queryRequestBuilder.maxDuration(traceQLParams.getMaxDuration());
+            } else if (maxDuration.isPresent()) {
+                
queryRequestBuilder.maxDuration(parseDurationToMicros(maxDuration.get()));

Review Comment:
   added args check in parseDurationToMicros



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to