| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Package logtest is a testing helper package. |
| 5 |
package logtest_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"io" |
| 11 |
"os" |
| 12 |
|
| 13 |
logapi "go.opentelemetry.io/otel/log" |
| 14 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 15 |
"go.opentelemetry.io/otel/sdk/log" |
| 16 |
"go.opentelemetry.io/otel/sdk/log/logtest" |
| 17 |
) |
| 18 |
|
| 19 |
func ExampleRecordFactory() { |
| 20 |
exp := exporter{os.Stdout} |
| 21 |
rf := logtest.RecordFactory{ |
| 22 |
InstrumentationScope: &instrumentation.Scope{Name: "myapp"}, |
| 23 |
} |
| 24 |
|
| 25 |
rf.Body = logapi.StringValue("foo") |
| 26 |
r1 := rf.NewRecord() |
| 27 |
|
| 28 |
rf.Body = logapi.StringValue("bar") |
| 29 |
r2 := rf.NewRecord() |
| 30 |
|
| 31 |
_ = exp.Export(context.Background(), []log.Record{r1, r2}) |
| 32 |
|
| 33 |
// Output: |
| 34 |
// scope=myapp msg=foo |
| 35 |
// scope=myapp msg=bar |
| 36 |
} |
| 37 |
|
| 38 |
// Compile time check exporter implements log.Exporter. |
| 39 |
var _ log.Exporter = exporter{} |
| 40 |
|
| 41 |
type exporter struct{ io.Writer } |
| 42 |
|
| 43 |
func (e exporter) Export(_ context.Context, records []log.Record) error { |
| 44 |
for i, r := range records { |
| 45 |
if i != 0 { |
| 46 |
if _, err := e.Write([]byte("\n")); err != nil { |
| 47 |
return err |
| 48 |
} |
| 49 |
} |
| 50 |
if _, err := fmt.Fprintf(e, "scope=%s msg=%s", r.InstrumentationScope().Name, r.Body().String()); err != nil { |
| 51 |
return err |
| 52 |
} |
| 53 |
} |
| 54 |
return nil |
| 55 |
} |
| 56 |
|
| 57 |
func (exporter) Shutdown(context.Context) error { |
| 58 |
return nil |
| 59 |
} |
| 60 |
|
| 61 |
// appropriate error should be returned in these situations. |
| 62 |
func (exporter) ForceFlush(context.Context) error { |
| 63 |
return nil |
| 64 |
} |