From dc4a44ca4e03345ec7dad6ab097f172311d5ab87 Mon Sep 17 00:00:00 2001 From: thinkerou Date: Tue, 2 Jul 2019 03:17:50 +0800 Subject: [PATCH] split core.go (#39) --- api/core/core.go | 62 ++++------------------------------ api/core/span_context.go | 73 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 56 deletions(-) create mode 100644 api/core/span_context.go diff --git a/api/core/core.go b/api/core/core.go index 640aca2777d7..949a8e01d84c 100644 --- a/api/core/core.go +++ b/api/core/core.go @@ -23,19 +23,6 @@ import ( "github.com/open-telemetry/opentelemetry-go/api/unit" ) -type ScopeID struct { - EventID - SpanContext -} - -type SpanContext struct { - TraceIDHigh uint64 - TraceIDLow uint64 - SpanID uint64 -} - -type EventID uint64 - type BaseMeasure interface { Name() string Description() string @@ -51,14 +38,6 @@ type Measure interface { V(float64) KeyValue } -type Measurement struct { - // NOTE: If we add a ScopeID field this can carry - // pre-aggregated measures via the stats.Record API. - Measure Measure - Value float64 - ScopeID ScopeID -} - type Key interface { BaseMeasure @@ -132,31 +111,6 @@ const ( DELETE ) -var ( - // INVALID_SPAN_CONTEXT is meant for internal use to return invalid span context during error - // conditions. - INVALID_SPAN_CONTEXT = SpanContext{} -) - -func (sc SpanContext) HasTraceID() bool { - return sc.TraceIDHigh != 0 || sc.TraceIDLow != 0 -} - -func (sc SpanContext) HasSpanID() bool { - return sc.SpanID != 0 -} - -func (sc SpanContext) SpanIDString() string { - p := fmt.Sprintf("%.16x", sc.SpanID) - return p[0:3] + ".." + p[13:16] -} - -func (sc SpanContext) TraceIDString() string { - p1 := fmt.Sprintf("%.16x", sc.TraceIDHigh) - p2 := fmt.Sprintf("%.16x", sc.TraceIDLow) - return p1[0:3] + ".." + p2[13:16] -} - // TODO make this a lazy one-time conversion. func (v Value) Emit() string { switch v.Type { @@ -181,16 +135,12 @@ func (m Mutator) WithMaxHops(hops int) Mutator { return m } -func (e EventID) Scope() ScopeID { - return ScopeID{ - EventID: e, - } -} - -func (s SpanContext) Scope() ScopeID { - return ScopeID{ - SpanContext: s, - } +type Measurement struct { + // NOTE: If we add a ScopeID field this can carry + // pre-aggregated measures via the stats.Record API. + Measure Measure + Value float64 + ScopeID ScopeID } func (m Measurement) With(id ScopeID) Measurement { diff --git a/api/core/span_context.go b/api/core/span_context.go new file mode 100644 index 000000000000..1a3a7e6fb11e --- /dev/null +++ b/api/core/span_context.go @@ -0,0 +1,73 @@ +// Copyright 2019, OpenTelemetry 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 core + +import ( + "fmt" +) + +type EventID uint64 + +type TraceID struct { + IDHigh uint64 + IDLow uint64 +} + +type SpanContext struct { + TraceID + SpanID uint64 +} + +type ScopeID struct { + EventID + SpanContext +} + +func (e EventID) Scope() ScopeID { + return ScopeID{ + EventID: e, + } +} + +var ( + // INVALID_SPAN_CONTEXT is meant for internal use to return invalid span context during error + // conditions. + INVALID_SPAN_CONTEXT = SpanContext{} +) + +func (sc SpanContext) HasTraceID() bool { + return sc.TraceIDHigh != 0 || sc.TraceIDLow != 0 +} + +func (sc SpanContext) HasSpanID() bool { + return sc.SpanID != 0 +} + +func (sc SpanContext) SpanIDString() string { + p := fmt.Sprintf("%.16x", sc.SpanID) + return p[0:3] + ".." + p[13:16] +} + +func (sc SpanContext) TraceIDString() string { + p1 := fmt.Sprintf("%.16x", sc.TraceIDHigh) + p2 := fmt.Sprintf("%.16x", sc.TraceIDLow) + return p1[0:3] + ".." + p2[13:16] +} + +func (s SpanContext) Scope() ScopeID { + return ScopeID{ + SpanContext: s, + } +}