-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DogStatsD parser and UDP server transport
- Loading branch information
Nick Fischer
committed
Jun 29, 2020
1 parent
01393f6
commit 2034105
Showing
5 changed files
with
225 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package protocol | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1" | ||
) | ||
|
||
type DogStatsDParser struct{} | ||
|
||
func (p *DogStatsDParser) Parse(line string) (*metricspb.Metric, error) { | ||
parts := strings.Split(line, ":") | ||
if len(parts) < 2 { | ||
return nil, errors.New("not enough statsd message parts") | ||
} | ||
|
||
return &metricspb.Metric{ | ||
MetricDescriptor: &metricspb.MetricDescriptor{ | ||
Name: parts[0], | ||
}, | ||
}, nil | ||
} |
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,9 @@ | ||
package protocol | ||
|
||
import ( | ||
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1" | ||
) | ||
|
||
type Parser interface { | ||
Parse(in string) (*metricspb.Metric, error) | ||
} |
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,43 @@ | ||
// 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 | ||
// | ||
// 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 transport | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/protocol" | ||
"go.opentelemetry.io/collector/consumer" | ||
) | ||
|
||
var ( | ||
errNilListenAndServeParameters = errors.New( | ||
"no parameter of ListenAndServe can be nil") | ||
) | ||
|
||
// Server abstracts the type of transport being used and offer an | ||
// interface to handle serving clients over that transport. | ||
type Server interface { | ||
// ListenAndServe is a blocking call that starts to listen for client messages | ||
// on the specific transport, and prepares the message to be processed by | ||
// the Parser and passed to the next consumer. | ||
ListenAndServe( | ||
p protocol.Parser, | ||
mc consumer.MetricsConsumerOld, | ||
) error | ||
|
||
// Close stops any running ListenAndServe, however, it waits for any | ||
// data already received to be parsed and sent to the next consumer. | ||
Close() error | ||
} |
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,124 @@ | ||
// 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 | ||
// | ||
// 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 transport | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io" | ||
"net" | ||
"strings" | ||
"sync" | ||
|
||
"go.opentelemetry.io/collector/consumer" | ||
"go.opentelemetry.io/collector/consumer/consumerdata" | ||
|
||
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/statsdreceiver/protocol" | ||
) | ||
|
||
type udpServer struct { | ||
wg sync.WaitGroup | ||
packetConn net.PacketConn | ||
} | ||
|
||
var _ (Server) = (*udpServer)(nil) | ||
|
||
// NewUDPServer creates a transport.Server using UDP as its transport. | ||
func NewUDPServer(addr string) (Server, error) { | ||
packetConn, err := net.ListenPacket("udp", addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
u := udpServer{ | ||
packetConn: packetConn, | ||
} | ||
return &u, nil | ||
} | ||
|
||
func (u *udpServer) ListenAndServe( | ||
parser protocol.Parser, | ||
nextConsumer consumer.MetricsConsumerOld, | ||
) error { | ||
if parser == nil || nextConsumer == nil { | ||
return errNilListenAndServeParameters | ||
} | ||
|
||
buf := make([]byte, 65527) // max size for udp packet body (assuming ipv6) | ||
for { | ||
n, _, err := u.packetConn.ReadFrom(buf) | ||
if n > 0 { | ||
u.wg.Add(1) | ||
bufCopy := make([]byte, n) | ||
copy(bufCopy, buf) | ||
go func() { | ||
u.handlePacket(parser, nextConsumer, bufCopy) | ||
u.wg.Done() | ||
}() | ||
} | ||
if err != nil { | ||
if netErr, ok := err.(net.Error); ok { | ||
if netErr.Temporary() { | ||
continue | ||
} | ||
} | ||
return err | ||
} | ||
} | ||
} | ||
|
||
func (u *udpServer) Close() error { | ||
err := u.packetConn.Close() | ||
u.wg.Wait() | ||
return err | ||
} | ||
|
||
func (u *udpServer) handlePacket( | ||
p protocol.Parser, | ||
nextConsumer consumer.MetricsConsumerOld, | ||
data []byte, | ||
) { | ||
ctx := context.Background() | ||
var numReceivedTimeseries, numInvalidTimeseries int | ||
var metrics []*metricspb.Metric | ||
buf := bytes.NewBuffer(data) | ||
for { | ||
bytes, err := buf.ReadBytes((byte)('\n')) | ||
if err == io.EOF { | ||
if len(bytes) == 0 { | ||
// Completed without errors. | ||
break | ||
} | ||
} | ||
line := strings.TrimSpace(string(bytes)) | ||
if line != "" { | ||
numReceivedTimeseries++ | ||
metric, err := p.Parse(line) | ||
if err != nil { | ||
numInvalidTimeseries++ | ||
continue | ||
} | ||
|
||
metrics = append(metrics, metric) | ||
} | ||
} | ||
|
||
md := consumerdata.MetricsData{ | ||
Metrics: metrics, | ||
} | ||
// TODO: handle error? | ||
nextConsumer.ConsumeMetricsData(ctx, md) | ||
} |