Skip to content
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

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

Merged
merged 3 commits into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions api/event/event.go
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add that var trick that enforces this struct to implement the Event interface?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

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 changes: 0 additions & 66 deletions api/log/log.go

This file was deleted.

7 changes: 4 additions & 3 deletions api/trace/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -51,8 +51,6 @@ type (
Span interface {
scope.Mutable

log.Interface

stats.Interface

SetError(bool)
Expand All @@ -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

Expand Down
13 changes: 7 additions & 6 deletions api/trace/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions api/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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"))

Expand All @@ -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))

Expand Down
4 changes: 2 additions & 2 deletions example/http/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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")
}
Expand Down
4 changes: 2 additions & 2 deletions exporter/observer/eventtype_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions exporter/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down
13 changes: 8 additions & 5 deletions exporter/reader/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
23 changes: 6 additions & 17 deletions exporter/reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -43,6 +44,7 @@ type (
SpanContext core.SpanContext
Tags tag.Map
Attributes tag.Map
Event event.Event
Stats []Measurement

Parent core.SpanContext
Expand Down Expand Up @@ -106,8 +108,7 @@ const (
INVALID EventType = iota
START_SPAN
FINISH_SPAN
LOG_EVENT
LOGF_EVENT
ADD_EVENT
MODIFY_ATTR
RECORD_STATS
SET_STATUS
Expand Down Expand Up @@ -260,28 +261,16 @@ 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)
if span != nil {
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

Expand Down
Loading