Skip to content

Commit

Permalink
Merge pull request #478 from xmidt-org/package-reorg
Browse files Browse the repository at this point in the history
updated package structure to include internal package
  • Loading branch information
maurafortino authored Apr 15, 2024
2 parents 03f7c9b + 828166a commit 15f8659
Show file tree
Hide file tree
Showing 26 changed files with 440 additions and 421 deletions.
34 changes: 10 additions & 24 deletions main.go → caduceus.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// SPDX-FileCopyrightText: 2023 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
package caduceus

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

"github.com/alecthomas/kong"
"github.com/goschtalt/goschtalt"
Expand All @@ -14,6 +13,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 +50,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 +77,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.Config]("sender"),
goschtalt.UnmarshalFunc[Service]("service"),
goschtalt.UnmarshalFunc[[]string]("authHeader"),
goschtalt.UnmarshalFunc[bool]("previousVersionSupport"),
Expand Down Expand Up @@ -135,11 +138,11 @@ func caduceus(arguments []string, run bool) error {
arrangehttp.ProvideServer("servers.primary"),
arrangehttp.ProvideServer("servers.alternate"),

ProvideHandler(),
ProvideWrapper(),
handler.Provide(),
sink.Provide(),
touchstone.Provide(),
touchhttp.Provide(),
ProvideMetrics(),
metrics.Provide(),
// ancla.ProvideMetrics(), //TODO: need to add back in once we fix the ancla/argus dependency issue

)
Expand Down Expand Up @@ -211,20 +214,3 @@ func provideCLIWithOpts(args cliArgs, testOpts bool) (*CLI, error) {

return &cli, nil
}

func main() {
defer func() {
if r := recover(); r != nil {
fmt.Println("stacktrace from panic: \n" + string(debug.Stack()))
}
}()

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

if err == nil {
return
}

fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
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
30 changes: 0 additions & 30 deletions caduceus_type_test.go

This file was deleted.

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()))
}
}()

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
SinkConfig sink.Config
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
Loading

0 comments on commit 15f8659

Please sign in to comment.