-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a coerce action to the attributesprocessor module
- Loading branch information
Hugh Simpson
committed
Feb 15, 2022
1 parent
e8deba7
commit 3e824c3
Showing
8 changed files
with
1,637 additions
and
4 deletions.
There are no files selected for viewing
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
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,69 @@ | ||
package attraction | ||
|
||
import ( | ||
"regexp" | ||
"strconv" | ||
|
||
"go.opentelemetry.io/collector/model/pdata" | ||
) | ||
|
||
var num = regexp.MustCompile(`^(\d+)(?:\.\d+)?$`) | ||
var dub = regexp.MustCompile(`^(\d+(?:\.\d+)?)$`) | ||
|
||
func coerceValue(to string, v pdata.AttributeValue) { | ||
switch to { | ||
case "string": | ||
switch v.Type().String() { | ||
case "STRING": | ||
default: | ||
v.SetStringVal(v.AsString()) | ||
} | ||
case "int": | ||
switch v.Type().String() { | ||
case "INT": | ||
case "DOUBLE": | ||
v.SetIntVal(int64(v.DoubleVal())) | ||
case "BOOL": | ||
if v.BoolVal() { | ||
v.SetIntVal(1) | ||
} else { | ||
v.SetIntVal(0) | ||
} | ||
case "STRING": | ||
s := v.StringVal() | ||
n := num.FindStringSubmatch(s) | ||
if n != nil { | ||
intVal, _ := strconv.Atoi(n[1]) | ||
v.SetIntVal(int64(intVal)) | ||
} else { | ||
v.SetIntVal(int64(0)) | ||
} | ||
default: | ||
v.SetIntVal(int64(0)) | ||
} | ||
case "double": | ||
switch v.Type().String() { | ||
case "INT": | ||
v.SetDoubleVal(float64(v.IntVal())) | ||
case "DOUBLE": | ||
case "BOOL": | ||
if v.BoolVal() { | ||
v.SetDoubleVal(1) | ||
} else { | ||
v.SetDoubleVal(0) | ||
} | ||
case "STRING": | ||
s := v.StringVal() | ||
n := dub.FindStringSubmatch(s) | ||
if n != nil { | ||
dubVal, _ := strconv.ParseFloat(n[1], 64) | ||
v.SetDoubleVal(dubVal) | ||
} else { | ||
v.SetDoubleVal(0) | ||
} | ||
default: | ||
v.SetDoubleVal(0) | ||
} | ||
default: // No-op | ||
} | ||
} |
Oops, something went wrong.