Skip to content

Commit

Permalink
TT-1303 INFOPLAT-1372 Add support for OTLP/HTTP exporters for beholde…
Browse files Browse the repository at this point in the history
…r sdk (#830)

* TT-1303 Add support for OTLP/HTTP exporters

* TT-1303 Add support for OTLP/HTTP exporters: enable case when InsecureConnection:false and CACertFile is not set

* insecureskipverify true

* revert insecureSkipVerify true

* Consolidate NewGRPCClient, NewHTTPClient into single constructor

* Return nil on error from Client constructors

* Add comment for used/unused context

---------

Co-authored-by: gheorghestrimtu <studentcuza@gmail.com>
Co-authored-by: 4of9 <177086174+4of9@users.noreply.github.com>
Co-authored-by: Geert G <117188496+cll-gg@users.noreply.github.com>
Co-authored-by: Clement <clement.erena78@gmail.com>
  • Loading branch information
5 people authored Oct 21, 2024
1 parent 32bc8c1 commit 4b45ad1
Show file tree
Hide file tree
Showing 8 changed files with 372 additions and 54 deletions.
25 changes: 14 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,29 @@ require (
github.com/smartcontractkit/libocr v0.0.0-20241007185508-adbe57025f12
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.52.0
go.opentelemetry.io/otel v1.28.0
go.opentelemetry.io/otel v1.30.0
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.0.0-20240823153156-2a54df7bffb9
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.6.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.28.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.30.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.28.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.30.0
go.opentelemetry.io/otel/exporters/stdout/stdoutlog v0.4.0
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.28.0
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.28.0
go.opentelemetry.io/otel/log v0.4.0
go.opentelemetry.io/otel/metric v1.28.0
go.opentelemetry.io/otel/sdk v1.28.0
go.opentelemetry.io/otel/sdk/log v0.4.0
go.opentelemetry.io/otel/sdk/metric v1.28.0
go.opentelemetry.io/otel/trace v1.28.0
go.opentelemetry.io/otel/log v0.6.0
go.opentelemetry.io/otel/metric v1.30.0
go.opentelemetry.io/otel/sdk v1.30.0
go.opentelemetry.io/otel/sdk/log v0.6.0
go.opentelemetry.io/otel/sdk/metric v1.30.0
go.opentelemetry.io/otel/trace v1.30.0
go.uber.org/goleak v1.3.0
go.uber.org/zap v1.27.0
golang.org/x/crypto v0.27.0
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0
golang.org/x/tools v0.25.0
gonum.org/v1/gonum v0.15.0
google.golang.org/grpc v1.65.0
google.golang.org/grpc v1.66.1
google.golang.org/protobuf v1.34.2
sigs.k8s.io/yaml v1.4.0
)
Expand Down Expand Up @@ -95,7 +98,7 @@ require (
github.com/stretchr/objx v0.5.2 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.30.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/mod v0.21.0 // indirect
Expand All @@ -104,7 +107,7 @@ require (
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240822170219-fc7c04adadcd // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240822170219-fc7c04adadcd // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
50 changes: 28 additions & 22 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -253,34 +253,40 @@ go.opencensus.io v0.23.0 h1:gqCw0LfLxScz8irSi8exQc7fyQ0fKQU/qnC/X8+V/1M=
go.opencensus.io v0.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.52.0 h1:vS1Ao/R55RNV4O7TA2Qopok8yN+X0LIP6RVWLFkprck=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.52.0/go.mod h1:BMsdeOxN04K0L5FNUBfjFdvwWGNe/rkmSwH4Aelu/X0=
go.opentelemetry.io/otel v1.28.0 h1:/SqNcYk+idO0CxKEUOtKQClMK/MimZihKYMruSMViUo=
go.opentelemetry.io/otel v1.28.0/go.mod h1:q68ijF8Fc8CnMHKyzqL6akLO46ePnjkgfIMIjUIX9z4=
go.opentelemetry.io/otel v1.30.0 h1:F2t8sK4qf1fAmY9ua4ohFS/K+FUuOPemHUIXHtktrts=
go.opentelemetry.io/otel v1.30.0/go.mod h1:tFw4Br9b7fOS+uEao81PJjVMjW/5fvNCbpsDIXqP0pc=
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.0.0-20240823153156-2a54df7bffb9 h1:UiRNKd1OgqsLbFwE+wkAWTdiAxXtCBqKIHeBIse4FUA=
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.0.0-20240823153156-2a54df7bffb9/go.mod h1:eqZlW3pJWhjyexnDPrdQxix1pn0wwhI4AO4GKpP/bMI=
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.6.0 h1:QSKmLBzbFULSyHzOdO9JsN9lpE4zkrz1byYGmJecdVE=
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.6.0/go.mod h1:sTQ/NH8Yrirf0sJ5rWqVu+oT82i4zL9FaF6rWcqnptM=
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.28.0 h1:U2guen0GhqH8o/G2un8f/aG/y++OuW6MyCo6hT9prXk=
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.28.0/go.mod h1:yeGZANgEcpdx/WK0IvvRFC+2oLiMS2u4L/0Rj2M2Qr0=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0 h1:3Q/xZUyC1BBkualc9ROb4G8qkH90LXEIICcs5zv1OYY=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0/go.mod h1:s75jGIWA9OfCMzF0xr+ZgfrB5FEbbV7UuYo32ahUiFI=
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.30.0 h1:VrMAbeJz4gnVDg2zEzjHG4dEH86j4jO6VYB+NgtGD8s=
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.30.0/go.mod h1:qqN/uFdpeitTvm+JDqqnjm517pmQRYxTORbETHq5tOc=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.30.0 h1:lsInsfvhVIfOI6qHVyysXMNDnjO9Npvl7tlDPJFBVd4=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.30.0/go.mod h1:KQsVNh4OjgjTG0G6EiNi1jVpnaeeKsKMRwbLN+f1+8M=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.28.0 h1:R3X6ZXmNPRR8ul6i3WgFURCHzaXjHdm0karRG/+dj3s=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.28.0/go.mod h1:QWFXnDavXWwMx2EEcZsf3yxgEKAqsxQ+Syjp+seyInw=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.30.0 h1:umZgi92IyxfXd/l4kaDhnKgY8rnN/cZcF1LKc6I8OQ8=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.30.0/go.mod h1:4lVs6obhSVRb1EW5FhOuBTyiQhtRtAnnva9vD3yRfq8=
go.opentelemetry.io/otel/exporters/stdout/stdoutlog v0.4.0 h1:0MH3f8lZrflbUWXVxyBg/zviDFdGE062uKh5+fu8Vv0=
go.opentelemetry.io/otel/exporters/stdout/stdoutlog v0.4.0/go.mod h1:Vh68vYiHY5mPdekTr0ox0sALsqjoVy0w3Os278yX5SQ=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.28.0 h1:BJee2iLkfRfl9lc7aFmBwkWxY/RI1RDdXepSF6y8TPE=
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.28.0/go.mod h1:DIzlHs3DRscCIBU3Y9YSzPfScwnYnzfnCd4g8zA7bZc=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.28.0 h1:EVSnY9JbEEW92bEkIYOVMw4q1WJxIAGoFTrtYOzWuRQ=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.28.0/go.mod h1:Ea1N1QQryNXpCD0I1fdLibBAIpQuBkznMmkdKrapk1Y=
go.opentelemetry.io/otel/log v0.4.0 h1:/vZ+3Utqh18e8TPjuc3ecg284078KWrR8BRz+PQAj3o=
go.opentelemetry.io/otel/log v0.4.0/go.mod h1:DhGnQvky7pHy82MIRV43iXh3FlKN8UUKftn0KbLOq6I=
go.opentelemetry.io/otel/metric v1.28.0 h1:f0HGvSl1KRAU1DLgLGFjrwVyismPlnuU6JD6bOeuA5Q=
go.opentelemetry.io/otel/metric v1.28.0/go.mod h1:Fb1eVBFZmLVTMb6PPohq3TO9IIhUisDsbJoL/+uQW4s=
go.opentelemetry.io/otel/sdk v1.28.0 h1:b9d7hIry8yZsgtbmM0DKyPWMMUMlK9NEKuIG4aBqWyE=
go.opentelemetry.io/otel/sdk v1.28.0/go.mod h1:oYj7ClPUA7Iw3m+r7GeEjz0qckQRJK2B8zjcZEfu7Pg=
go.opentelemetry.io/otel/sdk/log v0.4.0 h1:1mMI22L82zLqf6KtkjrRy5BbagOTWdJsqMY/HSqILAA=
go.opentelemetry.io/otel/sdk/log v0.4.0/go.mod h1:AYJ9FVF0hNOgAVzUG/ybg/QttnXhUePWAupmCqtdESo=
go.opentelemetry.io/otel/sdk/metric v1.28.0 h1:OkuaKgKrgAbYrrY0t92c+cC+2F6hsFNnCQArXCKlg08=
go.opentelemetry.io/otel/sdk/metric v1.28.0/go.mod h1:cWPjykihLAPvXKi4iZc1dpER3Jdq2Z0YLse3moQUCpg=
go.opentelemetry.io/otel/trace v1.28.0 h1:GhQ9cUuQGmNDd5BTCP2dAvv75RdMxEfTmYejp+lkx9g=
go.opentelemetry.io/otel/trace v1.28.0/go.mod h1:jPyXzNPg6da9+38HEwElrQiHlVMTnVfM3/yv2OlIHaI=
go.opentelemetry.io/otel/log v0.6.0 h1:nH66tr+dmEgW5y+F9LanGJUBYPrRgP4g2EkmPE3LeK8=
go.opentelemetry.io/otel/log v0.6.0/go.mod h1:KdySypjQHhP069JX0z/t26VHwa8vSwzgaKmXtIB3fJM=
go.opentelemetry.io/otel/metric v1.30.0 h1:4xNulvn9gjzo4hjg+wzIKG7iNFEaBMX00Qd4QIZs7+w=
go.opentelemetry.io/otel/metric v1.30.0/go.mod h1:aXTfST94tswhWEb+5QjlSqG+cZlmyXy/u8jFpor3WqQ=
go.opentelemetry.io/otel/sdk v1.30.0 h1:cHdik6irO49R5IysVhdn8oaiR9m8XluDaJAs4DfOrYE=
go.opentelemetry.io/otel/sdk v1.30.0/go.mod h1:p14X4Ok8S+sygzblytT1nqG98QG2KYKv++HE0LY/mhg=
go.opentelemetry.io/otel/sdk/log v0.6.0 h1:4J8BwXY4EeDE9Mowg+CyhWVBhTSLXVXodiXxS/+PGqI=
go.opentelemetry.io/otel/sdk/log v0.6.0/go.mod h1:L1DN8RMAduKkrwRAFDEX3E3TLOq46+XMGSbUfHU/+vE=
go.opentelemetry.io/otel/sdk/metric v1.30.0 h1:QJLT8Pe11jyHBHfSAgYH7kEmT24eX792jZO1bo4BXkM=
go.opentelemetry.io/otel/sdk/metric v1.30.0/go.mod h1:waS6P3YqFNzeP01kuo/MBBYqaoBJl7efRQHOaydhy1Y=
go.opentelemetry.io/otel/trace v1.30.0 h1:7UBkkYzeg3C7kQX8VAidWh2biiQbtAKjyIML8dQ9wmc=
go.opentelemetry.io/otel/trace v1.30.0/go.mod h1:5EyKqTzzmyqB9bwtCCq6pDLktPK6fmGf/Dph+8VI02o=
go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeXrui0=
go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
Expand Down Expand Up @@ -376,17 +382,17 @@ google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoA
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
google.golang.org/genproto v0.0.0-20210401141331-865547bb08e2/go.mod h1:9lPAdzaEmUacj36I+k7YKbEc5CXzPIeORRgDAUOu28A=
google.golang.org/genproto/googleapis/api v0.0.0-20240822170219-fc7c04adadcd h1:BBOTEWLuuEGQy9n1y9MhVJ9Qt0BDu21X8qZs71/uPZo=
google.golang.org/genproto/googleapis/api v0.0.0-20240822170219-fc7c04adadcd/go.mod h1:fO8wJzT2zbQbAjbIoos1285VfEIYKDDY+Dt+WpTkh6g=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240822170219-fc7c04adadcd h1:6TEm2ZxXoQmFWFlt1vNxvVOa1Q0dXFQD1m/rYjXmS0E=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240822170219-fc7c04adadcd/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU=
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1 h1:hjSy6tcFQZ171igDaN5QHOw2n6vx40juYbC/x67CEhc=
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1/go.mod h1:qpvKtACPCQhAdu3PyQgV4l3LMXZEtft7y8QcarRsp9I=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 h1:pPJltXNxVzT4pK9yD8vR9X75DaWYYmLGMsEvBfFQZzQ=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
google.golang.org/grpc v1.36.1/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU=
google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc=
google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ=
google.golang.org/grpc v1.66.1 h1:hO5qAXR19+/Z44hmvIM4dQFMSYX9XcWsByfoxutBpAM=
google.golang.org/grpc v1.66.1/go.mod h1:s3/l6xSSCURdVfAnL+TqCNMyTDAGN6+lZeVxnZR128Y=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
Expand Down
36 changes: 26 additions & 10 deletions pkg/beholder/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
otellog "go.opentelemetry.io/otel/log"
Expand Down Expand Up @@ -53,35 +54,48 @@ type Client struct {
// NewClient creates a new Client with initialized OpenTelemetry components
// To handle OpenTelemetry errors use [otel.SetErrorHandler](https://pkg.go.dev/go.opentelemetry.io/otel#SetErrorHandler)
func NewClient(cfg Config) (*Client, error) {
if cfg.OtelExporterGRPCEndpoint != "" && cfg.OtelExporterHTTPEndpoint != "" {
return nil, errors.New("only one exporter endpoint should be set")
}
if cfg.OtelExporterGRPCEndpoint == "" && cfg.OtelExporterHTTPEndpoint == "" {
return nil, errors.New("at least one exporter endpoint should be set")
}
if cfg.OtelExporterHTTPEndpoint != "" {
factory := func(options ...otlploghttp.Option) (sdklog.Exporter, error) {
// note: context is unused internally
return otlploghttp.New(context.Background(), options...) //nolint
}
return newHTTPClient(cfg, factory)
}

factory := func(options ...otlploggrpc.Option) (sdklog.Exporter, error) {
// note: context is unused internally
return otlploggrpc.New(context.Background(), options...) //nolint
}
return newClient(cfg, factory)
return newGRPCClient(cfg, factory)
}

// Used for testing to override the default exporter
type otlploggrpcFactory func(options ...otlploggrpc.Option) (sdklog.Exporter, error)

func newClient(cfg Config, otlploggrpcNew otlploggrpcFactory) (*Client, error) {
func newGRPCClient(cfg Config, otlploggrpcNew otlploggrpcFactory) (*Client, error) {
baseResource, err := newOtelResource(cfg)
noop := NewNoopClient()
if err != nil {
return noop, err
return nil, err
}
creds := insecure.NewCredentials()
if !cfg.InsecureConnection && cfg.CACertFile != "" {
creds, err = credentials.NewClientTLSFromFile(cfg.CACertFile, "")
if err != nil {
return noop, err
return nil, err
}
}
sharedLogExporter, err := otlploggrpcNew(
otlploggrpc.WithTLSCredentials(creds),
otlploggrpc.WithEndpoint(cfg.OtelExporterGRPCEndpoint),
)
if err != nil {
return noop, err
return nil, err
}

// Logger
Expand All @@ -102,7 +116,7 @@ func newClient(cfg Config, otlploggrpcNew otlploggrpcFactory) (*Client, error) {
baseResource,
)
if err != nil {
return noop, err
return nil, err
}
loggerProvider := sdklog.NewLoggerProvider(
sdklog.WithResource(loggerResource),
Expand All @@ -113,14 +127,14 @@ func newClient(cfg Config, otlploggrpcNew otlploggrpcFactory) (*Client, error) {
// Tracer
tracerProvider, err := newTracerProvider(cfg, baseResource, creds)
if err != nil {
return noop, err
return nil, err
}
tracer := tracerProvider.Tracer(defaultPackageName)

// Meter
meterProvider, err := newMeterProvider(cfg, baseResource, creds)
if err != nil {
return noop, err
return nil, err
}
meter := meterProvider.Meter(defaultPackageName)

Expand All @@ -143,7 +157,7 @@ func newClient(cfg Config, otlploggrpcNew otlploggrpcFactory) (*Client, error) {
baseResource,
)
if err != nil {
return noop, err
return nil, err
}

messageLoggerProvider := sdklog.NewLoggerProvider(
Expand Down Expand Up @@ -247,6 +261,7 @@ type shutdowner interface {
func newTracerProvider(config Config, resource *sdkresource.Resource, creds credentials.TransportCredentials) (*sdktrace.TracerProvider, error) {
ctx := context.Background()

// note: context is used internally
exporter, err := otlptracegrpc.New(ctx,
otlptracegrpc.WithTLSCredentials(creds),
otlptracegrpc.WithEndpoint(config.OtelExporterGRPCEndpoint),
Expand All @@ -273,6 +288,7 @@ func newTracerProvider(config Config, resource *sdkresource.Resource, creds cred
func newMeterProvider(config Config, resource *sdkresource.Resource, creds credentials.TransportCredentials) (*sdkmetric.MeterProvider, error) {
ctx := context.Background()

// note: context is unused internally
exporter, err := otlpmetricgrpc.New(
ctx,
otlpmetricgrpc.WithTLSCredentials(creds),
Expand Down
92 changes: 82 additions & 10 deletions pkg/beholder/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/mock"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp"
otellog "go.opentelemetry.io/otel/log"
sdklog "go.opentelemetry.io/otel/sdk/log"

Expand Down Expand Up @@ -54,6 +55,30 @@ func TestClient(t *testing.T) {
}
defaultMessageBody := []byte("body bytes")

mustNewGRPCClient := func(t *testing.T, exporterMock *mocks.OTLPExporter) *Client {
// Override exporter factory which is used by Client
exporterFactory := func(...otlploggrpc.Option) (sdklog.Exporter, error) {
return exporterMock, nil
}
client, err := newGRPCClient(TestDefaultConfig(), exporterFactory)
if err != nil {
t.Fatalf("Error creating beholder client: %v", err)
}
return client
}

mustNewHTTPClient := func(t *testing.T, exporterMock *mocks.OTLPExporter) *Client {
// Override exporter factory which is used by Client
exporterFactory := func(...otlploghttp.Option) (sdklog.Exporter, error) {
return exporterMock, nil
}
client, err := newHTTPClient(TestDefaultConfigHTTPClient(), exporterFactory)
if err != nil {
t.Fatalf("Error creating beholder client: %v", err)
}
return client
}

testCases := []struct {
name string
makeCustomAttributes func() map[string]any
Expand All @@ -62,9 +87,10 @@ func TestClient(t *testing.T) {
exporterMockErrorCount int
exporterOutputExpected bool
messageGenerator func(client *Client, messageBody []byte, customAttributes map[string]any)
mustNewGrpcClient func(*testing.T, *mocks.OTLPExporter) *Client
}{
{
name: "Test Emit",
name: "Test Emit (GRPC Client)",
makeCustomAttributes: defaultCustomAttributes,
messageBody: defaultMessageBody,
messageCount: 10,
Expand All @@ -74,6 +100,21 @@ func TestClient(t *testing.T) {
err := client.Emitter.Emit(tests.Context(t), messageBody, customAttributes)
assert.NoError(t, err)
},
mustNewGrpcClient: mustNewGRPCClient,
},

{
name: "Test Emit (HTTP Client)",
makeCustomAttributes: defaultCustomAttributes,
messageBody: defaultMessageBody,
messageCount: 10,
exporterMockErrorCount: 0,
exporterOutputExpected: true,
messageGenerator: func(client *Client, messageBody []byte, customAttributes map[string]any) {
err := client.Emitter.Emit(tests.Context(t), messageBody, customAttributes)
assert.NoError(t, err)
},
mustNewGrpcClient: mustNewHTTPClient,
},
}

Expand All @@ -82,14 +123,8 @@ func TestClient(t *testing.T) {
exporterMock := mocks.NewOTLPExporter(t)
defer exporterMock.AssertExpectations(t)

// Override exporter factory which is used by Client
exporterFactory := func(...otlploggrpc.Option) (sdklog.Exporter, error) {
return exporterMock, nil
}
client, err := newClient(TestDefaultConfig(), exporterFactory)
if err != nil {
t.Fatalf("Error creating beholder client: %v", err)
}
client := tc.mustNewGrpcClient(t, exporterMock)

otel.SetErrorHandler(otelMustNotErr(t))
// Number of exported messages
exportedMessageCount := 0
Expand Down Expand Up @@ -138,7 +173,7 @@ func TestClient(t *testing.T) {

func TestEmitterMessageValidation(t *testing.T) {
getEmitter := func(exporterMock *mocks.OTLPExporter) Emitter {
client, err := newClient(
client, err := newGRPCClient(
TestDefaultConfig(),
// Override exporter factory which is used by Client
func(...otlploggrpc.Option) (sdklog.Exporter, error) {
Expand Down Expand Up @@ -252,3 +287,40 @@ func TestClient_ForPackage(t *testing.T) {
func otelMustNotErr(t *testing.T) otel.ErrorHandlerFunc {
return func(err error) { t.Fatalf("otel error: %v", err) }
}

func TestNewClient(t *testing.T) {
t.Run("both endpoints set", func(t *testing.T) {
client, err := NewClient(Config{
OtelExporterGRPCEndpoint: "grpc-endpoint",
OtelExporterHTTPEndpoint: "http-endpoint",
})
assert.Error(t, err)
assert.Nil(t, client)
assert.Equal(t, "only one exporter endpoint should be set", err.Error())
})

t.Run("no endpoints set", func(t *testing.T) {
client, err := NewClient(Config{})
assert.Error(t, err)
assert.Nil(t, client)
assert.Equal(t, "at least one exporter endpoint should be set", err.Error())
})

t.Run("GRPC endpoint set", func(t *testing.T) {
client, err := NewClient(Config{
OtelExporterGRPCEndpoint: "grpc-endpoint",
})
assert.NoError(t, err)
assert.NotNil(t, client)
assert.IsType(t, &Client{}, client)
})

t.Run("HTTP endpoint set", func(t *testing.T) {
client, err := NewClient(Config{
OtelExporterHTTPEndpoint: "http-endpoint",
})
assert.NoError(t, err)
assert.NotNil(t, client)
assert.IsType(t, &Client{}, client)
})
}
Loading

0 comments on commit 4b45ad1

Please sign in to comment.