Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid alloc on creating hashable func #33

Merged
merged 3 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions aggregationpb/hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License 2.0;
// you may not use this file except in compliance with the Elastic License 2.0.

package aggregationpb

import (
"encoding/binary"

"github.com/cespare/xxhash/v2"

"github.com/elastic/apm-aggregation/aggregators/nullable"
)

func (k *ServiceAggregationKey) Hash(h xxhash.Digest) xxhash.Digest {
var buf [8]byte
binary.LittleEndian.PutUint64(buf[:], k.Timestamp)
h.Write(buf[:])

h.WriteString(k.ServiceName)
h.WriteString(k.ServiceEnvironment)
h.WriteString(k.ServiceLanguageName)
h.WriteString(k.AgentName)
return h
}

func (k *ServiceInstanceAggregationKey) Hash(h xxhash.Digest) xxhash.Digest {
h.Write(k.GlobalLabelsStr)
return h
}

func (k *ServiceTransactionAggregationKey) Hash(h xxhash.Digest) xxhash.Digest {
h.WriteString(k.TransactionType)
return h
}

func (k *SpanAggregationKey) Hash(h xxhash.Digest) xxhash.Digest {
h.WriteString(k.SpanName)
h.WriteString(k.Outcome)

h.WriteString(k.TargetType)
h.WriteString(k.TargetName)

h.WriteString(k.Resource)
return h
}

func (k *TransactionAggregationKey) Hash(h xxhash.Digest) xxhash.Digest {
if k.TraceRoot {
h.WriteString("1")
}

h.WriteString(k.ContainerId)
h.WriteString(k.KubernetesPodName)

h.WriteString(k.ServiceVersion)
h.WriteString(k.ServiceNodeName)

h.WriteString(k.ServiceRuntimeName)
h.WriteString(k.ServiceRuntimeVersion)
h.WriteString(k.ServiceLanguageVersion)

h.WriteString(k.HostHostname)
h.WriteString(k.HostName)
h.WriteString(k.HostOsPlatform)

h.WriteString(k.EventOutcome)

h.WriteString(k.TransactionName)
h.WriteString(k.TransactionType)
h.WriteString(k.TransactionResult)

if k.FaasColdstart == uint32(nullable.True) {
h.WriteString("1")
}
h.WriteString(k.FaasId)
h.WriteString(k.FaasName)
h.WriteString(k.FaasVersion)
h.WriteString(k.FaasTriggerType)

h.WriteString(k.CloudProvider)
h.WriteString(k.CloudRegion)
h.WriteString(k.CloudAvailabilityZone)
h.WriteString(k.CloudServiceName)
h.WriteString(k.CloudAccountId)
h.WriteString(k.CloudAccountName)
h.WriteString(k.CloudMachineType)
h.WriteString(k.CloudProjectId)
h.WriteString(k.CloudProjectName)
return h
}
3 changes: 2 additions & 1 deletion aggregators/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/elastic/apm-aggregation/aggregationpb"
"github.com/elastic/apm-aggregation/aggregators/internal/hdrhistogram"
"github.com/elastic/apm-aggregation/aggregators/internal/timestamppb"
"github.com/elastic/apm-aggregation/aggregators/nullable"
"github.com/elastic/apm-data/model/modelpb"
)

Expand Down Expand Up @@ -327,7 +328,7 @@ func (k *TransactionAggregationKey) FromProto(pb *aggregationpb.TransactionAggre
k.TransactionType = pb.TransactionType
k.TransactionResult = pb.TransactionResult

k.FAASColdstart = NullableBool(pb.FaasColdstart)
k.FAASColdstart = nullable.NullableBool(pb.FaasColdstart)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can change nullable.NullableBool to nullable.Bool

k.FAASID = pb.FaasId
k.FAASName = pb.FaasName
k.FAASVersion = pb.FaasVersion
Expand Down
17 changes: 9 additions & 8 deletions aggregators/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/elastic/apm-aggregation/aggregationpb"
"github.com/elastic/apm-aggregation/aggregators/internal/hdrhistogram"
tspb "github.com/elastic/apm-aggregation/aggregators/internal/timestamppb"
"github.com/elastic/apm-aggregation/aggregators/nullable"
"github.com/elastic/apm-data/model/modelpb"
)

