rename Log and Logf interface to AddEvent and AddEventf. (#22)

* rename Log and Logf interface to AddEvent and AddEventf.
- also remove log package.

* provide only AddEvent interface to Span
- Event can be created using Event specific interface.

* add var trick for Event interface.
This commit is contained in:
rghetia
2019-06-25 13:50:46 -07:00
committed by GitHub
parent abccacef85
commit d6d9786732
12 changed files with 104 additions and 114 deletions
+59
View File
@@ -0,0 +1,59 @@
// 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 event
import (
"fmt"
"github.com/open-telemetry/opentelemetry-go/api/core"
)
type (
event struct {
message string
attributes []core.KeyValue
}
// Event interface provides methods to retrieve Event properties.
Event interface {
// Message interface retrieves message string of the Event.
Message() string
// Attributes interface returns a copy of attributes associated with the Event.
Attributes() []core.KeyValue
}
)
var _ Event = (*event)(nil)
// WithAttr creates an Event with Attributes and a message.
// Attributes are immutable.
func WithAttr(msg string, attributes ...core.KeyValue) Event {
return event{message: msg, attributes: attributes}
}
// WithString creates an Event with formatted string.
func WithString(f string, args ...interface{}) Event {
return event{message: fmt.Sprint(f, args), attributes: nil}
}
func (e event) Message() string {
return e.message
}
func (e event) Attributes() []core.KeyValue {
return append(e.attributes[:0:0], e.attributes...)
}
-66
View File
@@ -1,66 +0,0 @@
// 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 log
import (
"context"
"github.com/open-telemetry/opentelemetry-go/api/core"
"github.com/open-telemetry/opentelemetry-go/api/scope"
"github.com/open-telemetry/opentelemetry-go/exporter/observer"
)
type (
Interface interface {
Log(ctx context.Context, msg string, fields ...core.KeyValue)
Logf(ctx context.Context, fmt string, args ...interface{})
}
Logger struct {
scope.Scope
}
)
func With(scope scope.Scope) Logger {
return Logger{scope}
}
func Log(ctx context.Context, msg string, fields ...core.KeyValue) {
With(scope.Active(ctx)).Log(ctx, msg, fields...)
}
func Logf(ctx context.Context, fmt string, args ...interface{}) {
With(scope.Active(ctx)).Logf(ctx, fmt, args...)
}
func (l Logger) Log(ctx context.Context, msg string, fields ...core.KeyValue) {
observer.Record(observer.Event{
Type: observer.LOG_EVENT,
Scope: l.ScopeID(),
String: msg,
Attributes: fields,
Context: ctx,
})
}
func (l Logger) Logf(ctx context.Context, fmt string, args ...interface{}) {
observer.Record(observer.Event{
Type: observer.LOGF_EVENT,
Scope: l.ScopeID(),
String: fmt,
Arguments: args,
Context: ctx,
})
}
+4 -3
View File
@@ -21,7 +21,7 @@ import (
"google.golang.org/grpc/codes"
"github.com/open-telemetry/opentelemetry-go/api/core"
"github.com/open-telemetry/opentelemetry-go/api/log"
"github.com/open-telemetry/opentelemetry-go/api/event"
"github.com/open-telemetry/opentelemetry-go/api/scope"
"github.com/open-telemetry/opentelemetry-go/api/stats"
"github.com/open-telemetry/opentelemetry-go/api/tag"
@@ -51,8 +51,6 @@ type (
Span interface {
scope.Mutable
log.Interface
stats.Interface
SetError(bool)
@@ -61,6 +59,9 @@ type (
Finish()
// AddEvent adds an event to the span.
AddEvent(ctx context.Context, event event.Event)
// IsRecordingEvents returns true is the span is active and recording events is enabled.
IsRecordingEvents() bool
+7 -6
View File
@@ -20,7 +20,7 @@ import (
"google.golang.org/grpc/codes"
"github.com/open-telemetry/opentelemetry-go/api/core"
"github.com/open-telemetry/opentelemetry-go/api/log"
"github.com/open-telemetry/opentelemetry-go/api/event"
"github.com/open-telemetry/opentelemetry-go/api/stats"
"github.com/open-telemetry/opentelemetry-go/exporter/observer"
)
@@ -167,12 +167,13 @@ func (sp *span) Tracer() Tracer {
return sp.tracer
}
func (sp *span) Log(ctx context.Context, msg string, args ...core.KeyValue) {
log.With(sp).Log(ctx, msg, args...)
}
func (sp *span) AddEvent(ctx context.Context, event event.Event) {
func (sp *span) Logf(ctx context.Context, fmt string, args ...interface{}) {
log.With(sp).Logf(ctx, fmt, args...)
observer.Record(observer.Event{
Type: observer.ADD_EVENT,
Event: event,
Context: ctx,
})
}
func (sp *span) Record(ctx context.Context, m ...core.Measurement) {
+2 -2
View File
@@ -23,7 +23,7 @@ import (
"google.golang.org/grpc/codes"
"github.com/open-telemetry/opentelemetry-go/api/core"
"github.com/open-telemetry/opentelemetry-go/api/log"
"github.com/open-telemetry/opentelemetry-go/api/event"
"github.com/open-telemetry/opentelemetry-go/api/scope"
"github.com/open-telemetry/opentelemetry-go/api/tag"
"github.com/open-telemetry/opentelemetry-go/exporter/observer"
@@ -90,7 +90,7 @@ func (t *tracer) WithSpan(ctx context.Context, name string, body func(context.Co
if err := body(ctx); err != nil {
span.SetAttribute(ErrorKey.Bool(true))
log.Log(ctx, "span error", MessageKey.String(err.Error()))
span.AddEvent(ctx, event.WithAttr("span error", MessageKey.String(err.Error())))
return err
}
return nil