Skip to content
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

updated package structure to include internal package #478

Merged
merged 4 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions main.go → caduceus.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-FileCopyrightText: 2023 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
package caduceus

import (
"fmt"
Expand All @@ -14,6 +14,10 @@ import (
_ "github.com/goschtalt/yaml-decoder"
_ "github.com/goschtalt/yaml-encoder"
"github.com/xmidt-org/arrange/arrangehttp"
"github.com/xmidt-org/caduceus/internal/handler"
"github.com/xmidt-org/caduceus/internal/metrics"
"github.com/xmidt-org/caduceus/internal/sink"

"github.com/xmidt-org/candlelight"
"github.com/xmidt-org/sallust"
"github.com/xmidt-org/touchstone"
Expand Down Expand Up @@ -47,7 +51,7 @@ type CLI struct {
// Provides a named type so it's a bit easier to flow through & use in fx.
type cliArgs []string

func caduceus(arguments []string, run bool) error {
func Caduceus(arguments []string, run bool) error {
var (
gscfg *goschtalt.Config

Expand All @@ -74,7 +78,7 @@ func caduceus(arguments []string, run bool) error {
goschtalt.UnmarshalFunc[sallust.Config]("logging"),
goschtalt.UnmarshalFunc[candlelight.Config]("tracing"),
goschtalt.UnmarshalFunc[touchstone.Config]("prometheus"),
goschtalt.UnmarshalFunc[SinkConfig]("sender"),
goschtalt.UnmarshalFunc[sink.SinkConfig]("sender"),
denopink marked this conversation as resolved.
Show resolved Hide resolved
goschtalt.UnmarshalFunc[Service]("service"),
goschtalt.UnmarshalFunc[[]string]("authHeader"),
goschtalt.UnmarshalFunc[bool]("previousVersionSupport"),
Expand Down Expand Up @@ -124,22 +128,22 @@ func caduceus(arguments []string, run bool) error {
candlelight.New,
),

providePprofEndpoint(),
provideMetricEndpoint(),
provideHealthCheck(),
provideCoreEndpoints(),
ProvidePprofEndpoint(),
ProvideMetricEndpoint(),
ProvideHealthCheck(),
ProvideCoreEndpoints(),
maurafortino marked this conversation as resolved.
Show resolved Hide resolved

arrangehttp.ProvideServer("servers.health"),
arrangehttp.ProvideServer("servers.metrics"),
arrangehttp.ProvideServer("servers.pprof"),
arrangehttp.ProvideServer("servers.primary"),
arrangehttp.ProvideServer("servers.alternate"),

ProvideHandler(),
ProvideWrapper(),
handler.ProvideHandler(),
sink.ProvideWrapper(),
maurafortino marked this conversation as resolved.
Show resolved Hide resolved
touchstone.Provide(),
touchhttp.Provide(),
ProvideMetrics(),
metrics.ProvideMetrics(),
maurafortino marked this conversation as resolved.
Show resolved Hide resolved
// ancla.ProvideMetrics(), //TODO: need to add back in once we fix the ancla/argus dependency issue

)
Expand Down Expand Up @@ -219,7 +223,7 @@ func main() {
}
}()

err := caduceus(os.Args[1:], true)
maurafortino marked this conversation as resolved.
Show resolved Hide resolved
err := Caduceus(os.Args[1:], true)

if err == nil {
return
Expand Down
6 changes: 3 additions & 3 deletions main_test.go → caduceus_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: 2023 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0

package main
package caduceus

import (
"testing"
Expand Down Expand Up @@ -86,10 +86,10 @@ func Test_caduceus(t *testing.T) {

if tc.panic {
assert.Panics(func() {
_ = caduceus(tc.args, false)
_ = Caduceus(tc.args, false)
})
} else {
err := caduceus(tc.args, false)
err := Caduceus(tc.args, false)

assert.ErrorIs(err, tc.expectedErr)
}
Expand Down
29 changes: 29 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-FileCopyrightText: 2023 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: LicenseRef-COMCAST

package main

import (
"fmt"
"os"
"runtime/debug"

"github.com/xmidt-org/caduceus"
)

func main() {
defer func() {
if r := recover(); r != nil {
fmt.Println("stacktrace from panic: \n" + string(debug.Stack()))
}
}()
maurafortino marked this conversation as resolved.
Show resolved Hide resolved

err := caduceus.Caduceus(os.Args[1:], true)

if err == nil {
return
}

fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
17 changes: 15 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: 2023 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0

package main
package caduceus

import (
"fmt"
Expand All @@ -11,7 +11,10 @@ import (
"github.com/goschtalt/goschtalt"
"github.com/xmidt-org/arrange/arrangehttp"
"github.com/xmidt-org/arrange/arrangepprof"
"github.com/xmidt-org/bascule"
"github.com/xmidt-org/caduceus/internal/sink"
"github.com/xmidt-org/candlelight"
"github.com/xmidt-org/clortho"
"github.com/xmidt-org/sallust"
"github.com/xmidt-org/touchstone"
"gopkg.in/dealancer/validate.v2"
Expand All @@ -26,7 +29,7 @@ type Config struct {
Servers Servers
ArgusClientTimeout HttpClientTimeout
JWTValidator JWTValidator
Sink SinkConfig
Sink sink.SinkConfig
Service Service
AuthHeader []string
Server string
Expand Down Expand Up @@ -114,6 +117,16 @@ type MetricsOption struct {
Subsystem string
}

// JWTValidator provides a convenient way to define jwt validator through config files
type JWTValidator struct {
// Config is used to create the clortho Resolver & Refresher for JWT verification keys
Config clortho.Config `json:"config"`

// Leeway is used to set the amount of time buffer should be given to JWT
// time values, such as nbf
Leeway bascule.Leeway
}

// Collect and process the configuration files and env vars and
// produce a configuration object.
func provideConfig(cli *CLI) (*goschtalt.Config, error) {
Expand Down
4 changes: 2 additions & 2 deletions client.go → internal/client/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-FileCopyrightText: 2024 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
package client

import "net/http"

Expand All @@ -10,7 +10,7 @@ type Client interface {
Do(*http.Request) (*http.Response, error)
}

func nopClient(next Client) Client {
func NopClient(next Client) Client {
return next
}

Expand Down
11 changes: 6 additions & 5 deletions httpClient.go → internal/client/httpClient.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: 2023 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0

package main
package client

import (
"errors"
Expand All @@ -10,6 +10,7 @@ import (
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/xmidt-org/caduceus/internal/metrics"
)

var (
Expand All @@ -22,7 +23,7 @@ type metricWrapper struct {
id string
}

func newMetricWrapper(now func() time.Time, queryLatency prometheus.ObserverVec, id string) (*metricWrapper, error) {
func NewMetricWrapper(now func() time.Time, queryLatency prometheus.ObserverVec, id string) (*metricWrapper, error) {
if now == nil {
now = time.Now
}
Expand All @@ -36,20 +37,20 @@ func newMetricWrapper(now func() time.Time, queryLatency prometheus.ObserverVec,
}, nil
}

func (m *metricWrapper) roundTripper(next Client) Client {
func (m *metricWrapper) RoundTripper(next Client) Client {
return doerFunc(func(req *http.Request) (*http.Response, error) {
startTime := m.now()
resp, err := next.Do(req)
endTime := m.now()
code := networkError
code := metrics.NetworkError

if err == nil {
code = strconv.Itoa(resp.StatusCode)
}

// find time difference, add to metric
var latency = endTime.Sub(startTime)
m.queryLatency.With(prometheus.Labels{UrlLabel: m.id, CodeLabel: code}).Observe(latency.Seconds())
m.queryLatency.With(prometheus.Labels{metrics.UrlLabel: m.id, metrics.CodeLabel: code}).Observe(latency.Seconds())

return resp, err
})
Expand Down
2 changes: 1 addition & 1 deletion httpClient_test.go → internal/client/httpClient_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: 2021 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0

package main
package client

// import (
// "errors"
Expand Down
24 changes: 24 additions & 0 deletions internal/handler/caduceus_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-FileCopyrightText: 2021 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package handler

import (
"go.uber.org/zap"

"github.com/xmidt-org/caduceus/internal/sink"
"github.com/xmidt-org/wrp-go/v3"
)

type RequestHandler interface {
HandleRequest(workerID int, msg *wrp.Message)
}

type CaduceusHandler struct {
wrapper sink.Wrapper
Logger *zap.Logger
}

func (ch *CaduceusHandler) HandleRequest(workerID int, msg *wrp.Message) {
ch.Logger.Info("Worker received a request, now passing to sender", zap.Int("workerId", workerID))
ch.wrapper.Queue(msg)
}
maurafortino marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 5 additions & 5 deletions caduceus_type_test.go → internal/handler/caduceus_type_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
// SPDX-FileCopyrightText: 2021 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
package handler

import (
"testing"

"github.com/stretchr/testify/mock"
"go.uber.org/zap/zaptest"

"github.com/xmidt-org/caduceus/internal/mocks"
"github.com/xmidt-org/wrp-go/v3"
"go.uber.org/zap/zaptest"
)

func TestCaduceusHandler(t *testing.T) {
logger := zaptest.NewLogger(t)

fakeSenderWrapper := new(mockSenderWrapper)
fakeSenderWrapper := new(mocks.MockSinkWrapper)
fakeSenderWrapper.On("Queue", mock.AnythingOfType("*wrp.Message")).Return().Once()

testHandler := CaduceusHandler{
wrapper: fakeSenderWrapper,
Logger: logger,
Logger: logger,
}

t.Run("TestHandleRequest", func(t *testing.T) {
Expand Down
19 changes: 10 additions & 9 deletions http.go → internal/handler/http.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-FileCopyrightText: 2021 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
package handler

import (
"io"
Expand All @@ -13,14 +13,15 @@ import (

"github.com/prometheus/client_golang/prometheus"
uuid "github.com/satori/go.uuid"

"github.com/xmidt-org/caduceus/internal/metrics"
"github.com/xmidt-org/caduceus/internal/sink"
"github.com/xmidt-org/sallust"
"github.com/xmidt-org/wrp-go/v3"
)

type ServerHandlerIn struct {
fx.In
SinkWrapper *SinkWrapper
SinkWrapper *sink.SinkWrapper
Logger *zap.Logger
Telemetry *HandlerTelemetry
}
Expand Down Expand Up @@ -57,7 +58,7 @@ type HandlerTelemetry struct {
}

func (sh *ServerHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
eventType := unknownEventType
eventType := metrics.UnknownEventType
logger := sallust.Get(request.Context())
// find time difference, add to metric after function finishes
defer func(s time.Time) {
Expand Down Expand Up @@ -155,16 +156,16 @@ func (sh *ServerHandler) fixWrp(msg *wrp.Message) *wrp.Message {
// use the one the source specified.
if msg.ContentType == "" {
msg.ContentType = wrp.MimeTypeJson
reason = emptyContentTypeReason
reason = metrics.EmptyContentTypeReason
}

// Ensure there is a transaction id even if we make one up
if msg.TransactionUUID == "" {
msg.TransactionUUID = uuid.NewV4().String()
if reason == "" {
reason = emptyUUIDReason
reason = metrics.EmptyUUIDReason
} else {
reason = bothEmptyReason
reason = metrics.BothEmptyReason
}
}

Expand Down Expand Up @@ -196,11 +197,11 @@ func ProvideHandler() fx.Option {
},
)
}
func New(sw *SinkWrapper, log *zap.Logger, t *HandlerTelemetry, maxOutstanding, incomingQueueDepth int64) (*ServerHandler, error) {
func New(sw *sink.SinkWrapper, log *zap.Logger, t *HandlerTelemetry, maxOutstanding, incomingQueueDepth int64) (*ServerHandler, error) {
maurafortino marked this conversation as resolved.
Show resolved Hide resolved
return &ServerHandler{
caduceusHandler: &CaduceusHandler{
wrapper: sw,
Logger: log,
Logger: log,
},
telemetry: t,
maxOutstanding: maxOutstanding,
Expand Down
Loading
Loading