From af41105030e5c1d3f669bdd2bf24d3b7274df639 Mon Sep 17 00:00:00 2001 From: Roni Riska Date: Tue, 13 Aug 2019 15:18:27 +0300 Subject: [PATCH] First version of the tracing library Supports only noop tracer. Change-Id: Ie5786036af4d44c2cc7906ee43e4a1672292fa1a Signed-off-by: Roni Riska --- LICENSES.txt | 47 +++++++++++++++++++++++++++++++++ README.md | 59 ++++++++++++++++++++++++++++++++++++++++++ go.mod | 8 ++++++ go.sum | 13 ++++++++++ pkg/tracelibgo/tracing.go | 34 ++++++++++++++++++++++++ pkg/tracelibgo/tracing_test.go | 32 +++++++++++++++++++++++ 6 files changed, 193 insertions(+) create mode 100644 LICENSES.txt create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 pkg/tracelibgo/tracing.go create mode 100644 pkg/tracelibgo/tracing_test.go diff --git a/LICENSES.txt b/LICENSES.txt new file mode 100644 index 0000000..fc2ed8e --- /dev/null +++ b/LICENSES.txt @@ -0,0 +1,47 @@ +Copyright (c) 2019 AT&T Intellectual Property. +Copyright (c) 2019 Nokia. + + +Unless otherwise specified, all software contained herein is licensed +under the Apache License, Version 2.0 (the "License"); +you may not use this software 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. + + + +Unless otherwise specified, all documentation contained herein is licensed +under the Creative Commons License, Attribution 4.0 Intl. (the "License"); +you may not use this documentation except in compliance with the License. +You may obtain a copy of the License at + +https://creativecommons.org/licenses/by/4.0/ + +Unless required by applicable law or agreed to in writing, documentation +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. + + + + +Unless otherwise specified, all data contained herein is licensed +under the CDLA-Permissive 1.0 License (the "License"); +you may not use this data except in compliance with the License. +You may obtain a copy of the License at + +https://cdla.io/permissive-1-0/ + +Unless required by applicable law or agreed to in writing, data +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e11effc --- /dev/null +++ b/README.md @@ -0,0 +1,59 @@ +# Tracing helper library + +The library creates a configured tracer instance. + +ToDO: configuration... + +## Usage + +Create a tracer instance and set it as a global tracer: + +```go +import ( + "github.com/opentracing/opentracing-go" + "gerrit.o-ran-sc.org/ric-plt/tracelibgo/pkg/tracelibgo" + ... +) + +tracer, closer := tracelibgo.CreateTracer("my-service-name") +defer closer.Close() +opentracing.SetGlobalTracer(tracer) +``` + +Serialize span context to a byte array that can be sent +to another component via some messaging. For example, using +the RMR library. The serialization uses JSON format. + +```go + carrier := make(map[string]string) + opentracing.GlobalTracer().Inject( + span.Context(), + opentracing.TextMap, + opentracing.TextMapCarrier(carrier)) + b, err := json.Marshal(carrier) // b is a []byte and contains serilized span context +``` + +Extract a span context from byte array and create a new child span from it. +The serialized span context is got, for example, from the RMR library. + +```go + var carrier map[string]string + err = json.Unmarshal(data, &carrier) // data is []byte containing serialized span context + if err != nil { + ... + } + context, err := opentracing.GlobalTracer().Extract(opentracing.TextMap, opentracing.TextMapCarrier(carrier)) + if err != nil { + ... + } + span := opentracing.GlobalTracer().StartSpan("go test span", opentracing.ChildOf(context)) +``` + +## Unit testing + + GO111MODULE=on go mod download + go test ./pkg/tracelibgo + +## License + +See [LICENSES.txt](LICENSES.txt) file. diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..bfd57df --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module gerrit.o-ran-sc.org/r/ric-plt/tracelibgo + +require ( + github.com/opentracing/opentracing-go v1.1.0 + github.com/stretchr/testify v1.3.0 + github.com/uber/jaeger-client-go v2.16.0+incompatible + github.com/uber/jaeger-lib v2.0.0+incompatible // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..f30cee3 --- /dev/null +++ b/go.sum @@ -0,0 +1,13 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU= +github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/uber/jaeger-client-go v2.16.0+incompatible h1:Q2Pp6v3QYiocMxomCaJuwQGFt7E53bPYqEgug/AoBtY= +github.com/uber/jaeger-client-go v2.16.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk= +github.com/uber/jaeger-lib v2.0.0+incompatible h1:iMSCV0rmXEogjNWPh2D0xk9YVKvrtGoHJNe9ebLu/pw= +github.com/uber/jaeger-lib v2.0.0+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U= diff --git a/pkg/tracelibgo/tracing.go b/pkg/tracelibgo/tracing.go new file mode 100644 index 0000000..df2f45c --- /dev/null +++ b/pkg/tracelibgo/tracing.go @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2019 AT&T Intellectual Property. + * Copyright (c) 2018-2019 Nokia. + * + * 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 tracelibgo implements a function to create a configured tracer instance +package tracelibgo + +import ( + "io" + + "github.com/opentracing/opentracing-go" + "github.com/uber/jaeger-client-go" +) + +// CreateTracer creates a tracer entry +func CreateTracer(name string) (opentracing.Tracer, io.Closer) { + tracer, closer := jaeger.NewTracer(name, + jaeger.NewConstSampler(false), + jaeger.NewNullReporter()) + return tracer, closer +} diff --git a/pkg/tracelibgo/tracing_test.go b/pkg/tracelibgo/tracing_test.go new file mode 100644 index 0000000..86a61ce --- /dev/null +++ b/pkg/tracelibgo/tracing_test.go @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2019 AT&T Intellectual Property. + * Copyright (c) 2018-2019 Nokia. + * + * 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 tracelibgo + +import ( + "testing" + "github.com/stretchr/testify/assert" +) + +func TestTracerCreate(t *testing.T) { + tracer, closer := CreateTracer("foo") + assert.NotNil(t, tracer) + assert.NotNil(t, closer) + err := closer.Close() + assert.Nil(t, err) +} + -- 2.16.6