forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_parser_helper.go
153 lines (137 loc) · 4.94 KB
/
path_parser_helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2019, 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
//
// 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 protocol
import (
"errors"
"fmt"
"strconv"
"strings"
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
)
// PathParser implements the code needed to handle only the <metric_path> part of
// a Carbon metric line:
//
// <metric_path> <metric_value> <metric_timestamp>
//
// See https://graphite.readthedocs.io/en/latest/feeding-carbon.html#the-plaintext-protocol,
// for more information.
//
// The type PathParserHelper implements the common code for parsers that differ
// only by the way that they handle the <metric_path>.
type PathParser interface {
// ParsePath parses the <metric_path> of a Carbon line (see Parse function
// for description of the full line). The results of parsing the path are
// stored on the parsedPath struct. Implementers of the interface can assume
// that the PathParserHelper will never pass nil when calling this method.
ParsePath(path string, parsedPath *ParsedPath) error
}
// ParsedPath holds the result of parsing the <metric_path> with the ParsePath
// method on the PathParser interface.
type ParsedPath struct {
// MetricName extracted/generated by the parser.
MetricName string
// LabelKeys extracted/generated by the parser.
LabelKeys []*metricspb.LabelKey
// LabelValues extracted/generated by the parser.
LabelValues []*metricspb.LabelValue
// MetricType instructs the helper to generate the metric as the specified
// TargetMetricType.
MetricType TargetMetricType
}
type TargetMetricType string
const (
DefaultMetricType = TargetMetricType("")
GaugeMetricType = TargetMetricType("gauge")
CumulativeMetricType = TargetMetricType("cumulative")
)
// PathParserHelper implements the common code to parse a Carbon line taking a
// PathParser to implement a full parser.
type PathParserHelper struct {
pathParser PathParser
}
var _ (Parser) = (*PathParserHelper)(nil)
// BuildParser creates a new Parser instance that receives plaintext
// Carbon data.
func NewParser(pathParser PathParser) (Parser, error) {
if pathParser == nil {
return nil, errors.New("nil pathParser")
}
return &PathParserHelper{
pathParser: pathParser,
}, nil
}
// Parse receives the string with plaintext data, aka line, in the Carbon
// format and transforms it to the collector metric format. See
// https://graphite.readthedocs.io/en/latest/feeding-carbon.html#the-plaintext-protocol.
//
// The expected line is a text line in the following format:
// "<metric_path> <metric_value> <metric_timestamp>"
//
// The <metric_path> is where there are variations that require selection
// of specialized parsers to handle them, but include the metric name and
// labels/dimensions for the metric.
//
// The <metric_value> is the textual representation of the metric value.
//
// The <metric_timestamp> is the Unix time text of when the measurement was
// made.
func (pph *PathParserHelper) Parse(line string) (*metricspb.Metric, error) {
parts := strings.SplitN(line, " ", 4)
if len(parts) != 3 {
return nil, fmt.Errorf("invalid carbon metric [%s]", line)
}
path := parts[0]
valueStr := parts[1]
timestampStr := parts[2]
parsedPath := ParsedPath{}
err := pph.pathParser.ParsePath(path, &parsedPath)
if err != nil {
return nil, fmt.Errorf("invalid carbon metric [%s]: %v", line, err)
}
unixTime, err := strconv.ParseInt(timestampStr, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid carbon metric time [%s]: %v", line, err)
}
var metricType metricspb.MetricDescriptor_Type
point := metricspb.Point{
Timestamp: convertUnixSec(unixTime),
}
intVal, err := strconv.ParseInt(valueStr, 10, 64)
if err == nil {
if parsedPath.MetricType == CumulativeMetricType {
metricType = metricspb.MetricDescriptor_CUMULATIVE_INT64
} else {
metricType = metricspb.MetricDescriptor_GAUGE_INT64
}
point.Value = &metricspb.Point_Int64Value{Int64Value: intVal}
} else {
dblVal, err := strconv.ParseFloat(valueStr, 64)
if err != nil {
return nil, fmt.Errorf("invalid carbon metric value [%s]: %v", line, err)
}
if parsedPath.MetricType == CumulativeMetricType {
metricType = metricspb.MetricDescriptor_CUMULATIVE_DOUBLE
} else {
metricType = metricspb.MetricDescriptor_GAUGE_DOUBLE
}
point.Value = &metricspb.Point_DoubleValue{DoubleValue: dblVal}
}
metric := buildMetricForSinglePoint(
parsedPath.MetricName,
metricType,
parsedPath.LabelKeys,
parsedPath.LabelValues,
&point)
return metric, nil
}