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

Begin prototyping Log API/SDK #3606

Closed
Closed
Show file tree
Hide file tree
Changes from 4 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
7 changes: 5 additions & 2 deletions example/namedtracer/foo/foo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/trace"
)

Expand All @@ -28,16 +29,18 @@ var (

// SubOperation is an example to demonstrate the use of named tracer.
// It creates a named tracer with its package path.
func SubOperation(ctx context.Context) error {
func SubOperation(ctx context.Context, logger log.Logger) error {
// Using global provider. Alternative is to have application provide a getter
// for its component to get the instance of the provider.
tr := otel.Tracer("example/namedtracer/foo")

var span trace.Span
_, span = tr.Start(ctx, "Sub operation...")
ctx, span = tr.Start(ctx, "Sub operation...")
defer span.End()
span.SetAttributes(lemonsKey.String("five"))
span.AddEvent("Sub span event")

logger.Emit(ctx, log.WithAttributes(attribute.String("operation", "suboperation")))

return nil
}
13 changes: 9 additions & 4 deletions example/namedtracer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ module go.opentelemetry.io/otel/example/namedtracer

go 1.18

replace (
go.opentelemetry.io/otel => ../..
go.opentelemetry.io/otel/sdk => ../../sdk
)
replace go.opentelemetry.io/otel => ../..

require (
github.com/go-logr/stdr v1.2.2
Expand All @@ -17,9 +14,17 @@ require (

require (
github.com/go-logr/logr v1.2.3 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdoutlog v0.0.0-00010101000000-000000000000 // indirect
go.opentelemetry.io/otel/log v0.0.0-00010101000000-000000000000 // indirect
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect
)

replace go.opentelemetry.io/otel/trace => ../../trace

replace go.opentelemetry.io/otel/log => ../../log

replace go.opentelemetry.io/otel/sdk => ../../sdk

replace go.opentelemetry.io/otel/exporters/stdout/stdouttrace => ../../exporters/stdout/stdouttrace

replace go.opentelemetry.io/otel/exporters/stdout/stdoutlog => ../../exporters/stdout/stdoutlog
29 changes: 28 additions & 1 deletion example/namedtracer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import (
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/otel/example/namedtracer/foo"
"go.opentelemetry.io/otel/exporters/stdout/stdoutlog"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
otellog "go.opentelemetry.io/otel/log"
sdklog "go.opentelemetry.io/otel/sdk/log"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
)
Expand All @@ -37,6 +40,7 @@ var (
)

var tp *sdktrace.TracerProvider
var lp *sdklog.LoggerProvider

// initTracer creates and registers trace provider instance.
func initTracer() error {
Expand All @@ -53,6 +57,20 @@ func initTracer() error {
return nil
}

// initLogger creates and registers logger provider instance.
func initLogger() error {
exp, err := stdoutlog.New(stdoutlog.WithPrettyPrint())
if err != nil {
return fmt.Errorf("failed to initialize stdoutlog exporter: %w", err)
}
bsp := sdklog.NewBatchLogRecordProcessor(exp)
lp = sdklog.NewLoggerProvider(
sdklog.WithLogRecordProcessor(bsp),
)
//otel.SetTracerProvider(tp)
return nil
}

func main() {
// Set logging level to info to see SDK status messages
stdr.SetVerbosity(5)
Expand All @@ -61,11 +79,16 @@ func main() {
if err := initTracer(); err != nil {
log.Panic(err)
}
// initialize logger provider.
if err := initLogger(); err != nil {
log.Panic(err)
}

// Create a named tracer with package path as its name.
tracer := tp.Tracer("example/namedtracer/main")
ctx := context.Background()
defer func() { _ = tp.Shutdown(ctx) }()
defer func() { _ = lp.Shutdown(ctx) }()

m0, _ := baggage.NewMember(string(fooKey), "foo1")
m1, _ := baggage.NewMember(string(barKey), "bar1")
Expand All @@ -77,7 +100,11 @@ func main() {
defer span.End()
span.AddEvent("Nice operation!", trace.WithAttributes(attribute.Int("bogons", 100)))
span.SetAttributes(anotherKey.String("yes"))
if err := foo.SubOperation(ctx); err != nil {

logger := lp.Logger("example/namedtracer/main")
logger.Emit(ctx, otellog.WithAttributes(attribute.String("operation", "main")))

if err := foo.SubOperation(ctx, logger); err != nil {
panic(err)
}
}
96 changes: 96 additions & 0 deletions exporters/stdout/stdoutlog/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright The 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 stdoutlog // import "go.opentelemetry.io/otel/exporters/stdout/stdouttrace"

import (
"io"
"os"
)

var (
defaultWriter = os.Stdout
defaultPrettyPrint = false
defaultTimestamps = true
)

// config contains options for the STDOUT exporter.
type config struct {
// Writer is the destination. If not set, os.Stdout is used.
Writer io.Writer

// PrettyPrint will encode the output into readable JSON. Default is
// false.
PrettyPrint bool

// Timestamps specifies if timestamps should be printed. Default is
// true.
Timestamps bool
}

// newConfig creates a validated Config configured with options.
func newConfig(options ...Option) (config, error) {
cfg := config{
Writer: defaultWriter,
PrettyPrint: defaultPrettyPrint,
Timestamps: defaultTimestamps,
}
for _, opt := range options {
cfg = opt.apply(cfg)
}
return cfg, nil
}

// Option sets the value of an option for a Config.
type Option interface {
apply(config) config
}

// WithWriter sets the export stream destination.
func WithWriter(w io.Writer) Option {
return writerOption{w}
}

type writerOption struct {
W io.Writer
}

func (o writerOption) apply(cfg config) config {
cfg.Writer = o.W
return cfg
}

// WithPrettyPrint sets the export stream format to use JSON.
func WithPrettyPrint() Option {
return prettyPrintOption(true)
}

type prettyPrintOption bool

func (o prettyPrintOption) apply(cfg config) config {
cfg.PrettyPrint = bool(o)
return cfg
}

// WithoutTimestamps sets the export stream to not include timestamps.
func WithoutTimestamps() Option {
return timestampsOption(false)
}

type timestampsOption bool

func (o timestampsOption) apply(cfg config) config {
cfg.Timestamps = bool(o)
return cfg
}
17 changes: 17 additions & 0 deletions exporters/stdout/stdoutlog/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright The 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 stdouttrace contains an OpenTelemetry exporter for tracing
// telemetry to be written to an output destination as JSON.
package stdoutlog // import "go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
23 changes: 23 additions & 0 deletions exporters/stdout/stdoutlog/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module go.opentelemetry.io/otel/exporters/stdout/stdoutlog

go 1.18

replace (
go.opentelemetry.io/otel => ../../..
go.opentelemetry.io/otel/sdk => ../../../sdk
)

require go.opentelemetry.io/otel/sdk v1.11.2

require (
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
go.opentelemetry.io/otel v1.11.2 // indirect
go.opentelemetry.io/otel/log v0.0.0-00010101000000-000000000000 // indirect
go.opentelemetry.io/otel/trace v1.11.2 // indirect
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect
)

replace go.opentelemetry.io/otel/log => ../../../log

replace go.opentelemetry.io/otel/sdk/log => ../../../sdk/log
14 changes: 14 additions & 0 deletions exporters/stdout/stdoutlog/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
go.opentelemetry.io/otel/trace v1.11.2 h1:Xf7hWSF2Glv0DE3MH7fBHvtpSBsjcBUe5MYAmZM/+y0=
go.opentelemetry.io/otel/trace v1.11.2/go.mod h1:4N+yC7QEz7TTsG9BSRLNAa63eg5E06ObSbKPmxQ/pKA=
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 h1:h+EGohizhe9XlX18rfpa8k8RAc5XyaeamM+0VHRd4lc=
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
114 changes: 114 additions & 0 deletions exporters/stdout/stdoutlog/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright The 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 stdoutlog // import "go.opentelemetry.io/otel/exporters/stdout/stdoutlog"

import (
"context"
"encoding/json"
"sync"
"time"

"go.opentelemetry.io/otel/sdk/log"
"go.opentelemetry.io/otel/sdk/log/logtest"
)

var zeroTime time.Time

var _ log.LogRecordExporter = &Exporter{}

// New creates an Exporter with the passed options.
func New(options ...Option) (*Exporter, error) {
cfg, err := newConfig(options...)
if err != nil {
return nil, err
}

enc := json.NewEncoder(cfg.Writer)
if cfg.PrettyPrint {
enc.SetIndent("", "\t")
}

return &Exporter{
encoder: enc,
timestamps: cfg.Timestamps,
}, nil
}

// Exporter is an implementation of trace.SpanSyncer that writes spans to stdout.
type Exporter struct {
encoder *json.Encoder
encoderMu sync.Mutex
timestamps bool

stoppedMu sync.RWMutex
stopped bool
}

// ExportLogRecords writes spans in json format to stdout.
func (e *Exporter) ExportLogRecords(ctx context.Context, spans []log.ReadOnlyLogRecord) error {
e.stoppedMu.RLock()
stopped := e.stopped
e.stoppedMu.RUnlock()
if stopped {
return nil
}

if len(spans) == 0 {
return nil
}

stubs := logtest.LogRecordStubsFromReadOnlyLogRecords(spans)

e.encoderMu.Lock()
defer e.encoderMu.Unlock()
for i := range stubs {
stub := &stubs[i]
// Remove timestamps
if !e.timestamps {
stub.Timestamp = zeroTime
}

// Encode span stubs, one by one
if err := e.encoder.Encode(stub); err != nil {
return err
}
}
return nil
}

// Shutdown is called to stop the exporter, it preforms no action.
func (e *Exporter) Shutdown(ctx context.Context) error {
e.stoppedMu.Lock()
e.stopped = true
e.stoppedMu.Unlock()

select {
case <-ctx.Done():
return ctx.Err()
default:
}
return nil
}

// MarshalLog is the marshaling function used by the logging system to represent this exporter.
func (e *Exporter) MarshalLog() interface{} {
return struct {
Type string
WithTimestamps bool
}{
Type: "stdout",
WithTimestamps: e.timestamps,
}
}
Loading