This repository has been archived by the owner on May 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a dedicated mechanism for parsing logger name
- Loading branch information
1 parent
6c3c01e
commit bb44cea
Showing
19 changed files
with
764 additions
and
34 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
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> |
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,65 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package helper | ||
|
||
import ( | ||
"github.com/open-telemetry/opentelemetry-log-collection/entry" | ||
"github.com/open-telemetry/opentelemetry-log-collection/errors" | ||
) | ||
|
||
// 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"` | ||
} | ||
|
||
// 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 *ScopeNameParser) Parse(ent *entry.Entry) error { | ||
value, ok := ent.Delete(p.ParseFrom) | ||
if !ok { | ||
return errors.NewError( | ||
"log entry does not have the expected parse_from field", | ||
"ensure that all entries forwarded to this parser contain the parse_from field", | ||
"parse_from", p.ParseFrom.String(), | ||
) | ||
} | ||
|
||
strVal, ok := value.(string) | ||
if !ok { | ||
err := ent.Set(p.ParseFrom, value) | ||
if err != nil { | ||
return errors.Wrap(err, "parse_from field does not contain a string") | ||
} | ||
return errors.NewError( | ||
"parse_from field does not contain a string", | ||
"ensure that all entries forwarded to this parser contain a string in the parse_from field", | ||
"parse_from", p.ParseFrom.String(), | ||
) | ||
} | ||
|
||
ent.ScopeName = strVal | ||
if p.PreserveTo != nil { | ||
if err := ent.Set(p.PreserveTo, value); err != nil { | ||
return errors.Wrap(err, "set preserve_to") | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.