Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add OTLP handler to send events using OpenTelemetry #290

Merged
merged 19 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ type Config struct {

// The list of HTTP headers to extract from a HTTP request/response.
HTTPHeadersToExtract []string

// Event Handler type to use for sending events.
EventHandlerType string
}

// NewConfig returns a new Config struct.
Expand Down Expand Up @@ -149,6 +152,7 @@ func NewConfig() Config {
AdditionalAttributes: utils.LookupEnvAsStringMap("ADDITIONAL_ATTRIBUTES"),
MikeGoldsmith marked this conversation as resolved.
Show resolved Hide resolved
IncludeRequestURL: utils.LookupEnvOrBool("INCLUDE_REQUEST_URL", true),
HTTPHeadersToExtract: getHTTPHeadersToExtract(),
EventHandlerType: utils.LookupEnvOrString("HANDLER_TYPE", "libhoney"),
}
}

Expand Down Expand Up @@ -246,7 +250,7 @@ var defaultHeadersToExtract = []string{
}

// getHTTPHeadersToExtract returns the list of HTTP headers to extract from a HTTP request/response
// based on a user-defined list in HTTP_HEADERS, or the default headers if no list is given.
// based on a user-defined list in HTTP_HEADERS, or the default headers if no list is given.
func getHTTPHeadersToExtract() []string {
if headers, found := utils.LookupEnvAsStringSlice("HTTP_HEADERS"); found {
return headers
Expand Down
39 changes: 37 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,46 @@ require (
github.com/rs/zerolog v1.31.0
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/otel v1.19.0
go.opentelemetry.io/otel/trace v1.19.0
k8s.io/api v0.28.2
k8s.io/apimachinery v0.28.2
k8s.io/client-go v0.28.2
)

require (
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
github.com/honeycombio/otel-config-go v1.12.1
github.com/lufia/plan9stats v0.0.0-20230326075908-cb1d2100619a // indirect
github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b // indirect
github.com/sethvargo/go-envconfig v0.9.0 // indirect
github.com/shirou/gopsutil/v3 v3.23.8 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.opentelemetry.io/contrib/instrumentation/host v0.44.0 // indirect
go.opentelemetry.io/contrib/instrumentation/runtime v0.44.0 // indirect
go.opentelemetry.io/contrib/propagators/b3 v1.19.0 // indirect
go.opentelemetry.io/contrib/propagators/ot v1.19.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.41.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.41.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v0.41.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.18.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.18.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.18.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/otel/sdk v1.18.0 // indirect
go.opentelemetry.io/otel/sdk/metric v0.41.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/grpc v1.58.1 // indirect
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
Expand Down Expand Up @@ -46,13 +81,13 @@ require (
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.30.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/alexcesaro/statsd.v2 v2.0.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
90 changes: 86 additions & 4 deletions go.sum

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions handlers/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"sync"

"github.com/honeycombio/honeycomb-network-agent/assemblers"
"github.com/honeycombio/honeycomb-network-agent/config"
"github.com/honeycombio/honeycomb-network-agent/utils"
)

// EventHandler is an interface for event handlers
Expand All @@ -13,3 +15,15 @@ type EventHandler interface {
Close()
handleEvent(event assemblers.Event)
}

// NewEventHandler returns an event handler based on the config's selected handler type.
func NewEventHandler(config config.Config, cachedK8sClient *utils.CachedK8sClient, eventsChannel chan assemblers.Event, version string) EventHandler {
var eventHandler EventHandler
switch config.EventHandlerType {
case "libhoney":
MikeGoldsmith marked this conversation as resolved.
Show resolved Hide resolved
eventHandler = NewLibhoneyEventHandler(config, cachedK8sClient, eventsChannel, version)
case "otel":
eventHandler = NewOtelHandler(config, cachedK8sClient, eventsChannel, version)
}
return eventHandler
}
6 changes: 4 additions & 2 deletions handlers/libhoney_event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type libhoneyEventHandler struct {
eventsChan chan assemblers.Event
}

var _ EventHandler = (*libhoneyEventHandler)(nil)

// NewLibhoneyEventHandler creates a new event handler that sends events using libhoney
func NewLibhoneyEventHandler(config config.Config, k8sClient *utils.CachedK8sClient, eventsChan chan assemblers.Event, version string) EventHandler {
initLibhoney(config, version)
Expand Down Expand Up @@ -96,7 +98,7 @@ func (handler *libhoneyEventHandler) handleEvent(event assemblers.Event) {
// the telemetry event to send
var ev *libhoney.Event = libhoney.NewEvent()

setTimestampsAndDurationIfValid(ev, event)
handler.setTimestampsAndDurationIfValid(ev, event)

ev.AddField("meta.stream.ident", event.StreamIdent())
ev.AddField("meta.seqack", event.RequestId())
Expand Down Expand Up @@ -133,7 +135,7 @@ func (handler *libhoneyEventHandler) handleEvent(event assemblers.Event) {
//
// It only sets timestamps if they are present in the captured event, and only
// computes and includes durations for which there are correct timestamps to based them upon.
func setTimestampsAndDurationIfValid(honeyEvent *libhoney.Event, event assemblers.Event) {
func (handler *libhoneyEventHandler) setTimestampsAndDurationIfValid(honeyEvent *libhoney.Event, event assemblers.Event) {
honeyEvent.AddField("meta.event_handled_at", time.Now())
switch {
case event.RequestTimestamp().IsZero() && event.ResponseTimestamp().IsZero():
Expand Down
11 changes: 6 additions & 5 deletions handlers/libhoney_event_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ func Test_libhoneyEventHandler_handleEvent(t *testing.T) {
"source.k8s.resource.type": "pod",
"source.k8s.namespace.name": "unit-tests",
"source.k8s.pod.name": "src-pod",
"source.k8s.pod.uid": srcPod.UID,
"source.k8s.pod.uid": string(srcPod.UID),
"destination.k8s.resource.type": "pod",
"destination.k8s.namespace.name": "unit-tests",
"destination.k8s.pod.name": "dest-pod",
"destination.k8s.pod.uid": destPod.UID,
"destination.k8s.pod.uid": string(destPod.UID),
}

assert.Equal(t, expectedAttrs, attrs)
Expand Down Expand Up @@ -262,11 +262,11 @@ func Test_libhoneyEventHandler_handleEvent_routed_to_service(t *testing.T) {
"source.k8s.resource.type": "pod",
"source.k8s.namespace.name": "unit-tests",
"source.k8s.pod.name": "src-pod",
"source.k8s.pod.uid": srcPod.UID,
"source.k8s.pod.uid": string(srcPod.UID),
"destination.k8s.resource.type": "service",
"destination.k8s.namespace.name": "unit-tests",
"destination.k8s.service.name": "dest-service",
"destination.k8s.service.uid": destService.UID,
"destination.k8s.service.uid": string(destService.UID),
}

assert.Equal(t, expectedAttrs, attrs)
Expand Down Expand Up @@ -323,12 +323,13 @@ func Test_reportingTimesAndDurations(t *testing.T) {
expectedTelemetryTime: nowish,
},
}
handler := &libhoneyEventHandler{}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
ev := libhoney.NewEvent()
event := createTestHttpEvent(tC.reqTime, tC.respTime)

setTimestampsAndDurationIfValid(ev, event)
handler.setTimestampsAndDurationIfValid(ev, event)

if tC.expectedTelemetryTime != nowish {
assert.Equal(t, tC.expectedTelemetryTime, ev.Timestamp)
Expand Down