1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-07-17 01:12:45 +02:00

Revert "Remove log interface from Span." (#21)

This reverts commit a551f0298c.
This commit is contained in:
rghetia
2019-06-24 10:35:16 -07:00
committed by GitHub
parent 73b844930c
commit abccacef85
7 changed files with 95 additions and 1 deletions

66
api/log/log.go Normal file
View File

@ -0,0 +1,66 @@
// 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,6 +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/scope"
"github.com/open-telemetry/opentelemetry-go/api/stats"
"github.com/open-telemetry/opentelemetry-go/api/tag"
@ -50,6 +51,8 @@ type (
Span interface {
scope.Mutable
log.Interface
stats.Interface
SetError(bool)

View File

@ -20,6 +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/stats"
"github.com/open-telemetry/opentelemetry-go/exporter/observer"
)
@ -166,6 +167,14 @@ 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) Logf(ctx context.Context, fmt string, args ...interface{}) {
log.With(sp).Logf(ctx, fmt, args...)
}
func (sp *span) Record(ctx context.Context, m ...core.Measurement) {
stats.With(sp).Record(ctx, m...)
}

View File

@ -23,6 +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/scope"
"github.com/open-telemetry/opentelemetry-go/api/tag"
"github.com/open-telemetry/opentelemetry-go/exporter/observer"
@ -89,6 +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()))
return err
}
return nil

View File

@ -17,6 +17,7 @@ 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"
@ -63,6 +64,8 @@ func main() {
trace.SetError(ctx, true)
log.Log(ctx, "Nice operation!", tag.New("bogons").Int(100))
trace.Active(ctx).SetAttributes(anotherKey.String("yes"))
gauge.Set(ctx, 1)
@ -73,6 +76,8 @@ func main() {
func(ctx context.Context) error {
trace.Active(ctx).SetAttribute(lemonsKey.String("five"))
log.Logf(ctx, "Format schmormat %d!", 100)
stats.Record(ctx, measureTwo.M(1.3))
return nil

View File

@ -19,6 +19,7 @@ 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"
@ -41,7 +42,7 @@ func main() {
req = req.WithContext(tag.WithMap(req.Context(), tag.NewMap(core.KeyValue{}, tags, core.Mutator{}, nil)))
_, span := tracer.Start(
ctx, span := tracer.Start(
req.Context(),
"hello",
trace.WithAttributes(attrs...),
@ -49,6 +50,8 @@ func main() {
)
defer span.Finish()
log.Log(ctx, "handling this...")
io.WriteString(w, "Hello, world!\n")
}

View File

@ -150,12 +150,18 @@ func (ct *clientTracer) wroteRequest(info httptrace.WroteRequestInfo) {
}
func (ct *clientTracer) got100Continue() {
ct.current().Log(ct.Context, "GOT 100 - Continue")
}
func (ct *clientTracer) wait100Continue() {
ct.current().Log(ct.Context, "GOT 100 - Wait")
}
func (ct *clientTracer) got1xxResponse(code int, header textproto.MIMEHeader) error {
ct.current().Log(ct.Context, "GOT 1xx",
HTTPStatus.Int(code),
HTTPHeaderMIME.String(sm2s(header)),
)
return nil
}