Skip to content

Commit

Permalink
improve util helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rubvs committed Sep 20, 2024
1 parent 6a3dbe9 commit a6450a9
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 47 deletions.
23 changes: 0 additions & 23 deletions input/otlp/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ import (
"github.com/elastic/apm-data/model/modelpb"
)

const (
MaxDataStreamBytes = 100
DisallowedDataStreamRunes = "-\\/*?\"<>| ,#"
)

// ConsumeLogsResult contains the number of rejected log records and error message for partial success response.
type ConsumeLogsResult struct {
ErrorMessage string
Expand Down Expand Up @@ -244,21 +239,3 @@ func (c *Consumer) convertLogRecord(

return event
}

// Sanitize the datastream fields (dataset, namespace) to apply restrictions
// as outlined in https://www.elastic.co/guide/en/ecs/current/ecs-data_stream.html
func sanitizeDataStreamField(field string) string {
field = strings.ToLower(field)
field = strings.Map(replaceReservedRune, field)
if len(field) > MaxDataStreamBytes {
return field[:MaxDataStreamBytes]
}
return field
}

func replaceReservedRune(r rune) rune {
if strings.ContainsRune(DisallowedDataStreamRunes, r) {
return '_'
}
return r
}
20 changes: 2 additions & 18 deletions input/otlp/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ package otlp_test

import (
"context"
"crypto/rand"
"fmt"
"math/big"
"testing"
"time"

Expand Down Expand Up @@ -579,8 +577,8 @@ func TestConsumerConsumeLogsDataStream(t *testing.T) {
{
resourceDataStreamDataset: otlp.DisallowedDataStreamRunes + randomString,
resourceDataStreamNamespace: otlp.DisallowedDataStreamRunes + randomString,
expectedDataStreamDataset: "____________" + randomString[:maxLen],
expectedDataStreamNamespace: "____________" + randomString[:maxLen],
expectedDataStreamDataset: revertedString(otlp.DisallowedDataStreamRunes) + randomString[:maxLen],
expectedDataStreamNamespace: revertedString(otlp.DisallowedDataStreamRunes) + randomString[:maxLen],
},
} {
tcName := fmt.Sprintf("%s,%s", tc.expectedDataStreamDataset, tc.expectedDataStreamNamespace)
Expand Down Expand Up @@ -846,17 +844,3 @@ func newLogRecord(body interface{}) plog.LogRecord {
}
return otelLogRecord
}

const charset = "abcdefghijklmnopqrstuvwxyz0123456789"

func generateRandomString(length int) (string, error) {
result := make([]byte, length)
for i := range result {
index, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
if err != nil {
return "", err
}
result[i] = charset[index.Int64()]
}
return string(result), nil
}
22 changes: 21 additions & 1 deletion input/otlp/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ import (
)

const (
AgentNameJaeger = "Jaeger"
AgentNameJaeger = "Jaeger"
MaxDataStreamBytes = 100
DisallowedDataStreamRunes = "-\\/*?\"<>| ,#:"
)

var (
Expand Down Expand Up @@ -545,3 +547,21 @@ func setLabel(key string, event *modelpb.APMEvent, v pcommon.Value) {
}
}
}

// Sanitize the datastream fields (dataset, namespace) to apply restrictions
// as outlined in https://www.elastic.co/guide/en/ecs/current/ecs-data_stream.html
func sanitizeDataStreamField(field string) string {
field = strings.ToLower(field)
field = strings.Map(replaceReservedRune, field)
if len(field) > MaxDataStreamBytes {
return field[:MaxDataStreamBytes]
}
return field
}

func replaceReservedRune(r rune) rune {
if strings.ContainsRune(DisallowedDataStreamRunes, r) {
return '_'
}
return r
}
25 changes: 25 additions & 0 deletions input/otlp/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package otlp_test

import (
"crypto/rand"
"math/big"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -395,3 +397,26 @@ func transformResourceMetadata(t *testing.T, resourceAttrs map[string]interface{
(*events)[0].Timestamp = 0
return (*events)[0]
}

const charset = "abcdefghijklmnopqrstuvwxyz0123456789"

func generateRandomString(length int) (string, error) {
result := make([]byte, length)
for i := range result {
index, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
if err != nil {
return "", err
}
result[i] = charset[index.Int64()]
}
return string(result), nil
}

// Creates a string consistening of _ that equals the length of otlp.DisallowedDataStreamRunes
func revertedString(s string) string {
var res []rune
for i := 0; i < len(s); i++ {
res = append(res, '_')
}
return string(res)
}
2 changes: 1 addition & 1 deletion input/otlp/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (c *Consumer) handleScopeMetrics(
if event.DataStream == nil {
event.DataStream = &modelpb.DataStream{}
}
event.DataStream.Dataset = v.Str()
event.DataStream.Dataset = sanitizeDataStreamField(v.Str())
case attributeDataStreamNamespace:
if event.DataStream == nil {
event.DataStream = &modelpb.DataStream{}
Expand Down
4 changes: 2 additions & 2 deletions input/otlp/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,8 +774,8 @@ func TestConsumeMetricsDataStream(t *testing.T) {
{
resourceDataStreamDataset: otlp.DisallowedDataStreamRunes + randomString,
resourceDataStreamNamespace: otlp.DisallowedDataStreamRunes + randomString,
expectedDataStreamDataset: "____________" + randomString[:maxLen],
expectedDataStreamNamespace: "____________" + randomString[:maxLen],
expectedDataStreamDataset: revertedString(otlp.DisallowedDataStreamRunes) + randomString[:maxLen],
expectedDataStreamNamespace: revertedString(otlp.DisallowedDataStreamRunes) + randomString[:maxLen],
},
} {
tcName := fmt.Sprintf("%s,%s", tc.expectedDataStreamDataset, tc.expectedDataStreamNamespace)
Expand Down
4 changes: 2 additions & 2 deletions input/otlp/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -918,8 +918,8 @@ func TestSpanDataStream(t *testing.T) {
{
resourceDataStreamDataset: otlp.DisallowedDataStreamRunes + randomString,
resourceDataStreamNamespace: otlp.DisallowedDataStreamRunes + randomString,
expectedDataStreamDataset: "____________" + randomString[:maxLen],
expectedDataStreamNamespace: "____________" + randomString[:maxLen],
expectedDataStreamDataset: revertedString(otlp.DisallowedDataStreamRunes) + randomString[:maxLen],
expectedDataStreamNamespace: revertedString(otlp.DisallowedDataStreamRunes) + randomString[:maxLen],
},
} {
for _, isTxn := range []bool{false, true} {
Expand Down

0 comments on commit a6450a9

Please sign in to comment.