Skip to content

Commit

Permalink
Initial revision
Browse files Browse the repository at this point in the history
  • Loading branch information
billmeyer committed May 5, 2022
1 parent a8d278e commit 6f84046
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
37 changes: 37 additions & 0 deletions exporter/mezmoexporter/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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 mezmoexporter

import "math/rand"

// truncateString Truncates the given string to a maximum length provided by max.
func truncateString(s string, max int) string {
if len(s) < max {
return s
}

return s[:max]
}

const letters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

// randString Returns a random string of the specified length.
func randString(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
56 changes: 56 additions & 0 deletions exporter/mezmoexporter/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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 mezmoexporter

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestTruncateString(t *testing.T) {
t.Run("Test empty string", func(t *testing.T) {
s := truncateString("", 10)
require.Len(t, s, 0)
})

// Test string is less than the maximum length
t.Run("Test shorter string", func(t *testing.T) {
s := truncateString("short", 10)
require.Len(t, s, 5)
require.Equal(t, s, "short")
})

// Test string is equal to the maximum length
t.Run("Test equal string", func(t *testing.T) {
s := truncateString("short", 5)
require.Len(t, s, 5)
require.Equal(t, s, "short")
})

// Test string is longer than the maximum length
t.Run("Test longer string", func(t *testing.T) {
s := truncateString("longstring", 4)
require.Len(t, s, 4)
require.Equal(t, s, "long")
})
}

func TestRandString(t *testing.T) {
t.Run("Test fixed length string", func(t *testing.T) {
var s = randString(16 * 1024)
require.Len(t, s, 16*1024)
})
}

0 comments on commit 6f84046

Please sign in to comment.