github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/x/opentelemetry/config_test.go (about)

     1  // Copyright (c) 2021 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package opentelemetry
    22  
    23  import (
    24  	"context"
    25  	"net"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/require"
    29  	"github.com/uber-go/tally"
    30  	"go.opentelemetry.io/collector/component"
    31  	"go.opentelemetry.io/collector/component/componenttest"
    32  	"go.opentelemetry.io/collector/consumer"
    33  	"go.opentelemetry.io/collector/consumer/consumertest"
    34  	"go.opentelemetry.io/collector/receiver/otlpreceiver"
    35  )
    36  
    37  func TestConfiguration(t *testing.T) {
    38  	ctx := context.Background()
    39  	addr := localAddress(t)
    40  	r := grpcReceiver(t, "receiver", addr, consumertest.NewNop(), consumertest.NewNop())
    41  	require.NotNil(t, r)
    42  	require.NoError(t, r.Start(ctx, componenttest.NewNopHost()))
    43  	defer func() {
    44  		require.NoError(t, r.Shutdown(ctx))
    45  	}()
    46  
    47  	cfg := Configuration{
    48  		ServiceName: "foo",
    49  		Endpoint:    addr,
    50  		Insecure:    true,
    51  		Attributes:  map[string]string{"bar": "baz"},
    52  	}
    53  
    54  	tracerProvider, err := cfg.NewTracerProvider(ctx, tally.NoopScope,
    55  		TracerProviderOptions{})
    56  	require.NoError(t, err)
    57  	require.NotNil(t, tracerProvider)
    58  }
    59  
    60  func grpcReceiver(
    61  	t *testing.T,
    62  	name, endpoint string,
    63  	tc consumer.Traces,
    64  	mc consumer.Metrics,
    65  ) component.Component {
    66  	factory := otlpreceiver.NewFactory()
    67  	cfg := factory.CreateDefaultConfig().(*otlpreceiver.Config)
    68  	cfg.SetIDName(name)
    69  	cfg.GRPC.NetAddr.Endpoint = endpoint
    70  	cfg.HTTP = nil
    71  
    72  	var (
    73  		set = componenttest.NewNopReceiverCreateSettings()
    74  		r   component.Component
    75  		err error
    76  	)
    77  	if tc != nil {
    78  		r, err = factory.CreateTracesReceiver(context.Background(), set, cfg, tc)
    79  		require.NoError(t, err)
    80  	}
    81  	if mc != nil {
    82  		r, err = factory.CreateMetricsReceiver(context.Background(), set, cfg, mc)
    83  		require.NoError(t, err)
    84  	}
    85  	return r
    86  }
    87  
    88  func localAddress(t *testing.T) string {
    89  	ln, err := net.Listen("tcp", "localhost:0")
    90  	require.NoError(t, err)
    91  	defer func() { _ = ln.Close() }()
    92  	return ln.Addr().String()
    93  }