Expand Down Expand Up @@ -51,8 +52,8 @@ func EventToCombinedMetrics(
return err
}
hasher := Hasher{}.
Chain(serviceKeyHasher(svcKey)).
Chain(serviceInstanceKeyHasher(svcInstanceKey))
Chain(svcKey).
Chain(svcInstanceKey)

// m collects service instance metrics for each partition
m := make(map[uint16]*aggregationpb.ServiceInstanceMetrics)
Expand Down Expand Up @@ -100,7 +101,7 @@ func EventToCombinedMetrics(
svcInstanceMetrics.TransactionMetrics, ktm,
)
partitionID := partitioner.Partition(
hasher.Chain(transactionKeyHasher(txnKey)).Sum(),
hasher.Chain(txnKey).Sum(),
)
addToM(partitionID, svcInstanceMetrics)

Expand All @@ -118,7 +119,7 @@ func EventToCombinedMetrics(
svcInstanceMetrics.ServiceTransactionMetrics, kstm,
)
partitionID = partitioner.Partition(
hasher.Chain(serviceTransactionKeyHasher(svcTxnKey)).Sum(),
hasher.Chain(svcTxnKey).Sum(),
)
addToM(partitionID, svcInstanceMetrics)

Expand All @@ -137,7 +138,7 @@ func EventToCombinedMetrics(
svcInstanceMetrics.SpanMetrics, kspm,
)
partitionID = partitioner.Partition(
hasher.Chain(spanKeyHasher(dssKey)).Sum(),
hasher.Chain(dssKey).Sum(),
)
addToM(partitionID, svcInstanceMetrics)
}
Expand Down Expand Up @@ -169,7 +170,7 @@ func EventToCombinedMetrics(
svcInstanceMetrics.SpanMetrics, kspm,
)
partitionID := partitioner.Partition(
hasher.Chain(spanKeyHasher(spanKey)).Sum(),
hasher.Chain(spanKey).Sum(),
)
addToM(partitionID, svcInstanceMetrics)
default:
Expand Down Expand Up @@ -505,7 +506,7 @@ func txnMetricsToAPMEvent(
baseEvent.Host.Os.Platform = key.HostOSPlatform
}

if key.FAASColdstart != Nil ||
if key.FAASColdstart != nullable.Nil ||
key.FAASID != "" ||
key.FAASName != "" ||
key.FAASVersion != "" ||
Expand Down Expand Up @@ -774,7 +775,7 @@ func serviceInstanceKey(e *modelpb.APMEvent) (*aggregationpb.ServiceInstanceAggr
}

