1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-02 08:52:21 +02:00

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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 104 additions and 114 deletions

59
api/event/event.go Normal file
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...)
}

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,
})
}

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

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) {

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

View File

@ -17,12 +17,12 @@ package main
import (
"context"
"github.com/open-telemetry/opentelemetry-go/api/log"
"github.com/open-telemetry/opentelemetry-go/api/metric"
"github.com/open-telemetry/opentelemetry-go/api/stats"
"github.com/open-telemetry/opentelemetry-go/api/tag"
"github.com/open-telemetry/opentelemetry-go/api/trace"
"github.com/open-telemetry/opentelemetry-go/api/event"
"github.com/open-telemetry/opentelemetry-go/exporter/loader"
)
@ -64,7 +64,7 @@ func main() {
trace.SetError(ctx, true)
log.Log(ctx, "Nice operation!", tag.New("bogons").Int(100))
trace.Active(ctx).AddEvent(ctx, event.WithAttr("Nice operation!", tag.New("bogons").Int(100)))
trace.Active(ctx).SetAttributes(anotherKey.String("yes"))
@ -76,7 +76,7 @@ func main() {
func(ctx context.Context) error {
trace.Active(ctx).SetAttribute(lemonsKey.String("five"))
log.Logf(ctx, "Format schmormat %d!", 100)
trace.Active(ctx).AddEvent(ctx, event.WithString("Format schmormat %d!", 100))
stats.Record(ctx, measureTwo.M(1.3))

View File

@ -19,11 +19,11 @@ import (
"net/http"
"github.com/open-telemetry/opentelemetry-go/api/core"
"github.com/open-telemetry/opentelemetry-go/api/log"
"github.com/open-telemetry/opentelemetry-go/api/tag"
"github.com/open-telemetry/opentelemetry-go/api/trace"
"github.com/open-telemetry/opentelemetry-go/plugin/httptrace"
"github.com/open-telemetry/opentelemetry-go/api/event"
_ "github.com/open-telemetry/opentelemetry-go/exporter/loader"
)
@ -50,7 +50,7 @@ func main() {
)
defer span.Finish()
log.Log(ctx, "handling this...")
span.AddEvent(ctx, event.WithString("handling this..."))
io.WriteString(w, "Hello, world!\n")
}

View File

@ -25,8 +25,8 @@ func _() {
_ = x[INVALID-0]
_ = x[START_SPAN-1]
_ = x[FINISH_SPAN-2]
_ = x[LOG_EVENT-3]
_ = x[LOGF_EVENT-4]
_ = x[ADD_EVENT-3]
_ = x[ADD_EVENTF-4]
_ = x[NEW_SCOPE-5]
_ = x[NEW_MEASURE-6]
_ = x[NEW_METRIC-7]

View File

@ -23,11 +23,13 @@ import (
"google.golang.org/grpc/codes"
"github.com/open-telemetry/opentelemetry-go/api/core"
"github.com/open-telemetry/opentelemetry-go/api/event"
)
type (
EventType int
// TODO: this Event is confusing with event.Event.
Event struct {
// Automatic fields
Sequence core.EventID // Auto-filled
@ -40,10 +42,10 @@ type (
// Arguments (type-specific)
Attribute core.KeyValue // SET_ATTRIBUTE
Attributes []core.KeyValue // SET_ATTRIBUTES, LOG_EVENT
Attributes []core.KeyValue // SET_ATTRIBUTES
Mutator core.Mutator // SET_ATTRIBUTE
Mutators []core.Mutator // SET_ATTRIBUTES
Arguments []interface{} // LOGF_EVENT
Event event.Event // ADD_EVENT
Recovered interface{} // FINISH_SPAN
Status codes.Code // SET_STATUS
@ -68,8 +70,8 @@ const (
INVALID EventType = iota
START_SPAN
FINISH_SPAN
LOG_EVENT
LOGF_EVENT
ADD_EVENT
ADD_EVENTF
NEW_SCOPE
NEW_MEASURE
NEW_METRIC

View File

@ -64,11 +64,14 @@ func AppendEvent(buf *strings.Builder, data reader.Event) {
buf.WriteString(data.Duration.String())
buf.WriteString(")")
case reader.LOG_EVENT:
buf.WriteString(data.Message)
case reader.LOGF_EVENT:
buf.WriteString(data.Message)
case reader.ADD_EVENT:
buf.WriteString("event: ")
buf.WriteString(data.Event.Message())
buf.WriteString(" (")
for _, kv := range data.Event.Attributes() {
buf.WriteString(" " + kv.Key.Name() + "=" + kv.Value.Emit())
}
buf.WriteString(")")
case reader.MODIFY_ATTR:
buf.WriteString("modify attr")

View File

@ -22,6 +22,7 @@ import (
"google.golang.org/grpc/codes"
"github.com/open-telemetry/opentelemetry-go/api/core"
"github.com/open-telemetry/opentelemetry-go/api/event"
"github.com/open-telemetry/opentelemetry-go/api/metric"
"github.com/open-telemetry/opentelemetry-go/api/tag"
"github.com/open-telemetry/opentelemetry-go/api/trace"
@ -43,6 +44,7 @@ type (
SpanContext core.SpanContext
Tags tag.Map
Attributes tag.Map
Event event.Event
Stats []Measurement
Parent core.SpanContext
@ -106,8 +108,7 @@ const (
INVALID EventType = iota
START_SPAN
FINISH_SPAN
LOG_EVENT
LOGF_EVENT
ADD_EVENT
MODIFY_ATTR
RECORD_STATS
SET_STATUS
@ -260,10 +261,9 @@ func (ro *readerObserver) Observe(event observer.Event) {
ro.metrics.Store(event.Sequence, metric)
return
case observer.LOG_EVENT:
read.Type = LOG_EVENT
read.Message = event.String
case observer.ADD_EVENT:
read.Type = ADD_EVENT
read.Event = event.Event
attrs, span := ro.readScope(event.Scope)
read.Attributes = attrs.Apply(core.KeyValue{}, event.Attributes, core.Mutator{}, nil)
@ -271,17 +271,6 @@ func (ro *readerObserver) Observe(event observer.Event) {
read.SpanContext = span.spanContext
}
case observer.LOGF_EVENT:
// TODO: this can't be done lazily, must be done before Record()
read.Message = fmt.Sprintf(event.String, event.Arguments...)
read.Type = LOGF_EVENT
attrs, span := ro.readScope(event.Scope)
read.Attributes = attrs
if span != nil {
read.SpanContext = span.spanContext
}
case observer.RECORD_STATS:
read.Type = RECORD_STATS

View File

@ -22,6 +22,7 @@ import (
"strings"
"github.com/open-telemetry/opentelemetry-go/api/core"
"github.com/open-telemetry/opentelemetry-go/api/event"
"github.com/open-telemetry/opentelemetry-go/api/tag"
"github.com/open-telemetry/opentelemetry-go/api/trace"
)
@ -150,18 +151,18 @@ func (ct *clientTracer) wroteRequest(info httptrace.WroteRequestInfo) {
}
func (ct *clientTracer) got100Continue() {
ct.current().Log(ct.Context, "GOT 100 - Continue")
ct.current().AddEvent(ct.Context, event.WithString("GOT 100 - Continue"))
}
func (ct *clientTracer) wait100Continue() {
ct.current().Log(ct.Context, "GOT 100 - Wait")
ct.current().AddEvent(ct.Context, event.WithString("GOT 100 - Wait"))
}
func (ct *clientTracer) got1xxResponse(code int, header textproto.MIMEHeader) error {
ct.current().Log(ct.Context, "GOT 1xx",
ct.current().AddEvent(ct.Context, event.WithAttr("GOT 1xx",
HTTPStatus.Int(code),
HTTPHeaderMIME.String(sm2s(header)),
)
))
return nil
}