Skip to content

Commit

Permalink
Revert "Collector example: add metrics and use official port (#4466)" (
Browse files Browse the repository at this point in the history
…#4487)

This reverts commit 6eedabf.
  • Loading branch information
MrAlias authored Sep 9, 2023
1 parent 9737995 commit e44ea5c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 88 deletions.
11 changes: 1 addition & 10 deletions example/otel-collector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ replace (

require (
go.opentelemetry.io/otel v1.17.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.40.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.17.0
go.opentelemetry.io/otel/metric v1.17.0
go.opentelemetry.io/otel/sdk v1.17.0
go.opentelemetry.io/otel/sdk/metric v0.40.0
go.opentelemetry.io/otel/trace v1.17.0
google.golang.org/grpc v1.57.0
)
Expand All @@ -24,8 +21,8 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.40.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.17.0 // indirect
go.opentelemetry.io/otel/metric v1.17.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.12.0 // indirect
Expand All @@ -42,9 +39,3 @@ replace go.opentelemetry.io/otel/exporters/otlp/otlptrace => ../../exporters/otl
replace go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc => ../../exporters/otlp/otlptrace/otlptracegrpc

replace go.opentelemetry.io/otel/metric => ../../metric

replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc => ../../exporters/otlp/otlpmetric/otlpmetricgrpc

replace go.opentelemetry.io/otel/exporters/otlp/otlpmetric => ../../exporters/otlp/otlpmetric

replace go.opentelemetry.io/otel/sdk/metric => ../../sdk/metric
94 changes: 16 additions & 78 deletions example/otel-collector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,37 @@ import (

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/propagation"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
"go.opentelemetry.io/otel/trace"
)

// Initialize a gRPC connection to be used by both the tracer and meter
// providers.
func initConn() (*grpc.ClientConn, error) {
// Initializes an OTLP exporter, and configures the corresponding trace and
// metric providers.
func initProvider() (func(context.Context) error, error) {
ctx := context.Background()

res, err := resource.New(ctx,
resource.WithAttributes(
// the service name used to display traces in backends
semconv.ServiceName("test-service"),
),
)
if err != nil {
return nil, fmt.Errorf("failed to create resource: %w", err)
}

// If the OpenTelemetry Collector is running on a local cluster (minikube or
// microk8s), it should be accessible through the NodePort service at the
// `localhost:4317` endpoint. Otherwise, replace `localhost` with the
// `localhost:30080` endpoint. Otherwise, replace `localhost` with the
// endpoint of your cluster. If you run the app inside k8s, then you can
// probably connect directly to the service through dns.
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
conn, err := grpc.DialContext(ctx, "localhost:4317",
conn, err := grpc.DialContext(ctx, "localhost:30080",
// Note the use of insecure transport here. TLS is recommended in production.
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
Expand All @@ -62,24 +69,6 @@ func initConn() (*grpc.ClientConn, error) {
return nil, fmt.Errorf("failed to create gRPC connection to collector: %w", err)
}

return conn, err
}

// Initializes an OTLP exporter, and configures the corresponding trace
// provider.
func initTracerProvider(conn *grpc.ClientConn) (func(context.Context) error, error) {
ctx := context.Background()

res, err := resource.New(ctx,
resource.WithAttributes(
// the service name used to display traces in backends
semconv.ServiceName("test-service"),
),
)
if err != nil {
return nil, fmt.Errorf("failed to create resource: %w", err)
}

// Set up a trace exporter
traceExporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithGRPCConn(conn))
if err != nil {
Expand All @@ -103,47 +92,13 @@ func initTracerProvider(conn *grpc.ClientConn) (func(context.Context) error, err
return tracerProvider.Shutdown, nil
}

// Initializes an OTLP exporter, and configures the corresponding meter
// provider.
func initMeterProvider(conn *grpc.ClientConn) (func(context.Context) error, error) {
ctx := context.Background()

res, err := resource.New(ctx,
resource.WithAttributes(
// the service name used to display traces in backends
semconv.ServiceName("test-service"),
),
)
if err != nil {
return nil, fmt.Errorf("failed to create resource: %w", err)
}

metricExporter, err := otlpmetricgrpc.New(ctx, otlpmetricgrpc.WithGRPCConn(conn))
if err != nil {
return nil, fmt.Errorf("failed to create metrics exporter: %w", err)
}

meterProvider := sdkmetric.NewMeterProvider(
sdkmetric.WithReader(sdkmetric.NewPeriodicReader(metricExporter)),
sdkmetric.WithResource(res),
)
otel.SetMeterProvider(meterProvider)

return meterProvider.Shutdown, nil
}

func main() {
log.Printf("Waiting for connection...")

ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()

conn, err := initConn()
if err != nil {
log.Fatal(err)
}

shutdown, err := initTracerProvider(conn)
shutdown, err := initProvider()
if err != nil {
log.Fatal(err)
}
Expand All @@ -153,18 +108,7 @@ func main() {
}
}()

shutdown, err = initMeterProvider(conn)
if err != nil {
log.Fatal(err)
}
defer func() {
if err := shutdown(ctx); err != nil {
log.Fatal("failed to shutdown MeterProvider: %w", err)
}
}()

tracer := otel.Tracer("test-tracer")
meter := otel.Meter("test-meter")

// Attributes represent additional key-value descriptors that can be bound
// to a metric observer or recorder.
Expand All @@ -174,11 +118,6 @@ func main() {
attribute.String("attrC", "vanilla"),
}

runCount, err := meter.Int64Counter("run", metric.WithDescription("The number of times the iteration ran"))
if err != nil {
log.Fatal(err)
}

// work begins
ctx, span := tracer.Start(
ctx,
Expand All @@ -187,7 +126,6 @@ func main() {
defer span.End()
for i := 0; i < 10; i++ {
_, iSpan := tracer.Start(ctx, fmt.Sprintf("Sample-%d", i))
runCount.Add(ctx, 1, metric.WithAttributes(commonAttrs...))
log.Printf("Doing really hard work (%d / 10)\n", i+1)

<-time.After(time.Second)
Expand Down

0 comments on commit e44ea5c

Please sign in to comment.