-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consolidate attribute code into single package
- Loading branch information
1 parent
f90f3b6
commit 52b1688
Showing
10 changed files
with
264 additions
and
283 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,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: deprecation | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: pkg/stanza | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Deprecate pkg/stanza/attrs package in favor of pkg/stanza/fileconsumer/attrs | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [30449] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [api] |
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,60 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package attrs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/attrs" | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"runtime" | ||
) | ||
|
||
const ( | ||
LogFileName = "log.file.name" | ||
LogFilePath = "log.file.path" | ||
LogFileNameResolved = "log.file.name_resolved" | ||
LogFilePathResolved = "log.file.path_resolved" | ||
) | ||
|
||
type Resolver struct { | ||
IncludeFileName bool `mapstructure:"include_file_name,omitempty"` | ||
IncludeFilePath bool `mapstructure:"include_file_path,omitempty"` | ||
IncludeFileNameResolved bool `mapstructure:"include_file_name_resolved,omitempty"` | ||
IncludeFilePathResolved bool `mapstructure:"include_file_path_resolved,omitempty"` | ||
} | ||
|
||
func (r *Resolver) Resolve(path string) (attributes map[string]any, err error) { | ||
// size 2 is sufficient if not resolving symlinks. This optimizes for the most performant cases. | ||
attributes = make(map[string]any, 2) | ||
if r.IncludeFileName { | ||
attributes[LogFileName] = filepath.Base(path) | ||
} | ||
if r.IncludeFilePath { | ||
attributes[LogFilePath] = path | ||
} | ||
if !r.IncludeFileNameResolved && !r.IncludeFilePathResolved { | ||
return attributes, nil | ||
} | ||
|
||
resolved := path | ||
// Dirty solution, waiting for this permanent fix https://github.com/golang/go/issues/39786 | ||
// EvalSymlinks on windows is partially working depending on the way you use Symlinks and Junctions | ||
if runtime.GOOS != "windows" { | ||
resolved, err = filepath.EvalSymlinks(path) | ||
if err != nil { | ||
return nil, fmt.Errorf("resolve symlinks: %w", err) | ||
} | ||
} | ||
abs, err := filepath.Abs(resolved) | ||
if err != nil { | ||
return nil, fmt.Errorf("resolve abs: %w", err) | ||
} | ||
|
||
if r.IncludeFileNameResolved { | ||
attributes[LogFileNameResolved] = filepath.Base(abs) | ||
} | ||
if r.IncludeFilePathResolved { | ||
attributes[LogFilePathResolved] = abs | ||
} | ||
return attributes, nil | ||
} |
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,73 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package attrs | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/filetest" | ||
) | ||
|
||
func TestResolver(t *testing.T) { | ||
t.Parallel() | ||
|
||
for i := 0; i < 16; i++ { | ||
|
||
// Create a 4 bit string where each bit represents the value of a config option | ||
bitString := fmt.Sprintf("%04b", i) | ||
|
||
// Create a resolver with a config that matches the bit pattern of i | ||
r := Resolver{ | ||
IncludeFileName: bitString[0] == '1', | ||
IncludeFilePath: bitString[1] == '1', | ||
IncludeFileNameResolved: bitString[2] == '1', | ||
IncludeFilePathResolved: bitString[3] == '1', | ||
} | ||
|
||
t.Run(bitString, func(t *testing.T) { | ||
// Create a file | ||
tempDir := t.TempDir() | ||
temp := filetest.OpenTemp(t, tempDir) | ||
|
||
attributes, err := r.Resolve(temp.Name()) | ||
assert.NoError(t, err) | ||
|
||
var expectLen int | ||
if r.IncludeFileName { | ||
expectLen++ | ||
assert.Equal(t, filepath.Base(temp.Name()), attributes[LogFileName]) | ||
} else { | ||
assert.Empty(t, attributes[LogFileName]) | ||
} | ||
if r.IncludeFilePath { | ||
expectLen++ | ||
assert.Equal(t, temp.Name(), attributes[LogFilePath]) | ||
} else { | ||
assert.Empty(t, attributes[LogFilePath]) | ||
} | ||
|
||
// We don't have an independent way to resolve the path, so the only meangingful validate | ||
// is to ensure that the resolver returns nothing vs something based on the config. | ||
if r.IncludeFileNameResolved { | ||
expectLen++ | ||
assert.NotNil(t, attributes[LogFileNameResolved]) | ||
assert.IsType(t, "", attributes[LogFileNameResolved]) | ||
} else { | ||
assert.Empty(t, attributes[LogFileNameResolved]) | ||
} | ||
if r.IncludeFilePathResolved { | ||
expectLen++ | ||
assert.NotNil(t, attributes[LogFilePathResolved]) | ||
assert.IsType(t, "", attributes[LogFilePathResolved]) | ||
} else { | ||
assert.Empty(t, attributes[LogFilePathResolved]) | ||
} | ||
assert.Equal(t, expectLen, len(attributes)) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.