-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[coordinator] Add Graphite rewrite cleanup directive for cleansing in…
…coming metrics (#3047)
- Loading branch information
1 parent
f19fa9a
commit 7f34add
Showing
7 changed files
with
250 additions
and
30 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
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,88 @@ | ||
// Copyright (c) 2020 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package ingestcarbon | ||
|
||
import ( | ||
"github.com/m3db/m3/src/cmd/services/m3query/config" | ||
) | ||
|
||
// nolint: gocyclo | ||
func copyAndRewrite( | ||
dst, src []byte, | ||
cfg *config.CarbonIngesterRewriteConfiguration, | ||
) []byte { | ||
if cfg == nil || !cfg.Cleanup { | ||
// No rewrite required. | ||
return append(dst[:0], src...) | ||
} | ||
|
||
// Copy into dst as we rewrite. | ||
dst = dst[:0] | ||
leadingDots := true | ||
numDots := 0 | ||
for _, c := range src { | ||
if c == '.' { | ||
numDots++ | ||
} else { | ||
numDots = 0 | ||
leadingDots = false | ||
} | ||
|
||
if leadingDots { | ||
// Currently processing leading dots. | ||
continue | ||
} | ||
|
||
if numDots > 1 { | ||
// Do not keep multiple dots. | ||
continue | ||
} | ||
|
||
if !(c >= 'a' && c <= 'z') && | ||
!(c >= 'A' && c <= 'Z') && | ||
!(c >= '0' && c <= '9') && | ||
c != '.' && | ||
c != '-' && | ||
c != '_' && | ||
c != ':' && | ||
c != '#' { | ||
// Invalid character, replace with underscore. | ||
if n := len(dst); n > 0 && dst[n-1] == '_' { | ||
// Preceding character already underscore. | ||
continue | ||
} | ||
dst = append(dst, '_') | ||
continue | ||
} | ||
|
||
// Valid character and not proceeding dot or multiple dots. | ||
dst = append(dst, c) | ||
} | ||
for i := len(dst) - 1; i >= 0; i-- { | ||
if dst[i] != '.' { | ||
// Found non dot. | ||
break | ||
} | ||
// Remove trailing dot. | ||
dst = dst[:i] | ||
} | ||
return dst | ||
} |
124 changes: 124 additions & 0 deletions
124
src/cmd/services/m3coordinator/ingest/carbon/rewrite_test.go
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,124 @@ | ||
// Copyright (c) 2020 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package ingestcarbon | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/m3db/m3/src/cmd/services/m3query/config" | ||
) | ||
|
||
func TestCopyAndRewrite(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
expected string | ||
cfg *config.CarbonIngesterRewriteConfiguration | ||
}{ | ||
{ | ||
name: "bad but no rewrite", | ||
input: "foo$$.bar%%.baz@@", | ||
expected: "foo$$.bar%%.baz@@", | ||
cfg: nil, | ||
}, | ||
{ | ||
name: "bad but no rewrite cleanup", | ||
input: "foo$$.bar%%.baz@@", | ||
expected: "foo$$.bar%%.baz@@", | ||
cfg: &config.CarbonIngesterRewriteConfiguration{ | ||
Cleanup: false, | ||
}, | ||
}, | ||
{ | ||
name: "good with rewrite cleanup", | ||
input: "foo.bar.baz", | ||
expected: "foo.bar.baz", | ||
cfg: &config.CarbonIngesterRewriteConfiguration{ | ||
Cleanup: true, | ||
}, | ||
}, | ||
{ | ||
name: "bad with rewrite cleanup", | ||
input: "foo$$.bar%%.baz@@", | ||
expected: "foo_.bar_.baz_", | ||
cfg: &config.CarbonIngesterRewriteConfiguration{ | ||
Cleanup: true, | ||
}, | ||
}, | ||
{ | ||
name: "collapse two dots with rewrite cleanup", | ||
input: "foo..bar.baz", | ||
expected: "foo.bar.baz", | ||
cfg: &config.CarbonIngesterRewriteConfiguration{ | ||
Cleanup: true, | ||
}, | ||
}, | ||
{ | ||
name: "collapse three and two dots with rewrite cleanup", | ||
input: "foo...bar..baz", | ||
expected: "foo.bar.baz", | ||
cfg: &config.CarbonIngesterRewriteConfiguration{ | ||
Cleanup: true, | ||
}, | ||
}, | ||
{ | ||
name: "remove leading dot with rewrite cleanup", | ||
input: ".foo.bar.baz", | ||
expected: "foo.bar.baz", | ||
cfg: &config.CarbonIngesterRewriteConfiguration{ | ||
Cleanup: true, | ||
}, | ||
}, | ||
{ | ||
name: "remove multiple leading dots with rewrite cleanup", | ||
input: "..foo.bar.baz", | ||
expected: "foo.bar.baz", | ||
cfg: &config.CarbonIngesterRewriteConfiguration{ | ||
Cleanup: true, | ||
}, | ||
}, | ||
{ | ||
name: "remove trailing dot with rewrite cleanup", | ||
input: "foo.bar.baz.", | ||
expected: "foo.bar.baz", | ||
cfg: &config.CarbonIngesterRewriteConfiguration{ | ||
Cleanup: true, | ||
}, | ||
}, | ||
{ | ||
name: "remove multiple trailing dots with rewrite cleanup", | ||
input: "foo.bar.baz..", | ||
expected: "foo.bar.baz", | ||
cfg: &config.CarbonIngesterRewriteConfiguration{ | ||
Cleanup: true, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
actual := copyAndRewrite(nil, []byte(test.input), test.cfg) | ||
require.Equal(t, test.expected, string(actual)) | ||
}) | ||
} | ||
} |
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