Skip to content

Commit 8479183

Browse files
[pkg/ottl] Add Murmur3Hash converter (#37027)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Add four murmur3 hash functions to convert the `target` string to hexadecimal string format `Murmur3Hash` returns a hexadecimal string in little-endian of the 32-bit Murmur3 hash `Murmur3Hash128` returns a hexadecimal string in little-endian of the 128-bit Murmur3 hash Example: `Murmur3Hash("20250106")` <!-- Issue number (e.g. #1234) or full URL to issue, if applicable. --> #### Link to tracking issue issue: #34077 supersedes: #34155 <!--Describe what testing was performed and which tests were added.--> #### Testing - Unit tests - E2E tests <!--Describe the documentation added.--> #### Documentation readme <!--Please delete paragraphs that you did not use before submitting.--> --------- Co-authored-by: Edmo Vamerlatti Costa <[email protected]>
1 parent 2026aef commit 8479183

File tree

42 files changed

+383
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+383
-0
lines changed

.chloggen/ottl_murmur3_func.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: pkg/ottl
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add `Murmur3Hash`and `Murmur3Hash128` functions to convert the `target` string to Murmur3 hash in hexadecimal string format
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [34077]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

cmd/opampsupervisor/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ require (
121121
github.com/spf13/pflag v1.0.6 // indirect
122122
github.com/tklauser/go-sysconf v0.3.14 // indirect
123123
github.com/tklauser/numcpus v0.8.0 // indirect
124+
github.com/twmb/murmur3 v1.1.8 // indirect
124125
github.com/ua-parser/uap-go v0.0.0-20240611065828-3a4781585db6 // indirect
125126
github.com/valyala/fastjson v1.6.4 // indirect
126127
github.com/yusufpapurcu/wmi v1.2.4 // indirect

cmd/opampsupervisor/go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

connector/countconnector/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ require (
5353
github.com/modern-go/reflect2 v1.0.2 // indirect
5454
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.121.0 // indirect
5555
github.com/pmezard/go-difflib v1.0.0 // indirect
56+
github.com/twmb/murmur3 v1.1.8 // indirect
5657
github.com/ua-parser/uap-go v0.0.0-20240611065828-3a4781585db6 // indirect
5758
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
5859
go.opentelemetry.io/collector/connector/xconnector v0.121.1-0.20250313100724-0885401136ff // indirect

connector/countconnector/go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

connector/routingconnector/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ require (
5353
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.121.0 // indirect
5454
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.121.0 // indirect
5555
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
56+
github.com/twmb/murmur3 v1.1.8 // indirect
5657
github.com/ua-parser/uap-go v0.0.0-20240611065828-3a4781585db6 // indirect
5758
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
5859
go.opentelemetry.io/collector/connector/xconnector v0.121.1-0.20250313100724-0885401136ff // indirect

connector/routingconnector/go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

connector/signaltometricsconnector/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ require (
5555
github.com/modern-go/reflect2 v1.0.2 // indirect
5656
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.121.0 // indirect
5757
github.com/pmezard/go-difflib v1.0.0 // indirect
58+
github.com/twmb/murmur3 v1.1.8 // indirect
5859
github.com/ua-parser/uap-go v0.0.0-20240611065828-3a4781585db6 // indirect
5960
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
6061
go.opentelemetry.io/collector/connector/xconnector v0.121.1-0.20250313100724-0885401136ff // indirect

connector/signaltometricsconnector/go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

connector/sumconnector/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ require (
5454
github.com/modern-go/reflect2 v1.0.2 // indirect
5555
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.121.0 // indirect
5656
github.com/pmezard/go-difflib v1.0.0 // indirect
57+
github.com/twmb/murmur3 v1.1.8 // indirect
5758
github.com/ua-parser/uap-go v0.0.0-20240611065828-3a4781585db6 // indirect
5859
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
5960
go.opentelemetry.io/collector/connector/xconnector v0.121.1-0.20250313100724-0885401136ff // indirect

connector/sumconnector/go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exporter/elasticsearchexporter/integrationtest/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ require (
133133
github.com/spf13/pflag v1.0.6 // indirect
134134
github.com/tklauser/go-sysconf v0.3.14 // indirect
135135
github.com/tklauser/numcpus v0.8.0 // indirect
136+
github.com/twmb/murmur3 v1.1.8 // indirect
136137
github.com/ua-parser/uap-go v0.0.0-20240611065828-3a4781585db6 // indirect
137138
github.com/valyala/fastjson v1.6.4 // indirect
138139
github.com/yusufpapurcu/wmi v1.2.4 // indirect

exporter/elasticsearchexporter/integrationtest/go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exporter/honeycombmarkerexporter/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ require (
6363
github.com/pierrec/lz4/v4 v4.1.22 // indirect
6464
github.com/pmezard/go-difflib v1.0.0 // indirect
6565
github.com/rs/cors v1.11.1 // indirect
66+
github.com/twmb/murmur3 v1.1.8 // indirect
6667
github.com/ua-parser/uap-go v0.0.0-20240611065828-3a4781585db6 // indirect
6768
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
6869
go.opentelemetry.io/collector/client v1.27.1-0.20250313100724-0885401136ff // indirect

exporter/honeycombmarkerexporter/go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/filter/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ require (
4848
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4949
github.com/modern-go/reflect2 v1.0.2 // indirect
5050
github.com/pmezard/go-difflib v1.0.0 // indirect
51+
github.com/twmb/murmur3 v1.1.8 // indirect
5152
github.com/ua-parser/uap-go v0.0.0-20240611065828-3a4781585db6 // indirect
5253
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
5354
go.opentelemetry.io/collector/pdata/pprofile v0.121.1-0.20250313100724-0885401136ff // indirect

internal/filter/go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/ottl/e2e/e2e_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,18 @@ func Test_e2e_converters(t *testing.T) {
763763
tCtx.GetLogRecord().Attributes().PutDouble("test", 60)
764764
},
765765
},
766+
{
767+
statement: `set(attributes["test"], Murmur3Hash128("Hello World"))`,
768+
want: func(tCtx ottllog.TransformContext) {
769+
tCtx.GetLogRecord().Attributes().PutStr("test", "dbc2a0c1ab26631a27b4c09fcf1fe683")
770+
},
771+
},
772+
{
773+
statement: `set(attributes["test"], Murmur3Hash("Hello World"))`,
774+
want: func(tCtx ottllog.TransformContext) {
775+
tCtx.GetLogRecord().Attributes().PutStr("test", "ce837619")
776+
},
777+
},
766778
{
767779
statement: `set(attributes["test"], Nanoseconds(Duration("1ms")))`,
768780
want: func(tCtx ottllog.TransformContext) {

pkg/ottl/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.121.0
1515
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.121.0
1616
github.com/stretchr/testify v1.10.0
17+
github.com/twmb/murmur3 v1.1.8
1718
github.com/ua-parser/uap-go v0.0.0-20240611065828-3a4781585db6
1819
go.opentelemetry.io/collector/component v1.27.1-0.20250313100724-0885401136ff
1920
go.opentelemetry.io/collector/component/componenttest v0.121.1-0.20250313100724-0885401136ff

pkg/ottl/go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/ottl/ottlfuncs/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,8 @@ Available Converters:
489489
- [Minute](#minute)
490490
- [Minutes](#minutes)
491491
- [Month](#month)
492+
- [Murmur3Hash](#murmur3hash)
493+
- [Murmur3Hash128](#murmur3hash128)
492494
- [Nanosecond](#nanosecond)
493495
- [Nanoseconds](#nanoseconds)
494496
- [Now](#now)
@@ -1345,6 +1347,34 @@ Examples:
13451347

13461348
- `Month(Now())`
13471349

1350+
### Murmur3Hash
1351+
1352+
`Murmur3Hash(target)`
1353+
1354+
The `Murmur3Hash` Converter converts the `target` string to a hexadecimal string in little-endian of the 32-bit Murmur3 hash.
1355+
1356+
`target` is a Getter that returns a string.
1357+
1358+
The returned type is `string`.
1359+
1360+
Examples:
1361+
1362+
- `Murmur3Hash(attributes["order.productId"])`
1363+
1364+
### Murmur3Hash128
1365+
1366+
`Murmur3Hash128(target)`
1367+
1368+
The `Murmur3Hash128` Converter converts the `target` string to a hexadecimal string in little-endian of the 128-bit Murmur3 hash.
1369+
1370+
`target` is a Getter that returns a string.
1371+
1372+
The returned type is `string`.
1373+
1374+
Examples:
1375+
1376+
- `Murmur3Hash128(attributes["order.productId"])`
1377+
13481378
### Nanosecond
13491379

13501380
`Nanosecond(value)`
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"
5+
6+
import (
7+
"context"
8+
"encoding/binary"
9+
"encoding/hex"
10+
"fmt"
11+
12+
"github.com/twmb/murmur3"
13+
14+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
15+
)
16+
17+
type Murmur3HashArguments[K any] struct {
18+
Target ottl.StringGetter[K]
19+
}
20+
21+
func NewMurmur3HashFactory[K any]() ottl.Factory[K] {
22+
return ottl.NewFactory("Murmur3Hash", &Murmur3HashArguments[K]{}, createMurmur3HashFunction[K])
23+
}
24+
25+
func createMurmur3HashFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) {
26+
args, ok := oArgs.(*Murmur3HashArguments[K])
27+
28+
if !ok {
29+
return nil, fmt.Errorf("Murmur3HashFactory args must be of type *Murmur3HashArguments[K]")
30+
}
31+
32+
return murmur3Hash(args.Target), nil
33+
}
34+
35+
func murmur3Hash[K any](target ottl.StringGetter[K]) ottl.ExprFunc[K] {
36+
return func(ctx context.Context, tCtx K) (any, error) {
37+
val, err := target.Get(ctx, tCtx)
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
h := murmur3.Sum32([]byte(val))
43+
b := make([]byte, 4)
44+
binary.LittleEndian.PutUint32(b, h)
45+
return hex.EncodeToString(b), nil
46+
}
47+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"
5+
6+
import (
7+
"context"
8+
"encoding/binary"
9+
"encoding/hex"
10+
"fmt"
11+
12+
"github.com/twmb/murmur3"
13+
14+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
15+
)
16+
17+
type Murmur3Hash128Arguments[K any] struct {
18+
Target ottl.StringGetter[K]
19+
}
20+
21+
func NewMurmur3Hash128Factory[K any]() ottl.Factory[K] {
22+
return ottl.NewFactory("Murmur3Hash128", &Murmur3Hash128Arguments[K]{}, createMurmur3Hash128Function[K])
23+
}
24+
25+
func createMurmur3Hash128Function[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) {
26+
args, ok := oArgs.(*Murmur3Hash128Arguments[K])
27+
28+
if !ok {
29+
return nil, fmt.Errorf("Murmur3Hash128Factory args must be of type *Murmur3Hash128Arguments[K]")
30+
}
31+
32+
return murmur3Hash128(args.Target), nil
33+
}
34+
35+
func murmur3Hash128[K any](target ottl.StringGetter[K]) ottl.ExprFunc[K] {
36+
return func(ctx context.Context, tCtx K) (any, error) {
37+
val, err := target.Get(ctx, tCtx)
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
h1, h2 := murmur3.Sum128([]byte(val))
43+
b := make([]byte, 16)
44+
binary.LittleEndian.PutUint64(b[:8], h1)
45+
binary.LittleEndian.PutUint64(b[8:], h2)
46+
return hex.EncodeToString(b), nil
47+
}
48+
}

0 commit comments

Comments
 (0)