-
Notifications
You must be signed in to change notification settings - Fork 0
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
Global initialization and resources proposal: demonstration #3
Changes from all commits
1ab9755
8c56569
14b3380
d3a4a9f
de03e71
a12eb0a
6212c3f
bb36339
688a12e
a3f44e5
734cafe
85d9006
6b5c992
62867aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,22 @@ | ||
// 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" | ||
"io" | ||
"unsafe" | ||
|
||
"go.opentelemetry.io/api/registry" | ||
|
@@ -18,6 +33,8 @@ type KeyValue struct { | |
|
||
type ValueType int | ||
|
||
type Encoder func(io.Writer) error | ||
|
||
type Value struct { | ||
Type ValueType | ||
Bool bool | ||
|
@@ -26,8 +43,8 @@ type Value struct { | |
Float64 float64 | ||
String string | ||
Bytes []byte | ||
|
||
// TODO Lazy value type? | ||
Struct interface{} | ||
Encoder Encoder | ||
} | ||
|
||
const ( | ||
|
@@ -41,6 +58,8 @@ const ( | |
FLOAT64 | ||
STRING | ||
BYTES | ||
STRUCT // Struct or Bytes, whichever is non-nil. | ||
ENCODER // Encoder or Bytes, whichever is non-nil. | ||
) | ||
|
||
func (k Key) Bool(v bool) KeyValue { | ||
|
@@ -147,11 +166,50 @@ func (k Key) Uint(v uint) KeyValue { | |
return k.Uint64(uint64(v)) | ||
} | ||
|
||
func (k Key) Struct(v interface{}) KeyValue { | ||
return KeyValue{ | ||
Key: k, | ||
Value: Value{ | ||
Type: STRUCT, | ||
Struct: v, | ||
}, | ||
} | ||
} | ||
|
||
func (k Key) Encode(v func(io.Writer) error) KeyValue { | ||
return KeyValue{ | ||
Key: k, | ||
Value: Value{ | ||
Type: ENCODER, | ||
Encoder: v, | ||
}, | ||
} | ||
} | ||
|
||
func (k Key) Defined() bool { | ||
return k.Variable.Defined() | ||
} | ||
|
||
// TODO make this a lazy one-time conversion. | ||
func (v Value) Evaluate() Value { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The issue of making a copy of the |
||
switch v.Type { | ||
case STRUCT: | ||
if v.Struct != nil { | ||
return Value{ | ||
Type: STRUCT, | ||
Bytes: encodeStruct(v.Struct), | ||
} | ||
} | ||
case ENCODER: | ||
if v.Encoder != nil { | ||
return Value{ | ||
Type: STRUCT, | ||
Bytes: applyEncoder(v.Encoder), | ||
} | ||
} | ||
} | ||
return v | ||
} | ||
|
||
func (v Value) Emit() string { | ||
switch v.Type { | ||
case BOOL: | ||
|
@@ -164,8 +222,10 @@ func (v Value) Emit() string { | |
return fmt.Sprint(v.Float64) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we only emit strings? |
||
case STRING: | ||
return v.String | ||
case BYTES: | ||
return string(v.Bytes) | ||
case BYTES, STRUCT, ENCODER: | ||
// Note: In case of a fully synchronous SDK, this call | ||
// could be the first to evaluate a struct/encoder value. | ||
return string(v.Evaluate().Bytes) | ||
} | ||
return "unknown" | ||
return "invalid" | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// 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 metric | ||
|
||
import ( | ||
"context" | ||
|
||
"go.opentelemetry.io/api/core" | ||
"go.opentelemetry.io/api/internal" | ||
"go.opentelemetry.io/api/metric" | ||
) | ||
|
||
// Meter returns the global Meter instance. Before opentelemetry.Init() is | ||
// called, this returns an "indirect" No-op implementation, that will be updated | ||
// once the SDK is initialized. | ||
func Meter() metric.Meter { | ||
if t := internal.GlobalMeter.Load(); t != nil { | ||
return t.(metric.Meter) | ||
} | ||
return metric.NoopMeter{} | ||
} | ||
|
||
// IndirectMeter implements Meter, allows callers of Meter() before Init() | ||
// to forward to the installed SDK. | ||
type IndirectMeter struct{} | ||
|
||
var globalIndirect metric.Meter = &IndirectMeter{} | ||
|
||
// GetFloat64Gauge implements Meter | ||
func (*IndirectMeter) GetFloat64Gauge( | ||
ctx context.Context, | ||
gauge *metric.Float64GaugeHandle, | ||
labels ...core.KeyValue, | ||
) metric.Float64Gauge { | ||
return Meter().GetFloat64Gauge(ctx, gauge, labels...) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package opentelemetry | ||
|
||
import ( | ||
"sync" | ||
|
||
"go.opentelemetry.io/api/internal" | ||
"go.opentelemetry.io/api/metric" | ||
"go.opentelemetry.io/api/trace" | ||
) | ||
|
||
var once sync.Once | ||
|
||
type SDK interface { | ||
trace.Tracer | ||
metric.Meter | ||
} | ||
|
||
func Init(sdk SDK) { | ||
once.Do(func() { | ||
internal.GlobalTracer.Store(sdk.(trace.Tracer)) | ||
internal.GlobalMeter.Store(sdk.(metric.Meter)) | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// 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 resource | ||
|
||
import ( | ||
"go.opentelemetry.io/api/core" | ||
"go.opentelemetry.io/api/key" | ||
"go.opentelemetry.io/api/tag" | ||
) | ||
|
||
var ( | ||
ComponentKey = key.New("component") | ||
ServiceKey = key.New("service") | ||
) | ||
|
||
type Map struct { | ||
labels tag.Map | ||
} | ||
|
||
func Service(name string) Map { | ||
return Map{tag.NewMap(tag.MapUpdate{SingleKV: ServiceKey.String(name)})} | ||
} | ||
|
||
func Component(name string) Map { | ||
return Map{tag.NewMap(tag.MapUpdate{SingleKV: ComponentKey.String(name)})} | ||
} | ||
|
||
func New(labels ...core.KeyValue) Map { | ||
return Map{tag.NewMap(tag.MapUpdate{MultiKV: labels})} | ||
} | ||
|
||
func (m Map) Foreach(kv func (core.KeyValue) bool) { | ||
m.labels.Foreach(kv) | ||
} | ||
|
||
func Merge(maps ...Map) Map { | ||
var all []core.KeyValue | ||
for _, m := range maps { | ||
m.labels.Foreach(func(kv core.KeyValue) bool { | ||
all = append(all, kv) | ||
return true | ||
}) | ||
} | ||
return Map{tag.NewMap(tag.MapUpdate{MultiKV: all})} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a type that we have in the OpenTelemetry specs.