@@ -5,6 +5,7 @@ package splunk // import "github.com/open-telemetry/opentelemetry-collector-cont
55
66import (
77 "encoding/json"
8+ "regexp"
89 "strconv"
910 "strings"
1011)
@@ -31,6 +32,14 @@ const (
3132 DefaultRawPath = "/services/collector/raw"
3233 DefaultHealthPath = "/services/collector/health"
3334 DefaultAckPath = "/services/collector/ack"
35+
36+ // https://docs.splunk.com/Documentation/Splunk/9.2.1/Metrics/Overview#What_is_a_metric_data_point.3F
37+ // metric name can contain letters, numbers, underscore, dot or colon. cannot start with number or underscore, or contain metric_name
38+ metricNamePattern = "^metric_name:([A-Za-z\\ .:][A-Za-z0-9_\\ .:]*)$"
39+ )
40+
41+ var (
42+ metricNameRegexp = regexp .MustCompile (metricNamePattern )
3443)
3544
3645// AccessTokenPassthroughConfig configures passing through access tokens.
@@ -55,6 +64,15 @@ func (e *Event) IsMetric() bool {
5564 return e .Event == HecEventMetricType || (e .Event == nil && len (e .GetMetricValues ()) > 0 )
5665}
5766
67+ // checks if the field name matches the requirements for a metric datapoint field,
68+ // and returns the metric name and a bool indicating whether the field is a metric.
69+ func getMetricNameFromField (fieldName string ) (string , bool ) {
70+ if matches := metricNameRegexp .FindStringSubmatch (fieldName ); len (matches ) > 1 {
71+ return matches [1 ], ! strings .Contains (matches [1 ], "metric_name" )
72+ }
73+ return "" , false
74+ }
75+
5876// GetMetricValues extracts metric key value pairs from a Splunk HEC metric.
5977func (e * Event ) GetMetricValues () map [string ]any {
6078 if v , ok := e .Fields ["metric_name" ]; ok {
@@ -63,8 +81,11 @@ func (e *Event) GetMetricValues() map[string]any {
6381
6482 values := map [string ]any {}
6583 for k , v := range e .Fields {
66- if strings .HasPrefix (k , "metric_name:" ) {
67- values [k [12 :]] = v
84+ // only consider metric name if it fits regex criteria.
85+ // use matches[1] since first element contains entire string.
86+ // first subgroup will be the actual metric name.
87+ if metricName , ok := getMetricNameFromField (k ); ok {
88+ values [metricName ] = v
6889 }
6990 }
7091 return values
0 commit comments