-
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.
- Loading branch information
Showing
7 changed files
with
735 additions
and
141 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,126 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package libhoneyreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/libhoneyreceiver" | ||
|
||
import ( | ||
"bytes" | ||
|
||
"github.com/gogo/protobuf/jsonpb" | ||
spb "google.golang.org/genproto/googleapis/rpc/status" | ||
|
||
"go.opentelemetry.io/collector/pdata/plog/plogotlp" | ||
"go.opentelemetry.io/collector/pdata/pmetric/pmetricotlp" | ||
"go.opentelemetry.io/collector/pdata/ptrace/ptraceotlp" | ||
) | ||
|
||
const ( | ||
pbContentType = "application/x-protobuf" | ||
jsonContentType = "application/json" | ||
msgpackContentType = "application/x-msgpack" | ||
) | ||
|
||
var ( | ||
jsEncoder = &jsonEncoder{} | ||
jsonPbMarshaler = &jsonpb.Marshaler{} | ||
mpEncoder = &msgpackEncoder{} | ||
) | ||
|
||
type encoder interface { | ||
unmarshalTracesRequest(buf []byte) (ptraceotlp.ExportRequest, error) | ||
unmarshalMetricsRequest(buf []byte) (pmetricotlp.ExportRequest, error) | ||
unmarshalLogsRequest(buf []byte) (plogotlp.ExportRequest, error) | ||
|
||
marshalTracesResponse(ptraceotlp.ExportResponse) ([]byte, error) | ||
marshalMetricsResponse(pmetricotlp.ExportResponse) ([]byte, error) | ||
marshalLogsResponse(plogotlp.ExportResponse) ([]byte, error) | ||
|
||
marshalStatus(rsp *spb.Status) ([]byte, error) | ||
|
||
contentType() string | ||
} | ||
|
||
type jsonEncoder struct{} | ||
|
||
func (jsonEncoder) unmarshalTracesRequest(buf []byte) (ptraceotlp.ExportRequest, error) { | ||
req := ptraceotlp.NewExportRequest() | ||
err := req.UnmarshalJSON(buf) | ||
return req, err | ||
} | ||
|
||
func (jsonEncoder) unmarshalMetricsRequest(buf []byte) (pmetricotlp.ExportRequest, error) { | ||
req := pmetricotlp.NewExportRequest() | ||
err := req.UnmarshalJSON(buf) | ||
return req, err | ||
} | ||
|
||
func (jsonEncoder) unmarshalLogsRequest(buf []byte) (plogotlp.ExportRequest, error) { | ||
req := plogotlp.NewExportRequest() | ||
err := req.UnmarshalJSON(buf) | ||
return req, err | ||
} | ||
|
||
func (jsonEncoder) marshalTracesResponse(resp ptraceotlp.ExportResponse) ([]byte, error) { | ||
return resp.MarshalJSON() | ||
} | ||
|
||
func (jsonEncoder) marshalMetricsResponse(resp pmetricotlp.ExportResponse) ([]byte, error) { | ||
return resp.MarshalJSON() | ||
} | ||
|
||
func (jsonEncoder) marshalLogsResponse(resp plogotlp.ExportResponse) ([]byte, error) { | ||
return resp.MarshalJSON() | ||
} | ||
|
||
func (jsonEncoder) marshalStatus(resp *spb.Status) ([]byte, error) { | ||
buf := new(bytes.Buffer) | ||
err := jsonPbMarshaler.Marshal(buf, resp) | ||
return buf.Bytes(), err | ||
} | ||
|
||
func (jsonEncoder) contentType() string { | ||
return jsonContentType | ||
} | ||
|
||
// messagepack responses seem to work in JSON so leaving this alone for now. | ||
type msgpackEncoder struct{} | ||
|
||
func (msgpackEncoder) unmarshalTracesRequest(buf []byte) (ptraceotlp.ExportRequest, error) { | ||
req := ptraceotlp.NewExportRequest() | ||
err := req.UnmarshalJSON(buf) | ||
return req, err | ||
} | ||
|
||
func (msgpackEncoder) unmarshalMetricsRequest(buf []byte) (pmetricotlp.ExportRequest, error) { | ||
req := pmetricotlp.NewExportRequest() | ||
err := req.UnmarshalJSON(buf) | ||
return req, err | ||
} | ||
|
||
func (msgpackEncoder) unmarshalLogsRequest(buf []byte) (plogotlp.ExportRequest, error) { | ||
req := plogotlp.NewExportRequest() | ||
err := req.UnmarshalJSON(buf) | ||
return req, err | ||
} | ||
|
||
func (msgpackEncoder) marshalTracesResponse(resp ptraceotlp.ExportResponse) ([]byte, error) { | ||
return resp.MarshalJSON() | ||
} | ||
|
||
func (msgpackEncoder) marshalMetricsResponse(resp pmetricotlp.ExportResponse) ([]byte, error) { | ||
return resp.MarshalJSON() | ||
} | ||
|
||
func (msgpackEncoder) marshalLogsResponse(resp plogotlp.ExportResponse) ([]byte, error) { | ||
return resp.MarshalJSON() | ||
} | ||
|
||
func (msgpackEncoder) marshalStatus(resp *spb.Status) ([]byte, error) { | ||
buf := new(bytes.Buffer) | ||
err := jsonPbMarshaler.Marshal(buf, resp) | ||
return buf.Bytes(), err | ||
} | ||
|
||
func (msgpackEncoder) contentType() string { | ||
return msgpackContentType | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,65 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package eventtime // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/libhoneyreceiver/internal/eventtime" | ||
|
||
import ( | ||
"math" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
func GetNowTime() time.Time { | ||
return time.Now() | ||
} | ||
|
||
func GetEventTime(etHeader string) time.Time { | ||
var eventTime time.Time | ||
if etHeader != "" { | ||
// Great, they sent us a time header. let's try and parse it. | ||
// RFC3339Nano is the default that we send from all our SDKs | ||
eventTime, _ = time.Parse(time.RFC3339Nano, etHeader) | ||
if eventTime.IsZero() { | ||
// the default didn't catch it, let's try a few other things | ||
// is it all numeric? then try unix epoch times | ||
epochInt, err := strconv.ParseInt(etHeader, 0, 64) | ||
if err == nil { | ||
// it might be seconds or it might be milliseconds! Who can know! | ||
// 10-digit numbers are seconds, 13-digit milliseconds, 16 microseconds | ||
if len(etHeader) == 10 { | ||
eventTime = time.Unix(epochInt, 0) | ||
} else if len(etHeader) > 10 { | ||
// turn it into seconds and fractional seconds | ||
fractionalTime := etHeader[:10] + "." + etHeader[10:] | ||
// then chop it into the int part and the fractional part | ||
if epochFloat, err := strconv.ParseFloat(fractionalTime, 64); err == nil { | ||
sec, dec := math.Modf(epochFloat) | ||
eventTime = time.Unix(int64(sec), int64(dec*(1e9))) | ||
} | ||
|
||
} | ||
} else { | ||
epochFloat, err := strconv.ParseFloat(etHeader, 64) | ||
if err == nil { | ||
sec, dec := math.Modf(epochFloat) | ||
eventTime = time.Unix(int64(sec), int64(dec*(1e9))) | ||
} | ||
} | ||
} | ||
} | ||
return eventTime.UTC() | ||
} | ||
|
||
func GetEventTimeSec(etHeader string) int64 { | ||
eventTime := GetEventTime(etHeader) | ||
return eventTime.Unix() | ||
} | ||
|
||
func GetEventTimeNano(etHeader string) int64 { | ||
eventTime := GetEventTime(etHeader) | ||
return eventTime.UnixNano() | ||
} | ||
|
||
func GetEventTimeDefaultString() string { | ||
return time.Now().Format(time.RFC3339Nano) | ||
} |
Oops, something went wrong.