-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into remove_context_check
- Loading branch information
Showing
11 changed files
with
459 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Package logtest is a testing helper package. | ||
package logtest // import "go.opentelemetry.io/otel/log/logtest" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package logtest // import "go.opentelemetry.io/otel/log/logtest" | ||
|
||
import ( | ||
"time" | ||
|
||
"go.opentelemetry.io/otel/log" | ||
) | ||
|
||
// RecordFactory is used to facilitate unit testing bridge implementations that | ||
// make use of a [go.opentelemetry.io/otel/log.Record] | ||
// | ||
// Do not use RecordFactory to create records in production code. | ||
type RecordFactory struct { | ||
Timestamp time.Time | ||
ObservedTimestamp time.Time | ||
Severity log.Severity | ||
SeverityText string | ||
Body log.Value | ||
Attributes []log.KeyValue | ||
} | ||
|
||
// NewRecord returns a log record. | ||
func (b RecordFactory) NewRecord() log.Record { | ||
var record log.Record | ||
record.SetTimestamp(b.Timestamp) | ||
record.SetObservedTimestamp(b.ObservedTimestamp) | ||
record.SetSeverity(b.Severity) | ||
record.SetSeverityText(b.SeverityText) | ||
record.SetBody(b.Body) | ||
record.AddAttributes(b.Attributes...) | ||
|
||
return record | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package logtest | ||
|
||
import ( | ||
"slices" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"go.opentelemetry.io/otel/log" | ||
) | ||
|
||
func TestRecordFactory(t *testing.T) { | ||
now := time.Now() | ||
observed := now.Add(time.Second) | ||
severity := log.SeverityDebug | ||
severityText := "DBG" | ||
body := log.StringValue("Message") | ||
attrs := []log.KeyValue{ | ||
log.Int("int", 1), | ||
log.String("str", "foo"), | ||
log.Float64("flt", 3.14), | ||
} | ||
|
||
got := RecordFactory{ | ||
Timestamp: now, | ||
ObservedTimestamp: observed, | ||
Severity: severity, | ||
SeverityText: severityText, | ||
Body: body, | ||
Attributes: attrs, | ||
}.NewRecord() | ||
|
||
assert.Equal(t, now, got.Timestamp()) | ||
assert.Equal(t, observed, got.ObservedTimestamp()) | ||
assert.Equal(t, severity, got.Severity()) | ||
assert.Equal(t, severityText, got.SeverityText()) | ||
assertBody(t, body, got) | ||
assertAttributes(t, attrs, got) | ||
} | ||
|
||
func TestRecordFactoryMultiple(t *testing.T) { | ||
now := time.Now() | ||
attrs := []log.KeyValue{ | ||
log.Int("int", 1), | ||
log.String("str", "foo"), | ||
log.Float64("flt", 3.14), | ||
} | ||
|
||
f := RecordFactory{ | ||
Timestamp: now, | ||
Attributes: attrs, | ||
} | ||
|
||
record1 := f.NewRecord() | ||
f.Attributes = append(f.Attributes, log.Bool("added", true)) | ||
|
||
record2 := f.NewRecord() | ||
assert.Equal(t, now, record2.Timestamp()) | ||
assertAttributes(t, append(attrs, log.Bool("added", true)), record2) | ||
|
||
// Previously returned record is unharmed by the builder changes. | ||
assert.Equal(t, now, record1.Timestamp()) | ||
assertAttributes(t, attrs, record1) | ||
} | ||
|
||
func assertBody(t *testing.T, want log.Value, r log.Record) { | ||
t.Helper() | ||
got := r.Body() | ||
if !got.Equal(want) { | ||
t.Errorf("Body value is not equal:\nwant: %v\ngot: %v", want, got) | ||
} | ||
} | ||
|
||
func assertAttributes(t *testing.T, want []log.KeyValue, r log.Record) { | ||
t.Helper() | ||
var got []log.KeyValue | ||
r.WalkAttributes(func(kv log.KeyValue) bool { | ||
got = append(got, kv) | ||
return true | ||
}) | ||
if !slices.EqualFunc(want, got, log.KeyValue.Equal) { | ||
t.Errorf("Attributes are not equal:\nwant: %v\ngot: %v", want, got) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Log Test SDK | ||
|
||
[![PkgGoDev](https://pkg.go.dev/badge/go.opentelemetry.io/otel/sdk/log/logtest)](https://pkg.go.dev/go.opentelemetry.io/otel/sdk/log/logtest) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Package logtest is a testing helper package. | ||
package logtest_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
logapi "go.opentelemetry.io/otel/log" | ||
"go.opentelemetry.io/otel/sdk/instrumentation" | ||
"go.opentelemetry.io/otel/sdk/log" | ||
"go.opentelemetry.io/otel/sdk/log/logtest" | ||
) | ||
|
||
func ExampleRecordFactory() { | ||
exp := exporter{os.Stdout} | ||
rf := logtest.RecordFactory{ | ||
InstrumentationScope: instrumentation.Scope{Name: "myapp"}, | ||
} | ||
|
||
rf.Body = logapi.StringValue("foo") | ||
r1 := rf.NewRecord() | ||
|
||
rf.Body = logapi.StringValue("bar") | ||
r2 := rf.NewRecord() | ||
|
||
_ = exp.Export(context.Background(), []log.Record{r1, r2}) | ||
|
||
// Output: | ||
// scope=myapp msg=foo | ||
// scope=myapp msg=bar | ||
} | ||
|
||
// Compile time check exporter implements log.Exporter. | ||
var _ log.Exporter = exporter{} | ||
|
||
type exporter struct{ io.Writer } | ||
|
||
func (e exporter) Export(ctx context.Context, records []log.Record) error { | ||
for i, r := range records { | ||
if i != 0 { | ||
if _, err := e.Write([]byte("\n")); err != nil { | ||
return err | ||
} | ||
} | ||
if _, err := fmt.Fprintf(e, "scope=%s msg=%s", r.InstrumentationScope().Name, r.Body().String()); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (e exporter) Shutdown(context.Context) error { | ||
return nil | ||
} | ||
|
||
// appropriate error should be returned in these situations. | ||
func (e exporter) ForceFlush(context.Context) error { | ||
return nil | ||
} |
Oops, something went wrong.