-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
392 additions
and
26 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
exporter/collector/internal/datapointstorage/datapointcache.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// 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 | ||
// | ||
// https://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 datapointstorage | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"go.opentelemetry.io/collector/model/pdata" | ||
monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres" | ||
) | ||
|
||
const gcInterval = 20 * time.Minute | ||
|
||
type Cache map[string]usedPoint | ||
|
||
type usedPoint struct { | ||
point *pdata.NumberDataPoint | ||
used bool | ||
} | ||
|
||
// New instantiates a cache and starts background processes | ||
func NewCache(shutdown <-chan struct{}) Cache { | ||
c := make(Cache) | ||
go func() { | ||
for { | ||
c.gc(shutdown, time.NewTicker(gcInterval).C) | ||
} | ||
}() | ||
return c | ||
} | ||
|
||
// Get retrieves the point associated with the identifier, and whether | ||
// or not it was found | ||
func (c Cache) Get(identifier string) (*pdata.NumberDataPoint, bool) { | ||
point, found := c[identifier] | ||
if found { | ||
point.used = true | ||
c[identifier] = point | ||
} | ||
return point.point, found | ||
} | ||
|
||
// Set assigns the point to the identifier in the cache | ||
func (c Cache) Set(identifier string, point *pdata.NumberDataPoint) { | ||
c[identifier] = usedPoint{point, true} | ||
} | ||
|
||
// gc garbage collects the cache after the ticker ticks | ||
func (c Cache) gc(shutdown <-chan struct{}, tickerCh <-chan time.Time) { | ||
select { | ||
case <-shutdown: | ||
return | ||
case <-tickerCh: | ||
// garbage collect the cache | ||
for id, point := range c { | ||
if point.used { | ||
// for points that have been used, mark them as unused | ||
point.used = false | ||
c[id] = point | ||
} else { | ||
// for points that have not been used, delete points | ||
delete(c, id) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Identifier returns the unique string identifier for a metric | ||
func Identifier(resource *monitoredrespb.MonitoredResource, metric pdata.Metric, labels pdata.AttributeMap) string { | ||
var b strings.Builder | ||
|
||
// Resource identifiers | ||
fmt.Fprintf(&b, "%v", resource.GetLabels()) | ||
|
||
// Metric identifiers | ||
fmt.Fprintf(&b, " - %s", metric.Name()) | ||
labels.Sort().Range(func(k string, v pdata.AttributeValue) bool { | ||
fmt.Fprintf(&b, " %s=%s", k, v.AsString()) | ||
return true | ||
}) | ||
return b.String() | ||
} |
110 changes: 110 additions & 0 deletions
110
exporter/collector/internal/datapointstorage/datapointcache_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// 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 | ||
// | ||
// https://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 datapointstorage | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/model/pdata" | ||
) | ||
|
||
func TestSetAndGet(t *testing.T) { | ||
c := make(Cache) | ||
c.Set("foo", nil) | ||
point, found := c.Get("foo") | ||
assert.Nil(t, point) | ||
assert.True(t, found) | ||
|
||
point, found = c.Get("bar") | ||
assert.Nil(t, point) | ||
assert.False(t, found) | ||
|
||
setPoint := pdata.NewNumberDataPoint() | ||
c.Set("bar", &setPoint) | ||
|
||
point, found = c.Get("bar") | ||
assert.Equal(t, point, &setPoint) | ||
assert.True(t, found) | ||
} | ||
|
||
func TestShutdown(t *testing.T) { | ||
shutdown := make(chan struct{}) | ||
c := make(Cache) | ||
close(shutdown) | ||
// gc should return after shutdown is closed | ||
c.gc(shutdown, make(chan time.Time)) | ||
} | ||
|
||
func TestGC(t *testing.T) { | ||
shutdown := make(chan struct{}) | ||
c := make(Cache) | ||
fakeTicker := make(chan time.Time) | ||
|
||
c.Set("bar", nil) | ||
|
||
// bar exists since we just set it | ||
usedPoint, found := c["bar"] | ||
assert.True(t, usedPoint.used) | ||
assert.True(t, found) | ||
|
||
// first gc tick marks bar stale | ||
go func() { | ||
fakeTicker <- time.Now() | ||
}() | ||
c.gc(shutdown, fakeTicker) | ||
usedPoint, found = c["bar"] | ||
assert.False(t, usedPoint.used) | ||
assert.True(t, found) | ||
|
||
// second gc tick removes bar | ||
go func() { | ||
fakeTicker <- time.Now() | ||
}() | ||
c.gc(shutdown, fakeTicker) | ||
_, found = c["bar"] | ||
assert.False(t, found) | ||
} | ||
|
||
func TestGetPreventsGC(t *testing.T) { | ||
shutdown := make(chan struct{}) | ||
c := make(Cache) | ||
fakeTicker := make(chan time.Time) | ||
|
||
setPoint := pdata.NewNumberDataPoint() | ||
c.Set("bar", &setPoint) | ||
|
||
// bar exists since we just set it | ||
_, found := c["bar"] | ||
assert.True(t, found) | ||
|
||
// first gc tick marks bar stale | ||
go func() { | ||
fakeTicker <- time.Now() | ||
}() | ||
c.gc(shutdown, fakeTicker) | ||
// calling Get() marks it fresh again. | ||
_, found = c.Get("bar") | ||
assert.True(t, found) | ||
|
||
// second gc tick does not remove bar | ||
go func() { | ||
fakeTicker <- time.Now() | ||
}() | ||
c.gc(shutdown, fakeTicker) | ||
_, found = c["bar"] | ||
assert.True(t, found) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.