-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #162 from go-faster/feat/add-logger-provider
feat(app, autologs): add logger provider
- Loading branch information
Showing
9 changed files
with
328 additions
and
33 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
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,70 @@ | ||
package autologs | ||
|
||
import ( | ||
"context" | ||
"io" | ||
|
||
sdklog "go.opentelemetry.io/otel/sdk/log" | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
) | ||
|
||
// config contains configuration options for a LoggerProvider. | ||
type config struct { | ||
res *resource.Resource | ||
writer io.Writer | ||
lookup LookupExporter | ||
} | ||
|
||
// newConfig returns a config configured with options. | ||
func newConfig(options []Option) config { | ||
conf := config{res: resource.Default()} | ||
for _, o := range options { | ||
conf = o.apply(conf) | ||
} | ||
return conf | ||
} | ||
|
||
// Option applies a configuration option value to a LoggerProvider. | ||
type Option interface { | ||
apply(config) config | ||
} | ||
|
||
// optionFunc applies a set of options to a config. | ||
type optionFunc func(config) config | ||
|
||
// apply returns a config with option(s) applied. | ||
func (o optionFunc) apply(conf config) config { | ||
return o(conf) | ||
} | ||
|
||
// WithResource associates a Resource with a LoggerProvider. This Resource | ||
// represents the entity producing telemetry and is associated with all Meters | ||
// the LoggerProvider will create. | ||
// | ||
// By default, if this Option is not used, the default Resource from the | ||
// go.opentelemetry.io/otel/sdk/resource package will be used. | ||
func WithResource(res *resource.Resource) Option { | ||
return optionFunc(func(conf config) config { | ||
conf.res = res | ||
return conf | ||
}) | ||
} | ||
|
||
// WithWriter sets writer for the stderr, stdout exporters. | ||
func WithWriter(out io.Writer) Option { | ||
return optionFunc(func(conf config) config { | ||
conf.writer = out | ||
return conf | ||
}) | ||
} | ||
|
||
// LookupExporter creates exporter by name. | ||
type LookupExporter func(ctx context.Context, name string) (sdklog.Exporter, bool, error) | ||
|
||
// WithLookupExporter sets exporter lookup function. | ||
func WithLookupExporter(lookup LookupExporter) Option { | ||
return optionFunc(func(conf config) config { | ||
conf.lookup = lookup | ||
return conf | ||
}) | ||
} |
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,49 @@ | ||
package autologs | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/otel/exporters/stdout/stdoutlog" | ||
"go.opentelemetry.io/otel/sdk/log" | ||
) | ||
|
||
func TestWithLookupExporter(t *testing.T) { | ||
var lookup LookupExporter = func(ctx context.Context, name string) (log.Exporter, bool, error) { | ||
switch name { | ||
case "return_something": | ||
e, err := stdoutlog.New(stdoutlog.WithWriter(io.Discard)) | ||
return e, true, err | ||
case "return_error": | ||
return nil, false, errors.New("test error") | ||
default: | ||
return nil, false, nil | ||
} | ||
} | ||
|
||
for i, tt := range []struct { | ||
name string | ||
containsErr string | ||
}{ | ||
{"return_something", ``}, | ||
{"return_error", `test error`}, | ||
{"return_not_exist", `unsupported OTEL_LOGS_EXPORTER "return_not_exist"`}, | ||
} { | ||
tt := tt | ||
t.Run(fmt.Sprintf("Test%d", i+1), func(t *testing.T) { | ||
t.Setenv("OTEL_LOGS_EXPORTER", tt.name) | ||
ctx := context.Background() | ||
|
||
_, _, err := NewLoggerProvider(ctx, WithLookupExporter(lookup)) | ||
if tt.containsErr != "" { | ||
require.ErrorContains(t, err, tt.containsErr) | ||
return | ||
} | ||
require.NoError(t, err) | ||
}) | ||
} | ||
} |
Oops, something went wrong.