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 9d62b79
Show file tree
Hide file tree
Showing 2 changed files with 26 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
20 changes: 16 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,20 @@ 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.")

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

View workflow job for this annotation

GitHub Actions / lint (linux)

`recieved` is a misspelling of `received` (misspell)
return &protobufs.ServerToAgent{
InstanceUid: message.InstanceUid,
}
},
},
}

},
Expand Down

0 comments on commit 9d62b79

Please sign in to comment.