-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Change receiver shared component part #12203
Draft
bogdandrutu
wants to merge
1
commit into
open-telemetry:main
Choose a base branch
from
bogdandrutu:otlpreceiver
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+402
−375
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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,85 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package otlpreceiver // import "go.opentelemetry.io/collector/receiver/otlpreceiver" | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net" | ||
"sync" | ||
|
||
"go.uber.org/zap" | ||
"google.golang.org/grpc" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/component/componentstatus" | ||
"go.opentelemetry.io/collector/config/configgrpc" | ||
"go.opentelemetry.io/collector/internal/sharedcomponent" | ||
) | ||
|
||
type registerServerFunc func(srv *grpc.Server) | ||
|
||
type grpcComponent struct { | ||
cfg *configgrpc.ServerConfig | ||
serverGRPC *grpc.Server | ||
tel component.TelemetrySettings | ||
shutdownWG sync.WaitGroup | ||
servers []registerServerFunc | ||
} | ||
|
||
func newSharedGRPC(cfg *configgrpc.ServerConfig, tel component.TelemetrySettings) (*sharedcomponent.Component[*grpcComponent], error) { | ||
return grpcs.LoadOrStore( | ||
cfg, | ||
func() (*grpcComponent, error) { | ||
return &grpcComponent{cfg: cfg, tel: tel}, nil | ||
}, | ||
) | ||
} | ||
|
||
func (r *grpcComponent) Start(_ context.Context, host component.Host) error { | ||
var err error | ||
if r.serverGRPC, err = r.cfg.ToServer(context.Background(), host, r.tel); err != nil { | ||
return err | ||
} | ||
|
||
for _, registration := range r.servers { | ||
registration(r.serverGRPC) | ||
} | ||
|
||
r.tel.Logger.Info("Starting GRPC server", zap.String("endpoint", r.cfg.NetAddr.Endpoint)) | ||
var gln net.Listener | ||
if gln, err = r.cfg.NetAddr.Listen(context.Background()); err != nil { | ||
return err | ||
} | ||
|
||
r.shutdownWG.Add(1) | ||
go func() { | ||
defer r.shutdownWG.Done() | ||
if errGrpc := r.serverGRPC.Serve(gln); errGrpc != nil && !errors.Is(errGrpc, grpc.ErrServerStopped) { | ||
componentstatus.ReportStatus(host, componentstatus.NewFatalErrorEvent(errGrpc)) | ||
} | ||
}() | ||
return nil | ||
} | ||
|
||
// Shutdown is a method to turn off receiving. | ||
func (r *grpcComponent) Shutdown(context.Context) error { | ||
if r.serverGRPC != nil { | ||
r.serverGRPC.GracefulStop() | ||
} | ||
r.shutdownWG.Wait() | ||
return nil | ||
} | ||
|
||
func (r *grpcComponent) registerServer(sr registerServerFunc) { | ||
r.servers = append(r.servers, sr) | ||
} | ||
|
||
// This is the map of already created OTLP receivers for particular configurations. | ||
// We maintain this map because the receiver.Factory is asked trace and metric receivers separately | ||
// when it gets CreateTraces() and CreateMetrics() but they must not | ||
// create separate objects, they must use one otlpReceiver object per configuration. | ||
// When the receiver is shutdown it should be removed from this map so the same configuration | ||
// can be recreated successfully. | ||
var grpcs = sharedcomponent.NewMap[*configgrpc.ServerConfig, *grpcComponent]() |
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,90 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package otlpreceiver // import "go.opentelemetry.io/collector/receiver/otlpreceiver" | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net" | ||
"net/http" | ||
"sync" | ||
|
||
"go.uber.org/zap" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/component/componentstatus" | ||
"go.opentelemetry.io/collector/config/confighttp" | ||
"go.opentelemetry.io/collector/internal/sharedcomponent" | ||
) | ||
|
||
type handler struct { | ||
pattern string | ||
handlerFunc http.HandlerFunc | ||
} | ||
|
||
type httpComponent struct { | ||
cfg *confighttp.ServerConfig | ||
serverHTTP *http.Server | ||
tel component.TelemetrySettings | ||
shutdownWG sync.WaitGroup | ||
handlers []handler | ||
} | ||
|
||
func newSharedHTTP(cfg *confighttp.ServerConfig, tel component.TelemetrySettings) (*sharedcomponent.Component[*httpComponent], error) { | ||
return https.LoadOrStore( | ||
cfg, | ||
func() (*httpComponent, error) { | ||
return &httpComponent{cfg: cfg, tel: tel}, nil | ||
}, | ||
) | ||
} | ||
|
||
func (r *httpComponent) Start(ctx context.Context, host component.Host) error { | ||
httpMux := http.NewServeMux() | ||
for _, h := range r.handlers { | ||
httpMux.HandleFunc(h.pattern, h.handlerFunc) | ||
} | ||
|
||
var err error | ||
if r.serverHTTP, err = r.cfg.ToServer(ctx, host, r.tel, httpMux, confighttp.WithErrorHandler(errorHandler)); err != nil { | ||
return err | ||
} | ||
|
||
r.tel.Logger.Info("Starting HTTP server", zap.String("endpoint", r.cfg.Endpoint)) | ||
var hln net.Listener | ||
if hln, err = r.cfg.ToListener(ctx); err != nil { | ||
return err | ||
} | ||
|
||
r.shutdownWG.Add(1) | ||
go func() { | ||
defer r.shutdownWG.Done() | ||
if errHTTP := r.serverHTTP.Serve(hln); errHTTP != nil && !errors.Is(errHTTP, http.ErrServerClosed) { | ||
componentstatus.ReportStatus(host, componentstatus.NewFatalErrorEvent(errHTTP)) | ||
} | ||
}() | ||
return nil | ||
} | ||
|
||
// Shutdown is a method to turn off receiving. | ||
func (r *httpComponent) Shutdown(ctx context.Context) error { | ||
var err error | ||
if r.serverHTTP != nil { | ||
err = r.serverHTTP.Shutdown(ctx) | ||
} | ||
r.shutdownWG.Wait() | ||
return err | ||
} | ||
|
||
func (r *httpComponent) registerHandler(pattern string, handlerFunc http.HandlerFunc) { | ||
r.handlers = append(r.handlers, handler{pattern: pattern, handlerFunc: handlerFunc}) | ||
} | ||
|
||
// This is the map of already created OTLP receivers for particular configurations. | ||
// We maintain this map because the receiver.Factory is asked trace and metric receivers separately | ||
// when it gets CreateTraces() and CreateMetrics() but they must not | ||
// create separate objects, they must use one otlpReceiver object per configuration. | ||
// When the receiver is shutdown it should be removed from this map so the same configuration | ||
// can be recreated successfully. | ||
var https = sharedcomponent.NewMap[*confighttp.ServerConfig, *httpComponent]() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you pass in
tel
here, I assume you want it to not have anotel.signal
attribute on the logger, etc?This still leaves us in a situation where it needs to be present sometimes and not others. What's the mechanism for managing this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, when the server encounters an error, the metrics handler will have to use the signal-agnostic logger anyways, so doesn't it then have to use
zap.String(...)
anyways to append the signal attribute?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My hope is that, now that we know a component has to create different "types" of sub-component, the mechanism we design will allow that. Very important is that the "main" component still has all the "attributes".
The easiest way I can think of is via the TelemetrySettings we provide, we can have there a mechanism of retrieving the Logger via some functions instead of direct access to 1 variable.