Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support uploading traces to UI in OpenTelemetry format (OTLP JSON) #5155

Merged
merged 14 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions cmd/query/app/fixture/otlp2jaeger-in.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"resourceSpans":[
{
"resource":{
"attributes":[
{
"key":"service.name",
"value":{
"stringValue":"telemetrygen"
}
}
]
},
"scopeSpans":[
{
"scope":{
"name":"telemetrygen"
},
"spans":[
{
"traceId":"83a9efd15c1c98a977e0711cc93ee28b",
"spanId":"e127af99e3b3e074",
"parentSpanId":"909541b92cf05311",
"name":"okey-dokey-0",
"kind":2,
"startTimeUnixNano":"1706678909209712000",
"endTimeUnixNano":"1706678909209835000",
"attributes":[
{
"key":"net.peer.ip",
"value":{
"stringValue":"1.2.3.4"
}
},
{
"key":"peer.service",
"value":{
"stringValue":"telemetrygen-client"
}
}
],
"status":{

}
}
]
}
],
"schemaUrl":"https://opentelemetry.io/schemas/1.4.0"
}
]
}
59 changes: 59 additions & 0 deletions cmd/query/app/fixture/otlp2jaeger-out.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reformatted per cat file | jq .

"data": [
{
"traceID": "83a9efd15c1c98a977e0711cc93ee28b",
"spans": [
{
"traceID": "83a9efd15c1c98a977e0711cc93ee28b",
"spanID": "e127af99e3b3e074",
"operationName": "okey-dokey-0",
"references": [
{
"refType": "CHILD_OF",
"traceID": "83a9efd15c1c98a977e0711cc93ee28b",
"spanID": "909541b92cf05311"
}
],
"startTime": 1706678909209712,
"duration": 123,
"tags": [
{
"key": "otel.library.name",
"type": "string",
"value": "telemetrygen"
},
{
"key": "net.peer.ip",
"type": "string",
"value": "1.2.3.4"
},
{
"key": "peer.service",
"type": "string",
"value": "telemetrygen-client"
},
{
"key": "span.kind",
"type": "string",
"value": "server"
}
],
"logs": [],
"processID": "p1",
"warnings": null
}
],
"processes": {
"p1": {
"serviceName": "telemetrygen",
"tags": []
}
},
"warnings": null
}
],
"total": 0,
"limit": 0,
"offset": 0,
"errors": null
}
38 changes: 38 additions & 0 deletions cmd/query/app/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -128,6 +129,7 @@
aH.handleFunc(router, aH.getOperations, "/operations").Methods(http.MethodGet)
// TODO - remove this when UI catches up
aH.handleFunc(router, aH.getOperationsLegacy, "/services/{%s}/operations", serviceParam).Methods(http.MethodGet)
aH.handleFunc(router, aH.transformOTLP, "/transform").Methods(http.MethodPost)
aH.handleFunc(router, aH.dependencies, "/dependencies").Methods(http.MethodGet)
aH.handleFunc(router, aH.latencies, "/metrics/latencies").Methods(http.MethodGet)
aH.handleFunc(router, aH.calls, "/metrics/calls").Methods(http.MethodGet)
Expand Down Expand Up @@ -193,6 +195,42 @@
aH.writeJSON(w, r, &structuredRes)
}

func (aH *APIHandler) transformOTLP(w http.ResponseWriter, r *http.Request) {
NavinShrinivas marked this conversation as resolved.
Show resolved Hide resolved
body, err := io.ReadAll(r.Body)
if aH.handleError(w, err, http.StatusBadRequest) {
return
}

Check warning on line 202 in cmd/query/app/http_handler.go

View check run for this annotation

Codecov / codecov/patch

cmd/query/app/http_handler.go#L201-L202

Added lines #L201 - L202 were not covered by tests

var uiErrors []structuredError
batches, err := otlp2model(body)

if aH.handleError(w, err, http.StatusInternalServerError) {
return
}

traces, err := batchesToTraces(batches)
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved

if aH.handleError(w, err, http.StatusInternalServerError) {
return
}

Check warning on line 215 in cmd/query/app/http_handler.go

View check run for this annotation

Codecov / codecov/patch

cmd/query/app/http_handler.go#L214-L215

Added lines #L214 - L215 were not covered by tests

uiTraces := make([]*ui.Trace, len(traces))

for i, v := range traces {
uiTrace, uiErr := aH.convertModelToUI(v, false)
if uiErr != nil {
uiErrors = append(uiErrors, *uiErr)
}

Check warning on line 223 in cmd/query/app/http_handler.go

View check run for this annotation

Codecov / codecov/patch

cmd/query/app/http_handler.go#L222-L223

Added lines #L222 - L223 were not covered by tests
uiTraces[i] = uiTrace
}

structuredRes := structuredResponse{
Data: uiTraces,
Errors: uiErrors,
}
aH.writeJSON(w, r, structuredRes)
}

