Skip to content

Commit

Permalink
smaller fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
michel-laterman committed Sep 23, 2024
1 parent f3aefbb commit 6f688ac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
15 changes: 10 additions & 5 deletions internal/pkg/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ import (
"regexp"
"strings"

"github.com/elastic/fleet-server/v7/internal/pkg/config"
"github.com/elastic/fleet-server/v7/internal/pkg/limit"
"github.com/elastic/fleet-server/v7/internal/pkg/logger"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/rs/zerolog"
"go.elastic.co/apm/module/apmchiv5/v2"
"go.elastic.co/apm/v2"

"github.com/elastic/fleet-server/v7/internal/pkg/config"
"github.com/elastic/fleet-server/v7/internal/pkg/limit"
"github.com/elastic/fleet-server/v7/internal/pkg/logger"

opamp "github.com/open-telemetry/opamp-go/server"
)

const opampPath = "/v1/opamp"

func newRouter(cfg *config.ServerLimits, si ServerInterface, tracer *apm.Tracer, handlerFn opamp.HTTPHandlerFunc) http.Handler {
r := chi.NewRouter()
if tracer != nil {
Expand All @@ -30,7 +33,7 @@ func newRouter(cfg *config.ServerLimits, si ServerInterface, tracer *apm.Tracer,
r.Use(logger.Middleware) // Attach middlewares to router directly so the occur before any request parsing/validation
r.Use(middleware.Recoverer)
r.Use(Limiter(cfg).middleware)
r.HandleFunc("/opamp", http.HandlerFunc(
r.HandleFunc(opampPath, http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
handlerFn(w, r)
},
Expand Down Expand Up @@ -87,7 +90,7 @@ func pathToOperation(path string) string {
if path == "/api/status" {
return "status"
}
if path == "/opamp" {
if path == opampPath {
return "opamp"
}
if path == "/api/fleet/uploads" {
Expand Down Expand Up @@ -147,6 +150,8 @@ func (l *limiter) middleware(next http.Handler) http.Handler {
l.getPGPKey.Wrap("getPGPKey", &cntGetPGP, zerolog.DebugLevel)(next).ServeHTTP(w, r)
case "status":
l.status.Wrap("status", &cntStatus, zerolog.DebugLevel)(next).ServeHTTP(w, r)
case "audit-unenroll":
l.auditUnenroll.Wrap("audit-unenroll", &cntAuditUnenroll, zerolog.DebugLevel)(next).ServeHTTP(w, r)
default:
// no tracking or limits
next.ServeHTTP(w, r)
Expand Down
23 changes: 19 additions & 4 deletions internal/pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ import (
"net"
"net/http"

"go.elastic.co/apm/v2"

"github.com/elastic/elastic-agent-libs/transport/tlscommon"
"github.com/elastic/fleet-server/v7/internal/pkg/build"
"github.com/elastic/fleet-server/v7/internal/pkg/bulk"
"github.com/elastic/fleet-server/v7/internal/pkg/config"
"github.com/elastic/fleet-server/v7/internal/pkg/limit"
"github.com/elastic/fleet-server/v7/internal/pkg/logger"
"github.com/elastic/fleet-server/v7/internal/pkg/policy"
"go.elastic.co/apm/v2"

"github.com/open-telemetry/opamp-go/protobufs"
opamp "github.com/open-telemetry/opamp-go/server"
"github.com/open-telemetry/opamp-go/server/types"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -58,10 +60,23 @@ func NewServer(addr string, cfg *config.Server, ct *CheckinT, et *EnrollerT, at
handlerFn, contextWithConn, _ := ompampServer.Attach(opamp.Settings{
Callbacks: opamp.CallbacksStruct{
OnConnectingFunc: func(request *http.Request) types.ConnectionResponse {
fmt.Printf("TEST ")
return types.ConnectionResponse{
Accept: true,
ConnectionCallbacks: opamp.ConnectionCallbacksStruct{},
Accept: true,
ConnectionCallbacks: opamp.ConnectionCallbacksStruct{
OnConnectedFunc: func(ctx context.Context, _ types.Connection) {
zerolog.Ctx(ctx).Info().Msg("Opamp connection started.")
},
OnMessageFunc: func(ctx context.Context, _ types.Connection, message *protobufs.AgentToServer) *protobufs.ServerToAgent {
// TODO: opamp logic goes here
zerolog.Ctx(ctx).Info().Msg("Opamp message recieved.")
return &protobufs.ServerToAgent{
InstanceUid: message.InstanceUid,
}
},
OnConnectionClose: func(ctx context.Context, _ types.Connection) {

Check failure on line 76 in internal/pkg/api/server.go

View workflow job for this annotation

GitHub Actions / lint (linux)

unknown field OnConnectionClose in struct literal of type server.ConnectionCallbacksStruct (typecheck)

Check failure on line 76 in internal/pkg/api/server.go

View workflow job for this annotation

GitHub Actions / lint (linux)

unknown field OnConnectionClose in struct literal of type server.ConnectionCallbacksStruct) (typecheck)
zerolog.Ctx(ctx).Info().Msg("Opamp connection ended.")
},
},
}

},
Expand Down

0 comments on commit 6f688ac

Please sign in to comment.