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

Add endpoints to allow uploading OTLP traces #5138

Closed
Show file tree
Hide file tree
Changes from all 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
25 changes: 25 additions & 0 deletions cmd/query/app/apiv3/otlp_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ package apiv3
import (
"fmt"

"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
model2otel "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/jaeger"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/collector/pdata/ptrace/ptraceotlp"

"github.com/jaegertracing/jaeger/model"
Expand All @@ -45,3 +47,26 @@ func modelToOTLP(spans []*model.Span) ([]*tracev1.ResourceSpans, error) {
}
return chunk.ResourceSpans, nil
}

func OTLP2model(OTLPSpans []byte) ([]model.Trace, error) {
ptrace_unmarshaler := ptrace.JSONUnmarshaler{}
otlp_traces, err := ptrace_unmarshaler.UnmarshalTraces(OTLPSpans)
Comment on lines +52 to +53

Choose a reason for hiding this comment

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

Suggested change
ptrace_unmarshaler := ptrace.JSONUnmarshaler{}
otlp_traces, err := ptrace_unmarshaler.UnmarshalTraces(OTLPSpans)
ptraceUnmarshaler := ptrace.JSONUnmarshaler{}
otlpTraces, err := ptrace_unmarshaler.UnmarshalTraces(OTLPSpans)

same applies to jaeger_traces and jaeger_traces

context: https://go.dev/doc/effective_go#mixed-caps

if err != nil {
fmt.Println(err)
return nil, fmt.Errorf("cannot marshal OTLP : %w", err)
Comment on lines +55 to +56
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
fmt.Println(err)
return nil, fmt.Errorf("cannot marshal OTLP : %w", err)
return nil, fmt.Errorf("cannot unmarshal OTLP: %w", err)

}
batches, err := model2otel.ProtoFromTraces(otlp_traces)
fmt.Println(otlp_traces.ResourceSpans())
if err != nil {
fmt.Println(err)
return nil, fmt.Errorf("cannot marshal OTLP : %w", err)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return nil, fmt.Errorf("cannot marshal OTLP : %w", err)
return nil, fmt.Errorf("cannot transform OTLP to Jaeger: %w", err)

}
jaeger_traces := make([]model.Trace, len(batches) )
for _, v := range batches {
mar := jsonpb.Marshaler{}
fmt.Println(mar.MarshalToString(v))
jaeger_trace := v.ConvertToTraces()
jaeger_traces = append(jaeger_traces, *jaeger_trace)
}
return jaeger_traces, nil
}
36 changes: 36 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 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
Expand All @@ -31,6 +32,7 @@ import (
"go.opentelemetry.io/otel/propagation"
"go.uber.org/zap"

"github.com/jaegertracing/jaeger/cmd/query/app/apiv3"
"github.com/jaegertracing/jaeger/cmd/query/app/querysvc"
"github.com/jaegertracing/jaeger/model"
uiconv "github.com/jaegertracing/jaeger/model/converter/json"
Expand Down Expand Up @@ -121,6 +123,7 @@ func NewAPIHandler(queryService *querysvc.QueryService, tm *tenancy.Manager, opt
// RegisterRoutes registers routes for this handler on the given router
func (aH *APIHandler) RegisterRoutes(router *mux.Router) {
aH.handleFunc(router, aH.getTrace, "/traces/{%s}", traceIDParam).Methods(http.MethodGet)
aH.handleFunc(router, aH.transformOTLP, "/transform").Methods(http.MethodPost)
Copy link
Member

@yurishkuro yurishkuro Jan 23, 2024

Choose a reason for hiding this comment

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

Please move down, after aH.getOperationsLegacy

aH.handleFunc(router, aH.archiveTrace, "/archive/{%s}", traceIDParam).Methods(http.MethodPost)
aH.handleFunc(router, aH.search, "/traces").Methods(http.MethodGet)
aH.handleFunc(router, aH.getServices, "/services").Methods(http.MethodGet)
Expand Down Expand Up @@ -158,6 +161,39 @@ func (aH *APIHandler) formatRoute(route string, args ...interface{}) string {
return fmt.Sprintf("/%s"+route, args...)
}

func (aH *APIHandler) transformOTLP(w http.ResponseWriter, r *http.Request) {
Copy link
Member

Choose a reason for hiding this comment

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

please move the function to a place in the file so that it reflects its relative position in RegisterRoutes

body, err := ioutil.ReadAll(r.Body)
if aH.handleError(w, err, http.StatusBadRequest) {
return
}

if aH.handleError(w, err, http.StatusInternalServerError) {
return
}
Comment on lines +166 to +172
Copy link
Member

Choose a reason for hiding this comment

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

only one of these is needed

var uiErrors []structuredError
traces, err := apiv3.OTLP2model(body)

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

uiTraces := make([]*ui.Trace, len(traces))
for i, v := range traces {
uiTrace, uiErr := aH.convertModelToUI(&v, false)
if uiErr != nil {
uiErrors = append(uiErrors, *uiErr)
}
uiTraces[i] = uiTrace
}

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

}

func (aH *APIHandler) getServices(w http.ResponseWriter, r *http.Request) {
services, err := aH.queryService.GetServices(r.Context())
if aH.handleError(w, err, http.StatusInternalServerError) {
Expand Down
16 changes: 16 additions & 0 deletions model/model.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.