Skip to content

Commit

Permalink
chore: add doc comment to server API (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
haoqixu authored Dec 1, 2023
1 parent f0e220d commit fc9dfe6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
10 changes: 10 additions & 0 deletions server/callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@ import (
"github.com/open-telemetry/opamp-go/server/types"
)

// CallbacksStruct is a struct that implements Callbacks interface and allows
// to override only the methods that are needed. If a method is not overridden then it will
// accept all connections.
type CallbacksStruct struct {
OnConnectingFunc func(request *http.Request) types.ConnectionResponse
}

var _ types.Callbacks = (*CallbacksStruct)(nil)

// OnConnecting implements Callbacks.interface.
func (c CallbacksStruct) OnConnecting(request *http.Request) types.ConnectionResponse {
if c.OnConnectingFunc != nil {
return c.OnConnectingFunc(request)
}
return types.ConnectionResponse{Accept: true}
}

// ConnectionCallbacksStruct is a struct that implements ConnectionCallbacks interface and allows
// to override only the methods that are needed.
type ConnectionCallbacksStruct struct {
OnConnectedFunc func(conn types.Connection)
OnMessageFunc func(conn types.Connection, message *protobufs.AgentToServer) *protobufs.ServerToAgent
Expand All @@ -28,12 +34,15 @@ type ConnectionCallbacksStruct struct {

var _ types.ConnectionCallbacks = (*ConnectionCallbacksStruct)(nil)

// OnConnected implements ConnectionCallbacks.OnConnected.
func (c ConnectionCallbacksStruct) OnConnected(conn types.Connection) {
if c.OnConnectedFunc != nil {
c.OnConnectedFunc(conn)
}
}

// OnMessage implements ConnectionCallbacks.OnMessage.
// If OnMessageFunc is nil then it will send an empty response to the agent
func (c ConnectionCallbacksStruct) OnMessage(conn types.Connection, message *protobufs.AgentToServer) *protobufs.ServerToAgent {
if c.OnMessageFunc != nil {
return c.OnMessageFunc(conn, message)
Expand All @@ -45,6 +54,7 @@ func (c ConnectionCallbacksStruct) OnMessage(conn types.Connection, message *pro
}
}

// OnConnectionClose implements ConnectionCallbacks.OnConnectionClose.
func (c ConnectionCallbacksStruct) OnConnectionClose(conn types.Connection) {
if c.OnConnectionCloseFunc != nil {
c.OnConnectionCloseFunc(conn)
Expand Down
3 changes: 3 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/open-telemetry/opamp-go/server/types"
)

// Settings contains the settings for attaching an OpAMP Server.
type Settings struct {
// Callbacks that the Server will call after successful Attach/Start.
Callbacks types.Callbacks
Expand All @@ -19,6 +20,7 @@ type Settings struct {
EnableCompression bool
}

// StartSettings contains the settings for starting an OpAMP Server.
type StartSettings struct {
Settings

Expand All @@ -37,6 +39,7 @@ type HTTPHandlerFunc func(http.ResponseWriter, *http.Request)

type ConnContext func(ctx context.Context, c net.Conn) context.Context

// OpAMPServer is an interface representing the server side of the OpAMP protocol.
type OpAMPServer interface {
// Attach prepares the OpAMP Server to begin handling requests from an existing
// http.Server. The returned HTTPHandlerFunc and ConnContext should be added as a
Expand Down
1 change: 1 addition & 0 deletions server/serverimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type server struct {

var _ OpAMPServer = (*server)(nil)

// New creates a new OpAMP Server.
func New(logger types.Logger) *server {
if logger == nil {
logger = &internal.NopLogger{}
Expand Down

0 comments on commit fc9dfe6

Please sign in to comment.