func (aH *APIHandler) getOperations(w http.ResponseWriter, r *http.Request) {
service := r.FormValue(serviceParam)
if service == "" {
Expand Down
41 changes: 41 additions & 0 deletions cmd/query/app/http_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"math"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"

Expand Down Expand Up @@ -270,6 +271,24 @@ func TestWriteJSON(t *testing.T) {
}
}

func readOTLPTraces(t *testing.T) (interface{}, error) {
dat, err := os.ReadFile("./fixture/otlp2jaeger-in.json")
require.NoError(t, err)
out := new(interface{})
err = json.Unmarshal(dat, out)
require.NoError(t, err)
return out, nil
}

func readJaegerTraces(t *testing.T) (interface{}, error) {
dat, err := os.ReadFile("./fixture/otlp2jaeger-out.json")
out := new(interface{})
require.NoError(t, err)
err = json.Unmarshal(dat, out)
require.NoError(t, err)
return out, nil
}

func TestGetTrace(t *testing.T) {
testCases := []struct {
suffix string
Expand Down Expand Up @@ -623,6 +642,28 @@ func TestGetOperationsLegacyStorageFailure(t *testing.T) {
require.Error(t, err)
}

func TestTransformOTLPSuccess(t *testing.T) {
withTestServer(func(ts *testServer) {
response := new(interface{})
request, err := readOTLPTraces(t)
require.NoError(t, err)
err = postJSON(ts.server.URL+"/api/transform", request, response)
require.NoError(t, err)
corectResponse, err := readJaegerTraces(t)
require.NoError(t, err)
assert.Equal(t, response, corectResponse)
}, querysvc.QueryServiceOptions{})
}

func TestTransformOTLPEmptyFailure(t *testing.T) {
NavinShrinivas marked this conversation as resolved.
Show resolved Hide resolved
withTestServer(func(ts *testServer) {
response := new(interface{})
request := ""
err := postJSON(ts.server.URL+"/api/transform", request, response)
require.Error(t, err)
NavinShrinivas marked this conversation as resolved.
Show resolved Hide resolved
}, querysvc.QueryServiceOptions{})
}

func TestGetMetricsSuccess(t *testing.T) {
mr := &metricsmocks.Reader{}
apiHandlerOptions := []HandlerOption{
Expand Down
60 changes: 60 additions & 0 deletions cmd/query/app/otlp_translator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) 2024 The Jaeger Authors.
//
// Licensed 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 app

import (
"fmt"

model2otel "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/jaeger"
"go.opentelemetry.io/collector/pdata/ptrace"

"github.com/jaegertracing/jaeger/model"
)

func otlp2model(otlpSpans []byte) ([]*model.Batch, error) {
ptraceUnmarshaler := ptrace.JSONUnmarshaler{}
otlpTraces, err := ptraceUnmarshaler.UnmarshalTraces(otlpSpans)
if err != nil {
return nil, fmt.Errorf("cannot unmarshal OTLP : %w", err)
}
jaegerBatches, err := model2otel.ProtoFromTraces(otlpTraces)
if err != nil {
return nil, fmt.Errorf("cannot transform OTLP to Jaeger: %w", err)
}

Check warning on line 35 in cmd/query/app/otlp_translator.go

View check run for this annotation

Codecov / codecov/patch

cmd/query/app/otlp_translator.go#L34-L35

Added lines #L34 - L35 were not covered by tests
return jaegerBatches, nil
}

func batchesToTraces(jaegerBatches []*model.Batch) ([]*model.Trace, error) {
var traces []*model.Trace
traceMap := make(map[model.TraceID]*model.Trace)
for _, batch := range jaegerBatches {
for _, span := range batch.Spans {
if span.Process == nil {
span.Process = batch.Process
}
trace, ok := traceMap[span.TraceID]
if !ok {
newtrace := model.Trace{
Spans: []*model.Span{span},
}
traceMap[span.TraceID] = &newtrace
traces = append(traces, &newtrace)
} else {
trace.Spans = append(trace.Spans, span)
}
}
}
return traces, nil
}
68 changes: 68 additions & 0 deletions cmd/query/app/otlp_translator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) 2024 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package app

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/jaegertracing/jaeger/model"
)

func TestBatchesToTraces(t *testing.T) {
b1 := &model.Batch{
Spans: []*model.Span{
{TraceID: model.NewTraceID(1, 2), SpanID: model.NewSpanID(1), OperationName: "x"},
{TraceID: model.NewTraceID(1, 3), SpanID: model.NewSpanID(2), OperationName: "y"},
},
Process: model.NewProcess("process1", model.KeyValues{}),
}

b2 := &model.Batch{
Spans: []*model.Span{
{TraceID: model.NewTraceID(1, 2), SpanID: model.NewSpanID(2), OperationName: "z"},
},
Process: model.NewProcess("process2", model.KeyValues{}),
}

mainBatch := []*model.Batch{b1, b2}

traces, err := batchesToTraces(mainBatch)
require.NoError(t, err)

s1 := []*model.Span{
{
TraceID: model.NewTraceID(1, 2),
SpanID: model.NewSpanID(1),
OperationName: "x",
Process: model.NewProcess("process1", model.KeyValues{}),
},
{
TraceID: model.NewTraceID(1, 2),
SpanID: model.NewSpanID(2),
OperationName: "z",
Process: model.NewProcess("process2", model.KeyValues{}),
},
}

s2 := []*model.Span{
{
TraceID: model.NewTraceID(1, 3),
SpanID: model.NewSpanID(2),
OperationName: "y",
Process: model.NewProcess("process1", model.KeyValues{}),
},
}

t1 := model.Trace{
Spans: s1,
}
t2 := model.Trace{
Spans: s2,
}
mainTrace := []*model.Trace{&t1, &t2}
assert.Equal(t, mainTrace, traces)
}
Loading