Skip to content

Commit

Permalink
Add models for sort.go, span.go, spanref.go, time.go, trace.go, model…
Browse files Browse the repository at this point in the history
…_test.proto (#125)

## Which problem is this PR solving?
* Part of jaegertracing/jaeger#6571

## Description of the changes
- built on the prev [pr
#123](#123)
- Copy set of types from original `model/` -> `sort.go`, `spanref.go`,
`time.go`, `trace.go`

## How was this change tested?
- `go test ./model/v1`

## Checklist
- [x] I have read
https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md
- [x] I have signed all commits
- [x] I have added unit tests for the new functionality
- [x] I have run lint and test steps successfully
  - for lint: `golangci-lint run  ./model/v1/`
  - for tests: `go test ./model/v1`

---------

Signed-off-by: nabil salah <[email protected]>
Signed-off-by: Yuri Shkuro <[email protected]>
Co-authored-by: Yuri Shkuro <[email protected]>
Co-authored-by: Yuri Shkuro <[email protected]>
  • Loading branch information
3 people authored Jan 22, 2025
1 parent 66db30d commit 73f885e
Show file tree
Hide file tree
Showing 15 changed files with 1,327 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ test-ci:
go test -v -coverprofile=coverage.txt ./...

# proto target is used to generate source code that is released as part of this library
proto: proto-prepare proto-api-v2
proto: proto-prepare proto-api-v2 proto-prototest

# proto-all target is used to generate code for all languages as a validation step.
proto-all: proto-prepare-all proto-api-v2-all proto-api-v3-all
Expand All @@ -179,6 +179,10 @@ proto-prepare-all:
proto-prepare:
mkdir -p ${PROTO_GEN_GO_DIR}

.PHONY: proto-prototest
proto-prototest:
$(PROTOC) -Imodel/proto --go_out=$(PWD)/model/v1/ model/v1/prototest/model_test.proto

.PHONY: proto-api-v2
proto-api-v2:
mkdir -p ${PROTO_GEN_GO_DIR}/${API_V2_PATH}
Expand Down
77 changes: 77 additions & 0 deletions model/v1/ids_proto_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) 2019 The Jaeger Authors.
// Copyright (c) 2018 Uber Technologies, Inc.
// SPDX-License-Identifier: Apache-2.0

package model_test

import (
"bytes"
"testing"

"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/jaegertracing/jaeger-idl/model/v1"
"github.com/jaegertracing/jaeger-idl/model/v1/prototest"
)

// TraceID/SpanID fields are defined as bytes in proto, backed by custom types in Go.
// Unfortunately, that means they require manual implementations of proto & json serialization.
// To ensure that it's the same as the default protobuf serialization, file jaeger_test.proto
// contains a copy of SpanRef message without any gogo options. This test file is compiled with
// plain protoc -go_out (without gogo). This test performs proto and JSON marshaling/unmarshaling
// to ensure that the outputs of manual and standard serialization are identical.
func TestTraceSpanIDMarshalProto(t *testing.T) {
testCases := []struct {
name string
marshal func(proto.Message) ([]byte, error)
unmarshal func([]byte, proto.Message) error
expected string
}{
{
name: "protobuf",
marshal: proto.Marshal,
unmarshal: proto.Unmarshal,
},
{
name: "JSON",
marshal: func(m proto.Message) ([]byte, error) {
out := new(bytes.Buffer)
err := new(jsonpb.Marshaler).Marshal(out, m)
if err != nil {
return nil, err
}
return out.Bytes(), nil
},
unmarshal: func(in []byte, m proto.Message) error {
return jsonpb.Unmarshal(bytes.NewReader(in), m)
},
expected: `{"traceId":"AAAAAAAAAAIAAAAAAAAAAw==","spanId":"AAAAAAAAAAs="}`,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
ref1 := model.SpanRef{TraceID: model.NewTraceID(2, 3), SpanID: model.NewSpanID(11)}
ref2 := prototest.SpanRef{
// TODO: would be cool to fuzz that test
TraceId: []byte{0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3},
SpanId: []byte{0, 0, 0, 0, 0, 0, 0, 11},
}
d1, err := testCase.marshal(&ref1)
require.NoError(t, err)
d2, err := testCase.marshal(&ref2)
require.NoError(t, err)
assert.Equal(t, d2, d1)
if testCase.expected != "" {
assert.Equal(t, testCase.expected, string(d1))
}
// test unmarshal
var ref1u model.SpanRef
err = testCase.unmarshal(d2, &ref1u)
require.NoError(t, err)
assert.Equal(t, ref1, ref1u)
})
}
}
231 changes: 231 additions & 0 deletions model/v1/prototest/model_test.pb.go

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

30 changes: 30 additions & 0 deletions model/v1/prototest/model_test.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2018 Uber Technologies, Inc.
//
// 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.

syntax="proto3";

package prototest;

option go_package = "./prototest";

enum SpanRefType {
CHILD_OF = 0;
FOLLOWS_FROM = 1;
};

message SpanRef {
bytes trace_id = 1;
bytes span_id = 2;
SpanRefType ref_type = 3;
}
Loading

0 comments on commit 73f885e

Please sign in to comment.