func transactionKey(e *modelpb.APMEvent) *aggregationpb.TransactionAggregationKey {
var faasColdstart NullableBool
var faasColdstart nullable.NullableBool
faas := e.GetFaas()
if faas != nil {
faasColdstart.ParseBoolPtr(faas.ColdStart)
Expand Down
102 changes: 0 additions & 102 deletions aggregators/hasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
package aggregators

import (
"encoding/binary"

"github.com/cespare/xxhash/v2"

"github.com/elastic/apm-aggregation/aggregationpb"
)

// HashableFunc is a function type that implements Hashable.
Expand Down Expand Up @@ -39,101 +35,3 @@ func (h Hasher) Chain(hashable Hashable) Hasher {
func (h Hasher) Sum() uint64 {
return h.digest.Sum64()
}

func serviceKeyHasher(
k *aggregationpb.ServiceAggregationKey,
) Hashable {
return HashableFunc(func(h xxhash.Digest) xxhash.Digest {
var buf [8]byte
binary.LittleEndian.PutUint64(buf[:], k.Timestamp)
h.Write(buf[:])

h.WriteString(k.ServiceName)
h.WriteString(k.ServiceEnvironment)
h.WriteString(k.ServiceLanguageName)
h.WriteString(k.AgentName)
return h
})
}

func serviceInstanceKeyHasher(
k *aggregationpb.ServiceInstanceAggregationKey,
) Hashable {
return HashableFunc(func(h xxhash.Digest) xxhash.Digest {
h.Write(k.GlobalLabelsStr)
return h
})
}

func serviceTransactionKeyHasher(
k *aggregationpb.ServiceTransactionAggregationKey,
) Hashable {
return HashableFunc(func(h xxhash.Digest) xxhash.Digest {
h.WriteString(k.TransactionType)
return h
})
}

func spanKeyHasher(
k *aggregationpb.SpanAggregationKey,
) Hashable {
return HashableFunc(func(h xxhash.Digest) xxhash.Digest {
h.WriteString(k.SpanName)
h.WriteString(k.Outcome)

h.WriteString(k.TargetType)
h.WriteString(k.TargetName)

h.WriteString(k.Resource)
return h
})
}

func transactionKeyHasher(
k *aggregationpb.TransactionAggregationKey,
) Hashable {
return HashableFunc(func(h xxhash.Digest) xxhash.Digest {
if k.TraceRoot {
h.WriteString("1")
}

h.WriteString(k.ContainerId)
h.WriteString(k.KubernetesPodName)

h.WriteString(k.ServiceVersion)
h.WriteString(k.ServiceNodeName)

h.WriteString(k.ServiceRuntimeName)
h.WriteString(k.ServiceRuntimeVersion)
h.WriteString(k.ServiceLanguageVersion)

h.WriteString(k.HostHostname)
h.WriteString(k.HostName)
h.WriteString(k.HostOsPlatform)

h.WriteString(k.EventOutcome)

h.WriteString(k.TransactionName)
h.WriteString(k.TransactionType)
h.WriteString(k.TransactionResult)

if k.FaasColdstart == uint32(True) {
h.WriteString("1")
}
h.WriteString(k.FaasId)
h.WriteString(k.FaasName)
h.WriteString(k.FaasVersion)
h.WriteString(k.FaasTriggerType)

h.WriteString(k.CloudProvider)
h.WriteString(k.CloudRegion)
h.WriteString(k.CloudAvailabilityZone)
h.WriteString(k.CloudServiceName)
h.WriteString(k.CloudAccountId)
h.WriteString(k.CloudAccountName)
h.WriteString(k.CloudMachineType)
h.WriteString(k.CloudProjectId)
h.WriteString(k.CloudProjectName)
return h
})
}
46 changes: 3 additions & 43 deletions aggregators/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,10 @@ import (
"github.com/cespare/xxhash/v2"

"github.com/elastic/apm-aggregation/aggregators/internal/hdrhistogram"
"github.com/elastic/apm-aggregation/aggregators/nullable"
"github.com/elastic/apm-data/model/modelpb"
)

// NullableBool represents a bool value which can be set to nil.
// Using uint32 since uint32 is smallest proto type.
type NullableBool uint32

const (
// Nil represents an unset bool value.
Nil NullableBool = iota
// False represents a false bool value.
False
// True represents a true bool value.
True
)

// ParseBoolPtr sets nullable bool from bool pointer.
func (nb *NullableBool) ParseBoolPtr(b *bool) {
if b == nil {
*nb = Nil
return
}
if *b {
*nb = True
return
}
*nb = False
}

// ToBoolPtr converts Nullable bool to bool pointer.
func (nb *NullableBool) ToBoolPtr() *bool {
if nb == nil || *nb == Nil {
return nil
}
var b bool
switch *nb {
case False:
b = false
case True:
b = true
}
return &b
}

// Limits define the aggregation limits. Once the limits are reached
// the metrics will overflow into dedicated overflow buckets.
type Limits struct {
Expand Down Expand Up @@ -306,7 +266,7 @@ type TransactionAggregationKey struct {
TransactionType string
TransactionResult string

FAASColdstart NullableBool
FAASColdstart nullable.NullableBool
FAASID string
FAASName string
FAASVersion string
Expand Down Expand Up @@ -349,7 +309,7 @@ func (k TransactionAggregationKey) Hash(h xxhash.Digest) xxhash.Digest {
h.WriteString(k.TransactionType)
h.WriteString(k.TransactionResult)

if k.FAASColdstart == True {
if k.FAASColdstart == nullable.True {
h.WriteString("1")
}
h.WriteString(k.FAASID)
Expand Down
Loading