-
Notifications
You must be signed in to change notification settings - Fork 2.9k
SFx exporter: add delta translator #839
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
Merged
tigrannajaryan
merged 13 commits into
open-telemetry:master
from
pmcollins:delta-translator
Sep 2, 2020
Merged
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
793a71a
SFx exporter: add delta translator
pmcollins 3cf4ec7
Rebase from master
pmcollins ca96a79
Improve coverage
pmcollins 613e16c
Minor refactor
pmcollins d949810
Add ttl map
pmcollins ba3803e
Handle negative deltas
pmcollins a0e7052
Emit last value on negative deltas, make delta ttl configurable
pmcollins 1c534f6
Address PR feedback
pmcollins 2d1eb89
Put TTLMap into its own package
pmcollins 451ccf1
Improve test coverage
pmcollins 782ec1f
Increase test coverage
pmcollins 0aa9df4
Increase test coverage
pmcollins a2ef82b
Prevent race during test
pmcollins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,86 @@ | ||
// Copyright 2020, 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 translation | ||
|
||
import ( | ||
"github.com/gogo/protobuf/proto" | ||
sfxpb "github.com/signalfx/com_signalfx_metrics_protobuf/model" | ||
) | ||
|
||
type deltaTranslator struct { | ||
prevPts map[string]*sfxpb.DataPoint | ||
} | ||
|
||
func newDeltaTranslator() *deltaTranslator { | ||
return &deltaTranslator{prevPts: map[string]*sfxpb.DataPoint{}} | ||
} | ||
|
||
func (t *deltaTranslator) translate(processedDataPoints []*sfxpb.DataPoint, tr Rule) []*sfxpb.DataPoint { | ||
for _, currPt := range processedDataPoints { | ||
deltaMetricName, ok := tr.Mapping[currPt.Metric] | ||
if !ok { | ||
// only metrics defined in Rule.Mapping get translated | ||
continue | ||
} | ||
deltaPt := t.deltaPt(deltaMetricName, currPt) | ||
if deltaPt == nil { | ||
continue | ||
} | ||
processedDataPoints = append(processedDataPoints, deltaPt) | ||
} | ||
return processedDataPoints | ||
} | ||
|
||
func (t *deltaTranslator) deltaPt(deltaMetricName string, currPt *sfxpb.DataPoint) *sfxpb.DataPoint { | ||
// check if we have a previous point for this metric + dimensions | ||
dimKey := stringifyDimensions(currPt.Dimensions, nil) | ||
fullKey := currPt.Metric + ":" + dimKey | ||
prevPt, ok := t.prevPts[fullKey] | ||
t.prevPts[fullKey] = currPt | ||
pmcollins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if !ok { | ||
// no previous point, so we can't calculate a delta | ||
return nil | ||
} | ||
var deltaPt *sfxpb.DataPoint | ||
if currPt.Value.DoubleValue != nil && prevPt.Value.DoubleValue != nil { | ||
deltaPt = doublePt(currPt, prevPt, deltaMetricName) | ||
} else if currPt.Value.IntValue != nil && prevPt.Value.IntValue != nil { | ||
deltaPt = intPt(currPt, prevPt, deltaMetricName) | ||
} else { | ||
return nil | ||
} | ||
return deltaPt | ||
} | ||
|
||
func doublePt(currPt *sfxpb.DataPoint, prevPt *sfxpb.DataPoint, deltaMetricName string) *sfxpb.DataPoint { | ||
pmcollins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
deltaPt := basePt(currPt, deltaMetricName) | ||
*deltaPt.Value.DoubleValue = *currPt.Value.DoubleValue - *prevPt.Value.DoubleValue | ||
return deltaPt | ||
} | ||
|
||
func intPt(currPt *sfxpb.DataPoint, prevPt *sfxpb.DataPoint, deltaMetricName string) *sfxpb.DataPoint { | ||
deltaPt := basePt(currPt, deltaMetricName) | ||
*deltaPt.Value.IntValue = *currPt.Value.IntValue - *prevPt.Value.IntValue | ||
pmcollins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return deltaPt | ||
} | ||
|
||
var cumulativeCounterType = sfxpb.MetricType_CUMULATIVE_COUNTER | ||
|
||
func basePt(currPt *sfxpb.DataPoint, deltaMetricName string) *sfxpb.DataPoint { | ||
deltaPt := proto.Clone(currPt).(*sfxpb.DataPoint) | ||
deltaPt.Metric = deltaMetricName | ||
deltaPt.MetricType = &cumulativeCounterType | ||
pmcollins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return deltaPt | ||
} |
This file contains hidden or 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
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.