Skip to content
This repository has been archived by the owner on May 25, 2022. It is now read-only.

Commit

Permalink
Change from logger_name to scope_name
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski committed Mar 24, 2022
1 parent d9940bb commit e90c940
Show file tree
Hide file tree
Showing 21 changed files with 190 additions and 190 deletions.
64 changes: 0 additions & 64 deletions docs/types/logger_name.md

This file was deleted.

64 changes: 64 additions & 0 deletions docs/types/scope_name.md
Original file line number Diff line number Diff line change
@@ -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<scope_name_field>\S*)\s-\s(?P<message>.*)'
scope_name:
parse_from: body.scope_name_field
```
<table>
<tr><td> Input entry </td> <td> Output entry </td></tr>
<tr>
<td>
```json
{
"resource": { },
"attributes": { },
"body": "com.example.Foo - some message",
"scope_name": "",
}
```

</td>
<td>

```json
{
"resource": { },
"attributes": { },
"body": {
"message": "some message",
},
"scope_name": "com.example.Foo",
}
```

</td>
</tr>
</table>
4 changes: 2 additions & 2 deletions entry/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
}
}
10 changes: 5 additions & 5 deletions entry/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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) {
Expand Down
22 changes: 11 additions & 11 deletions operator/helper/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions operator/helper/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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",
},
}

Expand Down Expand Up @@ -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"),
},
}
Expand Down
14 changes: 7 additions & 7 deletions operator/helper/logger_name.go → operator/helper/scope_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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")
Expand Down
Loading

0 comments on commit e90c940

Please sign in to comment.