-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Log Support in Attribute Processor (2/2) (#1934)
This complements the trace span support already present. It shares the same config struct to maintain backwards-compatibility with spans in the cleanest way possible.
- Loading branch information
1 parent
7de4bf9
commit 3a185c5
Showing
6 changed files
with
640 additions
and
7 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,99 @@ | ||
// 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 attributesprocessor | ||
|
||
import ( | ||
"context" | ||
|
||
"go.opentelemetry.io/collector/consumer/pdata" | ||
"go.opentelemetry.io/collector/internal/processor/filterlog" | ||
"go.opentelemetry.io/collector/processor/processorhelper" | ||
) | ||
|
||
type logAttributesProcessor struct { | ||
attrProc *processorhelper.AttrProc | ||
include filterlog.Matcher | ||
exclude filterlog.Matcher | ||
} | ||
|
||
// newLogAttributesProcessor returns a processor that modifies attributes of a | ||
// log record. To construct the attributes processors, the use of the factory | ||
// methods are required in order to validate the inputs. | ||
func newLogAttributesProcessor(attrProc *processorhelper.AttrProc, include, exclude filterlog.Matcher) *logAttributesProcessor { | ||
return &logAttributesProcessor{ | ||
attrProc: attrProc, | ||
include: include, | ||
exclude: exclude, | ||
} | ||
} | ||
|
||
// ProcessLogs implements the LogsProcessor | ||
func (a *logAttributesProcessor) ProcessLogs(_ context.Context, ld pdata.Logs) (pdata.Logs, error) { | ||
rls := ld.ResourceLogs() | ||
for i := 0; i < rls.Len(); i++ { | ||
rs := rls.At(i) | ||
if rs.IsNil() { | ||
continue | ||
} | ||
ilss := rs.InstrumentationLibraryLogs() | ||
resource := rs.Resource() | ||
for j := 0; j < ilss.Len(); j++ { | ||
ils := ilss.At(j) | ||
if ils.IsNil() { | ||
continue | ||
} | ||
logs := ils.Logs() | ||
library := ils.InstrumentationLibrary() | ||
for k := 0; k < logs.Len(); k++ { | ||
lr := logs.At(k) | ||
if lr.IsNil() { | ||
// Do not create empty log records just to add attributes | ||
continue | ||
} | ||
|
||
if a.skipLog(lr, resource, library) { | ||
continue | ||
} | ||
|
||
a.attrProc.Process(lr.Attributes()) | ||
} | ||
} | ||
} | ||
return ld, nil | ||
} | ||
|
||
// skipLog determines if a log should be processed. | ||
// True is returned when a log should be skipped. | ||
// False is returned when a log should not be skipped. | ||
// The logic determining if a log should be processed is set | ||
// in the attribute configuration with the include and exclude settings. | ||
// Include properties are checked before exclude settings are checked. | ||
func (a *logAttributesProcessor) skipLog(lr pdata.LogRecord, resource pdata.Resource, library pdata.InstrumentationLibrary) bool { | ||
if a.include != nil { | ||
// A false returned in this case means the log should not be processed. | ||
if include := a.include.MatchLogRecord(lr, resource, library); !include { | ||
return true | ||
} | ||
} | ||
|
||
if a.exclude != nil { | ||
// A true returned in this case means the log should not be processed. | ||
if exclude := a.exclude.MatchLogRecord(lr, resource, library); exclude { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
Oops, something went wrong.