Skip to content

Commit 105b38c

Browse files
committed
Create a hash package and move fnv hashing
Signed-off-by: Kyle Eckhart <[email protected]>
1 parent c79a108 commit 105b38c

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

collectors/monitoring_metrics.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"sort"
2323

24+
"github.com/prometheus-community/stackdriver_exporter/hash"
2425
"github.com/prometheus-community/stackdriver_exporter/utils"
2526
)
2627

@@ -196,13 +197,13 @@ func (t *timeSeriesMetrics) newConstMetric(fqName string, reportTime time.Time,
196197
}
197198

198199
func hashLabelKeys(labelKeys []string) uint64 {
199-
dh := HashNew()
200+
dh := hash.New()
200201
sortedKeys := make([]string, len(labelKeys))
201202
copy(sortedKeys, labelKeys)
202203
sort.Strings(sortedKeys)
203204
for _, key := range sortedKeys {
204-
dh = HashAdd(dh, key)
205-
dh = hashAddByte(dh, separatorByte)
205+
dh = hash.Add(dh, key)
206+
dh = hash.AddByte(dh, hash.SeparatorByte)
206207
}
207208
return dh
208209
}

delta/counter.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"google.golang.org/api/monitoring/v3"
2626

2727
"github.com/prometheus-community/stackdriver_exporter/collectors"
28+
"github.com/prometheus-community/stackdriver_exporter/hash"
2829
)
2930

3031
type MetricEntry struct {
@@ -95,8 +96,8 @@ func toCounterKey(c *collectors.ConstMetric) uint64 {
9596
keyParts = append(keyParts, fmt.Sprintf("%s:%s", k, labels[k]))
9697
}
9798
hashText := fmt.Sprintf("%s|%s", c.FqName, strings.Join(keyParts, "|"))
98-
h := collectors.HashNew()
99-
h = collectors.HashAdd(h, hashText)
99+
h := hash.New()
100+
h = hash.Add(h, hashText)
100101

101102
return h
102103
}

delta/histogram.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"google.golang.org/api/monitoring/v3"
2626

2727
"github.com/prometheus-community/stackdriver_exporter/collectors"
28+
"github.com/prometheus-community/stackdriver_exporter/hash"
2829
)
2930

3031
type HistogramEntry struct {
@@ -94,8 +95,8 @@ func toHistogramKey(hist *collectors.HistogramMetric) uint64 {
9495
keyParts = append(keyParts, fmt.Sprintf("%s:%s", k, labels[k]))
9596
}
9697
hashText := fmt.Sprintf("%s|%s", hist.FqName, strings.Join(keyParts, "|"))
97-
h := collectors.HashNew()
98-
h = collectors.HashAdd(h, hashText)
98+
h := hash.New()
99+
h = hash.Add(h, hashText)
99100

100101
return h
101102
}

collectors/fnv.go renamed to hash/fnv.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
package collectors
14+
package hash
1515

16-
const separatorByte = 255
16+
const SeparatorByte = 255
1717

1818
// https://github.com/prometheus/client_golang/blob/master/prometheus/fnv.go
1919
// Inline and byte-free variant of hash/fnv's fnv64a.
@@ -23,22 +23,22 @@ const (
2323
prime64 = 1099511628211
2424
)
2525

26-
// HashNew initializies a new fnv64a hash value.
27-
func HashNew() uint64 {
26+
// New initializies a new fnv64a hash value.
27+
func New() uint64 {
2828
return offset64
2929
}
3030

31-
// HashAdd adds a string to a fnv64a hash value, returning the updated hash.
32-
func HashAdd(h uint64, s string) uint64 {
31+
// Add adds a string to a fnv64a hash value, returning the updated hash.
32+
func Add(h uint64, s string) uint64 {
3333
for i := 0; i < len(s); i++ {
3434
h ^= uint64(s[i])
3535
h *= prime64
3636
}
3737
return h
3838
}
3939

40-
// hashAddByte adds a byte to a fnv64a hash value, returning the updated hash.
41-
func hashAddByte(h uint64, b byte) uint64 {
40+
// AddByte adds a byte to a fnv64a hash value, returning the updated hash.
41+
func AddByte(h uint64, b byte) uint64 {
4242
h ^= uint64(b)
4343
h *= prime64
4444
return h

0 commit comments

Comments
 (0)