From e90c9407ec77c68fa5fcac8bfe022b2e8802c278 Mon Sep 17 00:00:00 2001 From: Dan Jaglowski Date: Tue, 8 Mar 2022 14:19:52 -0500 Subject: [PATCH] Change from logger_name to scope_name --- docs/types/logger_name.md | 64 ----------------- docs/types/scope_name.md | 64 +++++++++++++++++ entry/entry.go | 4 +- entry/entry_test.go | 10 +-- operator/helper/parser.go | 22 +++--- operator/helper/parser_test.go | 10 +-- .../helper/{logger_name.go => scope_name.go} | 14 ++-- ...logger_name_test.go => scope_name_test.go} | 70 +++++++++---------- .../testdata/loggername/parse_from.yaml | 1 - .../testdata/loggername/preserve_to.yaml | 2 - .../testdata/scope_name/parse_from.yaml | 1 + .../testdata/scope_name/preserve_to.yaml | 2 + operator/output/stdout/stdout_test.go | 2 +- operator/parser/json/config_test.go | 6 +- operator/parser/json/json_test.go | 4 +- .../parser/json/testdata/logger_name.yaml | 3 - operator/parser/json/testdata/scope_name.yaml | 3 + operator/parser/regex/config_test.go | 6 +- .../{logger_name.yaml => scope_name.yaml} | 4 +- .../logger_name.go => scope/scope_name.go} | 36 +++++----- .../scope_name_test.go} | 52 +++++++------- 21 files changed, 190 insertions(+), 190 deletions(-) delete mode 100644 docs/types/logger_name.md create mode 100644 docs/types/scope_name.md rename operator/helper/{logger_name.go => scope_name.go} (84%) rename operator/helper/{logger_name_test.go => scope_name_test.go} (73%) delete mode 100644 operator/helper/testdata/loggername/parse_from.yaml delete mode 100644 operator/helper/testdata/loggername/preserve_to.yaml create mode 100644 operator/helper/testdata/scope_name/parse_from.yaml create mode 100644 operator/helper/testdata/scope_name/preserve_to.yaml delete mode 100644 operator/parser/json/testdata/logger_name.yaml create mode 100644 operator/parser/json/testdata/scope_name.yaml rename operator/parser/regex/testdata/{logger_name.yaml => scope_name.yaml} (63%) rename operator/parser/{loggername/logger_name.go => scope/scope_name.go} (52%) rename operator/parser/{loggername/logger_name_test.go => scope/scope_name_test.go} (73%) diff --git a/docs/types/logger_name.md b/docs/types/logger_name.md deleted file mode 100644 index 9bcf7c78..00000000 --- a/docs/types/logger_name.md +++ /dev/null @@ -1,64 +0,0 @@ -## Logger Name Parsing - -A Logger Name may be parsed from a log entry in order to indicate the code from which a log was emitted. - -### `logger_name` parsing parameters - -Parser operators can parse a logger name and attach the resulting value to a log entry. - -| Field | Default | Description | -| --- | --- | --- | -| `parse_from` | required | The [field](/docs/types/field.md) from which the value will be parsed. | -| `preserve_to` | | Preserves the unparsed value at the specified [field](/docs/types/field.md). | - - -### How to use `logger_name` parsing - -All parser operators, such as [`regex_parser`](/docs/operators/regex_parser.md) support these fields inside of a `logger_name` block. - -If a `logger_name` block is specified, the parser operator will perform the parsing _after_ performing its other parsing actions, but _before_ passing the entry to the specified output operator. - - -### Example Configurations - -#### Parse a logger_name from a string - -Configuration: -```yaml -- type: regex_parser - regexp: '^(?P\S*)\s-\s(?P.*)' - logger_name: - parse_from: logger_name_field -``` - - - - - - - -
Input entry Output entry
- -```json -{ - "resource": { }, - "attributes": { }, - "body": "com.example.Foo - some message", - "logger_name": "", -} -``` - - - -```json -{ - "resource": { }, - "attributes": { }, - "body": { - "message": "some message", - }, - "logger_name": "com.example.Foo", -} -``` - -
diff --git a/docs/types/scope_name.md b/docs/types/scope_name.md new file mode 100644 index 00000000..7a71fac0 --- /dev/null +++ b/docs/types/scope_name.md @@ -0,0 +1,64 @@ +## Scope Name Parsing + +A Scope Name may be parsed from a log entry in order to indicate the code from which a log was emitted. + +### `scope_name` parsing parameters + +Parser operators can parse a scope name and attach the resulting value to a log entry. + +| Field | Default | Description | +| --- | --- | --- | +| `parse_from` | required | The [field](/docs/types/field.md) from which the value will be parsed. | +| `preserve_to` | | Preserves the unparsed value at the specified [field](/docs/types/field.md). | + + +### How to use `scope_name` parsing + +All parser operators, such as [`regex_parser`](/docs/operators/regex_parser.md) support these fields inside of a `scope_name` block. + +If a `scope_name` block is specified, the parser operator will perform the parsing _after_ performing its other parsing actions, but _before_ passing the entry to the specified output operator. + + +### Example Configurations + +#### Parse a scope_name from a string + +Configuration: +```yaml +- type: regex_parser + regexp: '^(?P\S*)\s-\s(?P.*)' + scope_name: + parse_from: body.scope_name_field +``` + + + + + + + +
Input entry Output entry
+ +```json +{ + "resource": { }, + "attributes": { }, + "body": "com.example.Foo - some message", + "scope_name": "", +} +``` + + + +```json +{ + "resource": { }, + "attributes": { }, + "body": { + "message": "some message", + }, + "scope_name": "com.example.Foo", +} +``` + +
diff --git a/entry/entry.go b/entry/entry.go index e41fb698..80749f20 100644 --- a/entry/entry.go +++ b/entry/entry.go @@ -32,7 +32,7 @@ type Entry struct { TraceId []byte `json:"trace_id,omitempty" yaml:"trace_id,omitempty"` TraceFlags []byte `json:"trace_flags,omitempty" yaml:"trace_flags,omitempty"` Severity Severity `json:"severity" yaml:"severity"` - LoggerName string `json:"logger_name" yaml:"logger_name"` + ScopeName string `json:"scope_name" yaml:"scope_name"` } // New will create a new log entry with current timestamp and an empty body. @@ -184,6 +184,6 @@ func (entry *Entry) Copy() *Entry { TraceId: copyByteArray(entry.TraceId), SpanId: copyByteArray(entry.SpanId), TraceFlags: copyByteArray(entry.TraceFlags), - LoggerName: entry.LoggerName, + ScopeName: entry.ScopeName, } } diff --git a/entry/entry_test.go b/entry/entry_test.go index ab8dea78..a3add889 100644 --- a/entry/entry_test.go +++ b/entry/entry_test.go @@ -143,7 +143,7 @@ func TestCopy(t *testing.T) { entry.TraceId = []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f} entry.SpanId = []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08} entry.TraceFlags = []byte{0x01} - entry.LoggerName = "my.logger" + entry.ScopeName = "my.logger" copy := entry.Copy() entry.Severity = Severity(1) @@ -155,7 +155,7 @@ func TestCopy(t *testing.T) { entry.TraceId[0] = 0xff entry.SpanId[0] = 0xff entry.TraceFlags[0] = 0xff - entry.LoggerName = "foo" + entry.ScopeName = "foo" require.Equal(t, time.Time{}, copy.Timestamp) require.Equal(t, Severity(0), copy.Severity) @@ -166,7 +166,7 @@ func TestCopy(t *testing.T) { require.Equal(t, []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, copy.TraceId) require.Equal(t, []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, copy.SpanId) require.Equal(t, []byte{0x01}, copy.TraceFlags) - require.Equal(t, "my.logger", copy.LoggerName) + require.Equal(t, "my.logger", copy.ScopeName) } func TestCopyNil(t *testing.T) { @@ -183,7 +183,7 @@ func TestCopyNil(t *testing.T) { entry.TraceId = []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f} entry.SpanId = []byte{0x04, 0x05, 0x06, 0x07, 0x08, 0x00, 0x01, 0x02, 0x03} entry.TraceFlags = []byte{0x01} - entry.LoggerName = "foo" + entry.ScopeName = "foo" require.Equal(t, time.Time{}, copy.Timestamp) require.Equal(t, Severity(0), copy.Severity) @@ -194,7 +194,7 @@ func TestCopyNil(t *testing.T) { require.Equal(t, []byte{}, copy.TraceId) require.Equal(t, []byte{}, copy.SpanId) require.Equal(t, []byte{}, copy.TraceFlags) - require.Equal(t, "", copy.LoggerName) + require.Equal(t, "", copy.ScopeName) } func TestFieldFromString(t *testing.T) { diff --git a/operator/helper/parser.go b/operator/helper/parser.go index 24477e9f..98dbbc01 100644 --- a/operator/helper/parser.go +++ b/operator/helper/parser.go @@ -43,7 +43,7 @@ type ParserConfig struct { TimeParser *TimeParser `mapstructure:"timestamp,omitempty" json:"timestamp,omitempty" yaml:"timestamp,omitempty"` SeverityParserConfig *SeverityParserConfig `mapstructure:"severity,omitempty" json:"severity,omitempty" yaml:"severity,omitempty"` TraceParser *TraceParser `mapstructure:"trace,omitempty" json:"trace,omitempty" yaml:"trace,omitempty"` - LoggerNameParser *LoggerNameParser `mapstructure:"logger_name,omitempty" json:"logger_name,omitempty" yaml:"logger_name,omitempty"` + ScopeNameParser *ScopeNameParser `mapstructure:"scope_name,omitempty" json:"scope_name,omitempty" yaml:"scope_name,omitempty"` } // Build will build a parser operator. @@ -88,13 +88,13 @@ func (c ParserConfig) Build(logger *zap.SugaredLogger) (ParserOperator, error) { // ParserOperator provides a basic implementation of a parser operator. type ParserOperator struct { TransformerOperator - ParseFrom entry.Field - ParseTo entry.Field - PreserveTo *entry.Field - TimeParser *TimeParser - SeverityParser *SeverityParser - TraceParser *TraceParser - LoggerNameParser *LoggerNameParser + ParseFrom entry.Field + ParseTo entry.Field + PreserveTo *entry.Field + TimeParser *TimeParser + SeverityParser *SeverityParser + TraceParser *TraceParser + ScopeNameParser *ScopeNameParser } // ProcessWith will run ParseWith on the entry, then forward the entry on to the next operators. @@ -172,8 +172,8 @@ func (p *ParserOperator) ParseWith(ctx context.Context, entry *entry.Entry, pars } var logernameParserErr error - if p.LoggerNameParser != nil { - logernameParserErr = p.LoggerNameParser.Parse(entry) + if p.ScopeNameParser != nil { + logernameParserErr = p.ScopeNameParser.Parse(entry) } // Handle time or severity parsing errors after attempting to parse both @@ -187,7 +187,7 @@ func (p *ParserOperator) ParseWith(ctx context.Context, entry *entry.Entry, pars return p.HandleEntryError(ctx, entry, errors.Wrap(traceParseErr, "trace parser")) } if logernameParserErr != nil { - return p.HandleEntryError(ctx, entry, errors.Wrap(logernameParserErr, "logger name parser")) + return p.HandleEntryError(ctx, entry, errors.Wrap(logernameParserErr, "scope_name parser")) } return nil } diff --git a/operator/helper/parser_test.go b/operator/helper/parser_test.go index 3dc71004..198f7a89 100644 --- a/operator/helper/parser_test.go +++ b/operator/helper/parser_test.go @@ -434,9 +434,9 @@ func NewTestParserConfig() ParserConfig { } expect.SeverityParserConfig = &sp - lnp := NewLoggerNameParser() + lnp := NewScopeNameParser() lnp.ParseFrom = entry.NewBodyField("logger") - expect.LoggerNameParser = &lnp + expect.ScopeNameParser = &lnp return expect } @@ -457,8 +457,8 @@ func TestMapStructureDecodeParserConfigWithHook(t *testing.T) { "warn": "4xx", }, }, - "logger_name": map[string]interface{}{ - "parse_from": "$body.logger", + "scope_name": map[string]interface{}{ + "parse_from": "body.logger", }, } @@ -488,7 +488,7 @@ func TestMapStructureDecodeParserConfig(t *testing.T) { "warn": "4xx", }, }, - "logger_name": map[string]interface{}{ + "scope_name": map[string]interface{}{ "parse_from": entry.NewBodyField("logger"), }, } diff --git a/operator/helper/logger_name.go b/operator/helper/scope_name.go similarity index 84% rename from operator/helper/logger_name.go rename to operator/helper/scope_name.go index a672a598..f766187c 100644 --- a/operator/helper/logger_name.go +++ b/operator/helper/scope_name.go @@ -19,19 +19,19 @@ import ( "github.com/open-telemetry/opentelemetry-log-collection/errors" ) -// LoggerNameParser is a helper that parses severity onto an entry. -type LoggerNameParser struct { +// ScopeNameParser is a helper that parses severity onto an entry. +type ScopeNameParser struct { ParseFrom entry.Field `mapstructure:"parse_from,omitempty" json:"parse_from,omitempty" yaml:"parse_from,omitempty"` PreserveTo *entry.Field `mapstructure:"preserve_to,omitempty" json:"preserve_to,omitempty" yaml:"preserve_to,omitempty"` } -// NewLoggerNameParser creates a new logger name parser with default values -func NewLoggerNameParser() LoggerNameParser { - return LoggerNameParser{} +// NewScopeNameParser creates a new scope parser with default values +func NewScopeNameParser() ScopeNameParser { + return ScopeNameParser{} } // Parse will parse severity from a field and attach it to the entry -func (p *LoggerNameParser) Parse(ent *entry.Entry) error { +func (p *ScopeNameParser) Parse(ent *entry.Entry) error { value, ok := ent.Delete(p.ParseFrom) if !ok { return errors.NewError( @@ -54,7 +54,7 @@ func (p *LoggerNameParser) Parse(ent *entry.Entry) error { ) } - ent.LoggerName = strVal + ent.ScopeName = strVal if p.PreserveTo != nil { if err := ent.Set(p.PreserveTo, value); err != nil { return errors.Wrap(err, "set preserve_to") diff --git a/operator/helper/logger_name_test.go b/operator/helper/scope_name_test.go similarity index 73% rename from operator/helper/logger_name_test.go rename to operator/helper/scope_name_test.go index d19334f6..130704b6 100644 --- a/operator/helper/logger_name_test.go +++ b/operator/helper/scope_name_test.go @@ -27,115 +27,115 @@ import ( "github.com/open-telemetry/opentelemetry-log-collection/entry" ) -const testLoggerName = "my.logger" +const testScopeName = "my.logger" -func TestLoggerNameParser(t *testing.T) { +func TestScopeNameParser(t *testing.T) { now := time.Now() testCases := []struct { name string - parser *LoggerNameParser + parser *ScopeNameParser input *entry.Entry expectErr bool expected *entry.Entry }{ { name: "root_string", - parser: &LoggerNameParser{ + parser: &ScopeNameParser{ ParseFrom: entry.NewBodyField(), }, input: func() *entry.Entry { e := entry.New() - e.Body = testLoggerName + e.Body = testScopeName e.Timestamp = now return e }(), expected: func() *entry.Entry { e := entry.New() - e.LoggerName = testLoggerName + e.ScopeName = testScopeName e.Timestamp = now return e }(), }, { name: "root_string_preserve", - parser: func() *LoggerNameParser { + parser: func() *ScopeNameParser { preserve := entry.NewBodyField() - return &LoggerNameParser{ + return &ScopeNameParser{ ParseFrom: entry.NewBodyField(), PreserveTo: &preserve, } }(), input: func() *entry.Entry { e := entry.New() - e.Body = testLoggerName + e.Body = testScopeName e.Timestamp = now return e }(), expected: func() *entry.Entry { e := entry.New() - e.Body = testLoggerName - e.LoggerName = testLoggerName + e.Body = testScopeName + e.ScopeName = testScopeName e.Timestamp = now return e }(), }, { name: "nondestructive_error", - parser: &LoggerNameParser{ + parser: &ScopeNameParser{ ParseFrom: entry.NewBodyField(), }, input: func() *entry.Entry { e := entry.New() - e.Body = map[string]interface{}{"logger": testLoggerName} + e.Body = map[string]interface{}{"logger": testScopeName} e.Timestamp = now return e }(), expectErr: true, expected: func() *entry.Entry { e := entry.New() - e.Body = map[string]interface{}{"logger": testLoggerName} + e.Body = map[string]interface{}{"logger": testScopeName} e.Timestamp = now return e }(), }, { name: "nonroot_string", - parser: &LoggerNameParser{ + parser: &ScopeNameParser{ ParseFrom: entry.NewBodyField("logger"), }, input: func() *entry.Entry { e := entry.New() - e.Body = map[string]interface{}{"logger": testLoggerName} + e.Body = map[string]interface{}{"logger": testScopeName} e.Timestamp = now return e }(), expected: func() *entry.Entry { e := entry.New() e.Body = map[string]interface{}{} - e.LoggerName = testLoggerName + e.ScopeName = testScopeName e.Timestamp = now return e }(), }, { name: "nonroot_string_preserve", - parser: func() *LoggerNameParser { + parser: func() *ScopeNameParser { preserve := entry.NewBodyField("somewhere") - return &LoggerNameParser{ + return &ScopeNameParser{ ParseFrom: entry.NewBodyField("logger"), PreserveTo: &preserve, } }(), input: func() *entry.Entry { e := entry.New() - e.Body = map[string]interface{}{"logger": testLoggerName} + e.Body = map[string]interface{}{"logger": testScopeName} e.Timestamp = now return e }(), expected: func() *entry.Entry { e := entry.New() - e.Body = map[string]interface{}{"somewhere": testLoggerName} - e.LoggerName = testLoggerName + e.Body = map[string]interface{}{"somewhere": testScopeName} + e.ScopeName = testScopeName e.Timestamp = now return e }(), @@ -155,17 +155,17 @@ func TestLoggerNameParser(t *testing.T) { } } -func TestGoldenLoggerNameParserConfig(t *testing.T) { +func TestGoldenScopeNameParserConfig(t *testing.T) { cases := []struct { name string expectErr bool - expect *LoggerNameParser + expect *ScopeNameParser }{ { "parse_from", false, - func() *LoggerNameParser { - cfg := NewLoggerNameParser() + func() *ScopeNameParser { + cfg := NewScopeNameParser() cfg.ParseFrom = entry.NewBodyField("from") return &cfg }(), @@ -173,8 +173,8 @@ func TestGoldenLoggerNameParserConfig(t *testing.T) { { "preserve_to", false, - func() *LoggerNameParser { - cfg := NewLoggerNameParser() + func() *ScopeNameParser { + cfg := NewScopeNameParser() cfg.ParseFrom = entry.NewBodyField("from") preserve := entry.NewBodyField("aField") cfg.PreserveTo = &preserve @@ -185,7 +185,7 @@ func TestGoldenLoggerNameParserConfig(t *testing.T) { for _, tc := range cases { t.Run("yaml/"+tc.name, func(t *testing.T) { - cfgFromYaml, yamlErr := loggerNameConfigFromFileViaYaml(path.Join(".", "testdata", "loggername", fmt.Sprintf("%s.yaml", tc.name))) + cfgFromYaml, yamlErr := ScopeConfigFromFileViaYaml(path.Join(".", "testdata", "scope_name", fmt.Sprintf("%s.yaml", tc.name))) if tc.expectErr { require.Error(t, yamlErr) } else { @@ -194,10 +194,10 @@ func TestGoldenLoggerNameParserConfig(t *testing.T) { } }) t.Run("mapstructure/"+tc.name, func(t *testing.T) { - parser := NewLoggerNameParser() + parser := NewScopeNameParser() cfgFromMapstructure := &parser - mapErr := loggerNameConfigFromFileViaMapstructure( - path.Join(".", "testdata", "loggername", fmt.Sprintf("%s.yaml", tc.name)), + mapErr := ScopeConfigFromFileViaMapstructure( + path.Join(".", "testdata", "scope_name", fmt.Sprintf("%s.yaml", tc.name)), cfgFromMapstructure, ) if tc.expectErr { @@ -210,13 +210,13 @@ func TestGoldenLoggerNameParserConfig(t *testing.T) { } } -func loggerNameConfigFromFileViaYaml(file string) (*LoggerNameParser, error) { +func ScopeConfigFromFileViaYaml(file string) (*ScopeNameParser, error) { bytes, err := ioutil.ReadFile(file) if err != nil { return nil, fmt.Errorf("could not find config file: %s", err) } - parser := NewLoggerNameParser() + parser := NewScopeNameParser() config := &parser if err := yaml.Unmarshal(bytes, config); err != nil { return nil, fmt.Errorf("failed to read config file as yaml: %s", err) @@ -225,7 +225,7 @@ func loggerNameConfigFromFileViaYaml(file string) (*LoggerNameParser, error) { return config, nil } -func loggerNameConfigFromFileViaMapstructure(file string, result *LoggerNameParser) error { +func ScopeConfigFromFileViaMapstructure(file string, result *ScopeNameParser) error { bytes, err := ioutil.ReadFile(file) if err != nil { return fmt.Errorf("could not find config file: %s", err) diff --git a/operator/helper/testdata/loggername/parse_from.yaml b/operator/helper/testdata/loggername/parse_from.yaml deleted file mode 100644 index 0416b1c6..00000000 --- a/operator/helper/testdata/loggername/parse_from.yaml +++ /dev/null @@ -1 +0,0 @@ -parse_from: $.from diff --git a/operator/helper/testdata/loggername/preserve_to.yaml b/operator/helper/testdata/loggername/preserve_to.yaml deleted file mode 100644 index d4a311e7..00000000 --- a/operator/helper/testdata/loggername/preserve_to.yaml +++ /dev/null @@ -1,2 +0,0 @@ -parse_from: $.from -preserve_to: aField diff --git a/operator/helper/testdata/scope_name/parse_from.yaml b/operator/helper/testdata/scope_name/parse_from.yaml new file mode 100644 index 00000000..12247acb --- /dev/null +++ b/operator/helper/testdata/scope_name/parse_from.yaml @@ -0,0 +1 @@ +parse_from: body.from diff --git a/operator/helper/testdata/scope_name/preserve_to.yaml b/operator/helper/testdata/scope_name/preserve_to.yaml new file mode 100644 index 00000000..539b3591 --- /dev/null +++ b/operator/helper/testdata/scope_name/preserve_to.yaml @@ -0,0 +1,2 @@ +parse_from: body.from +preserve_to: body.aField diff --git a/operator/output/stdout/stdout_test.go b/operator/output/stdout/stdout_test.go index 5c8b1d08..aa7d5279 100644 --- a/operator/output/stdout/stdout_test.go +++ b/operator/output/stdout/stdout_test.go @@ -55,6 +55,6 @@ func TestStdoutOperator(t *testing.T) { marshalledTimestamp, err := json.Marshal(ts) require.NoError(t, err) - expected := `{"timestamp":` + string(marshalledTimestamp) + `,"body":"test body","severity":0,"logger_name":""}` + "\n" + expected := `{"timestamp":` + string(marshalledTimestamp) + `,"body":"test body","severity":0,"scope_name":""}` + "\n" require.Equal(t, expected, buf.String()) } diff --git a/operator/parser/json/config_test.go b/operator/parser/json/config_test.go index 89763ede..21fa33df 100644 --- a/operator/parser/json/config_test.go +++ b/operator/parser/json/config_test.go @@ -93,12 +93,12 @@ func TestJSONParserConfig(t *testing.T) { }(), }, { - Name: "logger_name", + Name: "scope_name", Expect: func() *JSONParserConfig { cfg := defaultCfg() - loggerNameParser := helper.NewLoggerNameParser() + loggerNameParser := helper.NewScopeNameParser() loggerNameParser.ParseFrom = entry.NewBodyField("logger_name_field") - cfg.LoggerNameParser = &loggerNameParser + cfg.ScopeNameParser = &loggerNameParser return cfg }(), }, diff --git a/operator/parser/json/json_test.go b/operator/parser/json/json_test.go index f399bf8d..76516869 100644 --- a/operator/parser/json/json_test.go +++ b/operator/parser/json/json_test.go @@ -229,7 +229,7 @@ func TestJSONParserWithEmbeddedTimeParser(t *testing.T) { } } -func TestJSONParserWithEmbeddedLoggerNameParser(t *testing.T) { +func TestJSONParserWithEmbeddedScopeNameParser(t *testing.T) { cases := []struct { name string inputBody map[string]interface{} @@ -288,7 +288,7 @@ func TestJSONParserWithEmbeddedLoggerNameParser(t *testing.T) { parser, mockOutput := NewFakeJSONOperator() parseFrom := entry.NewBodyField("testparsed", "logger_name") - parser.ParserOperator.LoggerNameParser = &helper.LoggerNameParser{ + parser.ParserOperator.ScopeNameParser = &helper.ScopeNameParser{ ParseFrom: parseFrom, PreserveTo: tc.preserveTo, } diff --git a/operator/parser/json/testdata/logger_name.yaml b/operator/parser/json/testdata/logger_name.yaml deleted file mode 100644 index bcbeba91..00000000 --- a/operator/parser/json/testdata/logger_name.yaml +++ /dev/null @@ -1,3 +0,0 @@ -type: json_parser -logger_name: - parse_from: logger_name_field diff --git a/operator/parser/json/testdata/scope_name.yaml b/operator/parser/json/testdata/scope_name.yaml new file mode 100644 index 00000000..5b673ce8 --- /dev/null +++ b/operator/parser/json/testdata/scope_name.yaml @@ -0,0 +1,3 @@ +type: json_parser +scope_name: + parse_from: body.logger_name_field diff --git a/operator/parser/regex/config_test.go b/operator/parser/regex/config_test.go index c958dfc0..09fe0c9b 100644 --- a/operator/parser/regex/config_test.go +++ b/operator/parser/regex/config_test.go @@ -101,14 +101,14 @@ func TestRegexParserGoldenConfig(t *testing.T) { }(), }, { - Name: "logger_name", + Name: "scope_name", Expect: func() *RegexParserConfig { cfg := defaultCfg() cfg.Regex = "^Host=(?P[^,]+), Logger=(?P.*)$" parseField := entry.NewBodyField("logger_name_field") - loggerNameParser := helper.NewLoggerNameParser() + loggerNameParser := helper.NewScopeNameParser() loggerNameParser.ParseFrom = parseField - cfg.LoggerNameParser = &loggerNameParser + cfg.ScopeNameParser = &loggerNameParser return cfg }(), }, diff --git a/operator/parser/regex/testdata/logger_name.yaml b/operator/parser/regex/testdata/scope_name.yaml similarity index 63% rename from operator/parser/regex/testdata/logger_name.yaml rename to operator/parser/regex/testdata/scope_name.yaml index 963b0bf7..7b89bafb 100644 --- a/operator/parser/regex/testdata/logger_name.yaml +++ b/operator/parser/regex/testdata/scope_name.yaml @@ -1,4 +1,4 @@ type: regex_parser regex: '^Host=(?P[^,]+), Logger=(?P.*)$' -logger_name: - parse_from: logger_name_field +scope_name: + parse_from: body.logger_name_field diff --git a/operator/parser/loggername/logger_name.go b/operator/parser/scope/scope_name.go similarity index 52% rename from operator/parser/loggername/logger_name.go rename to operator/parser/scope/scope_name.go index ba93ca89..06ee0a82 100644 --- a/operator/parser/loggername/logger_name.go +++ b/operator/parser/scope/scope_name.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package loggername +package scope import ( "context" @@ -25,43 +25,43 @@ import ( ) func init() { - operator.Register("logger_name_parser", func() operator.Builder { return NewLoggerNameParserConfig("") }) + operator.Register("scope_name_parser", func() operator.Builder { return NewScopeNameParserConfig("") }) } -// NewLoggerNameParserConfig creates a new logger name parser config with default values -func NewLoggerNameParserConfig(operatorID string) *LoggerNameParserConfig { - return &LoggerNameParserConfig{ - TransformerConfig: helper.NewTransformerConfig(operatorID, "logger_name_parser"), - LoggerNameParser: helper.NewLoggerNameParser(), +// NewScopeNameParserConfig creates a new logger name parser config with default values +func NewScopeNameParserConfig(operatorID string) *ScopeNameParserConfig { + return &ScopeNameParserConfig{ + TransformerConfig: helper.NewTransformerConfig(operatorID, "scope_name_parser"), + ScopeNameParser: helper.NewScopeNameParser(), } } -// LoggerNameParserConfig is the configuration of a logger name parser operator. -type LoggerNameParserConfig struct { - helper.TransformerConfig `mapstructure:",squash" yaml:",inline"` - helper.LoggerNameParser `mapstructure:",omitempty,squash" yaml:",omitempty,inline"` +// ScopeNameParserConfig is the configuration of a logger name parser operator. +type ScopeNameParserConfig struct { + helper.TransformerConfig `mapstructure:",squash" yaml:",inline"` + helper.ScopeNameParser `mapstructure:",omitempty,squash" yaml:",omitempty,inline"` } // Build will build a logger name parser operator. -func (c LoggerNameParserConfig) Build(logger *zap.SugaredLogger) (operator.Operator, error) { +func (c ScopeNameParserConfig) Build(logger *zap.SugaredLogger) (operator.Operator, error) { transformerOperator, err := c.TransformerConfig.Build(logger) if err != nil { return nil, err } - return &LoggerNameParserOperator{ + return &ScopeNameParserOperator{ TransformerOperator: transformerOperator, - LoggerNameParser: c.LoggerNameParser, + ScopeNameParser: c.ScopeNameParser, }, nil } -// LoggerNameParserOperator is an operator that parses logger name from a field to an entry. -type LoggerNameParserOperator struct { +// ScopeNameParserOperator is an operator that parses logger name from a field to an entry. +type ScopeNameParserOperator struct { helper.TransformerOperator - helper.LoggerNameParser + helper.ScopeNameParser } // Process will parse logger name from an entry. -func (p *LoggerNameParserOperator) Process(ctx context.Context, entry *entry.Entry) error { +func (p *ScopeNameParserOperator) Process(ctx context.Context, entry *entry.Entry) error { return p.ProcessWith(ctx, entry, p.Parse) } diff --git a/operator/parser/loggername/logger_name_test.go b/operator/parser/scope/scope_name_test.go similarity index 73% rename from operator/parser/loggername/logger_name_test.go rename to operator/parser/scope/scope_name_test.go index 74e17cb9..6a7e24c9 100644 --- a/operator/parser/loggername/logger_name_test.go +++ b/operator/parser/scope/scope_name_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package loggername +package scope import ( "context" @@ -25,41 +25,41 @@ import ( "github.com/open-telemetry/opentelemetry-log-collection/testutil" ) -const testLoggerName = "my.logger" +const testScopeName = "my.logger" -func TestLoggerNameParser(t *testing.T) { +func TestScopeNameParser(t *testing.T) { now := time.Now() testCases := []struct { name string - config *LoggerNameParserConfig + config *ScopeNameParserConfig input *entry.Entry expectErr bool expected *entry.Entry }{ { name: "root_string", - config: func() *LoggerNameParserConfig { - cfg := NewLoggerNameParserConfig("test") + config: func() *ScopeNameParserConfig { + cfg := NewScopeNameParserConfig("test") cfg.ParseFrom = entry.NewBodyField() return cfg }(), input: func() *entry.Entry { e := entry.New() - e.Body = testLoggerName + e.Body = testScopeName e.Timestamp = now return e }(), expected: func() *entry.Entry { e := entry.New() - e.LoggerName = testLoggerName + e.ScopeName = testScopeName e.Timestamp = now return e }(), }, { name: "root_string_preserve", - config: func() *LoggerNameParserConfig { - cfg := NewLoggerNameParserConfig("test") + config: func() *ScopeNameParserConfig { + cfg := NewScopeNameParserConfig("test") cfg.ParseFrom = entry.NewBodyField() preserve := entry.NewBodyField() cfg.PreserveTo = &preserve @@ -67,64 +67,64 @@ func TestLoggerNameParser(t *testing.T) { }(), input: func() *entry.Entry { e := entry.New() - e.Body = testLoggerName + e.Body = testScopeName e.Timestamp = now return e }(), expected: func() *entry.Entry { e := entry.New() - e.Body = testLoggerName - e.LoggerName = testLoggerName + e.Body = testScopeName + e.ScopeName = testScopeName e.Timestamp = now return e }(), }, { name: "nondestructive_error", - config: func() *LoggerNameParserConfig { - cfg := NewLoggerNameParserConfig("test") + config: func() *ScopeNameParserConfig { + cfg := NewScopeNameParserConfig("test") cfg.ParseFrom = entry.NewBodyField() return cfg }(), input: func() *entry.Entry { e := entry.New() - e.Body = map[string]interface{}{"logger": testLoggerName} + e.Body = map[string]interface{}{"logger": testScopeName} e.Timestamp = now return e }(), expectErr: true, expected: func() *entry.Entry { e := entry.New() - e.Body = map[string]interface{}{"logger": testLoggerName} + e.Body = map[string]interface{}{"logger": testScopeName} e.Timestamp = now return e }(), }, { name: "nonroot_string", - config: func() *LoggerNameParserConfig { - cfg := NewLoggerNameParserConfig("test") + config: func() *ScopeNameParserConfig { + cfg := NewScopeNameParserConfig("test") cfg.ParseFrom = entry.NewBodyField("logger") return cfg }(), input: func() *entry.Entry { e := entry.New() - e.Body = map[string]interface{}{"logger": testLoggerName} + e.Body = map[string]interface{}{"logger": testScopeName} e.Timestamp = now return e }(), expected: func() *entry.Entry { e := entry.New() e.Body = map[string]interface{}{} - e.LoggerName = testLoggerName + e.ScopeName = testScopeName e.Timestamp = now return e }(), }, { name: "nonroot_string_preserve", - config: func() *LoggerNameParserConfig { - cfg := NewLoggerNameParserConfig("test") + config: func() *ScopeNameParserConfig { + cfg := NewScopeNameParserConfig("test") cfg.ParseFrom = entry.NewBodyField("logger") preserve := entry.NewBodyField("somewhere") cfg.PreserveTo = &preserve @@ -132,14 +132,14 @@ func TestLoggerNameParser(t *testing.T) { }(), input: func() *entry.Entry { e := entry.New() - e.Body = map[string]interface{}{"logger": testLoggerName} + e.Body = map[string]interface{}{"logger": testScopeName} e.Timestamp = now return e }(), expected: func() *entry.Entry { e := entry.New() - e.Body = map[string]interface{}{"somewhere": testLoggerName} - e.LoggerName = testLoggerName + e.Body = map[string]interface{}{"somewhere": testScopeName} + e.ScopeName = testScopeName e.Timestamp = now return e }(),