From adf73ae9734e18224a7a6bc59d9ccaa060ac5d45 Mon Sep 17 00:00:00 2001 From: Kcrong Date: Tue, 3 Oct 2023 21:31:17 +0900 Subject: [PATCH] Update --- Makefile | 11 +- cmd/main.go | 17 +- go.mod | 2 + go.sum | 4 + pkg/app.go | 7 +- pkg/config/parse.go | 2 +- pkg/handler/captiveportal/auth.go | 5 +- pkg/handler/captiveportal/auth_test.go | 5 +- pkg/handler/captiveportal/get.go | 4 +- pkg/handler/route.go | 4 +- .../v3/integrations/nrecho-v4/LICENSE.txt | 206 ++++++++++++++++++ .../v3/integrations/nrecho-v4/README.md | 10 + .../v3/integrations/nrecho-v4/nrecho.go | 133 +++++++++++ .../v3/integrations/nrredis-v9/LICENSE.txt | 206 ++++++++++++++++++ .../v3/integrations/nrredis-v9/README.md | 10 + .../v3/integrations/nrredis-v9/nrredis.go | 109 +++++++++ vendor/modules.txt | 6 + 17 files changed, 717 insertions(+), 24 deletions(-) create mode 100644 vendor/github.com/newrelic/go-agent/v3/integrations/nrecho-v4/LICENSE.txt create mode 100644 vendor/github.com/newrelic/go-agent/v3/integrations/nrecho-v4/README.md create mode 100644 vendor/github.com/newrelic/go-agent/v3/integrations/nrecho-v4/nrecho.go create mode 100644 vendor/github.com/newrelic/go-agent/v3/integrations/nrredis-v9/LICENSE.txt create mode 100644 vendor/github.com/newrelic/go-agent/v3/integrations/nrredis-v9/README.md create mode 100644 vendor/github.com/newrelic/go-agent/v3/integrations/nrredis-v9/nrredis.go diff --git a/Makefile b/Makefile index eab3642..2a962fc 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,14 @@ .PHONY: format format: - @go install golang.org/x/tools/cmd/goimports@latest - goimports -local "github.com/hodlgap" -w . + @#go install golang.org/x/tools/cmd/goimports@latest + @#goimports -local "github.com/hodlgap/captive-portal" -w . + @go install -v github.com/incu6us/goimports-reviser/v3@latest + goimports-reviser -rm-unused \ + -company-prefixes 'github.com/hodlgap' \ + -excludes 'pkg/models,vendor,db' \ + -project-name 'github.com/hodlgap/captive-portal' \ + -format \ + ./... gofmt -s -w . go mod tidy go mod vendor diff --git a/cmd/main.go b/cmd/main.go index 21c7d1e..73dea5c 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -8,15 +8,14 @@ import ( "os/signal" "time" - "github.com/hodlgap/captive-portal/pkg" - "github.com/hodlgap/captive-portal/pkg/handler" - + "github.com/labstack/gommon/log" + "github.com/newrelic/go-agent/v3/integrations/nrredis-v9" "github.com/pkg/errors" + "github.com/redis/go-redis/v9" + "github.com/hodlgap/captive-portal/pkg" "github.com/hodlgap/captive-portal/pkg/config" - - "github.com/labstack/gommon/log" - "github.com/redis/go-redis/v9" + "github.com/hodlgap/captive-portal/pkg/handler" ) const ( @@ -39,11 +38,13 @@ func main() { log.Fatalf("%+v", errors.WithStack(err)) } - redisCli := redis.NewClient(&redis.Options{ + redisOpt := &redis.Options{ Addr: fmt.Sprintf("%s:%d", c.Redis.Host, c.Redis.Port), Password: c.Redis.Password, // no password set DB: c.Redis.DB, // use default DB - }) + } + redisCli := redis.NewClient(redisOpt) + redisCli.AddHook(nrredis.NewHook(redisOpt)) app = handler.SetRoute(c, app, redisCli) diff --git a/go.mod b/go.mod index 4505dd6..c8bbbba 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,8 @@ require ( github.com/labstack/echo/v4 v4.11.1 github.com/labstack/gommon v0.4.0 github.com/newrelic/go-agent/v3 v3.26.0 + github.com/newrelic/go-agent/v3/integrations/nrecho-v4 v1.0.4 + github.com/newrelic/go-agent/v3/integrations/nrredis-v9 v1.0.0 github.com/pkg/errors v0.9.1 github.com/redis/go-redis/v9 v9.1.0 github.com/sirupsen/logrus v1.9.3 diff --git a/go.sum b/go.sum index 946d1d6..f0f158a 100644 --- a/go.sum +++ b/go.sum @@ -370,6 +370,10 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/newrelic/go-agent/v3 v3.26.0 h1:xJkqiQgLtC3ys5zoBxD91ITm7sVHZNEF+7/mqmFjnl0= github.com/newrelic/go-agent/v3 v3.26.0/go.mod h1:sE2WdlLF3B/xI5HUuIHTa7Aht1gpcIY65pzaUoN1pJs= +github.com/newrelic/go-agent/v3/integrations/nrecho-v4 v1.0.4 h1:c9kq15TraxDAZr0bHDmBq5VSRZYPGwH6IyksshILxnY= +github.com/newrelic/go-agent/v3/integrations/nrecho-v4 v1.0.4/go.mod h1:UdtAjlKMd7hzPxylHMXzCvv3Ryx6+Du1DWZez01iK+M= +github.com/newrelic/go-agent/v3/integrations/nrredis-v9 v1.0.0 h1:jPjdL6i69gAXeOHJH/GCmLXxrzMl0J19wE5H1U3FdGE= +github.com/newrelic/go-agent/v3/integrations/nrredis-v9 v1.0.0/go.mod h1:oRTLRKiOcFsOJZgioSMRUQ54vUMomoQkoQW8wql4B4s= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= diff --git a/pkg/app.go b/pkg/app.go index ec6e82b..c9166bc 100644 --- a/pkg/app.go +++ b/pkg/app.go @@ -1,8 +1,9 @@ package pkg import ( - "github.com/labstack/echo/v4" + echo "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" + nrecho "github.com/newrelic/go-agent/v3/integrations/nrecho-v4" "github.com/newrelic/go-agent/v3/newrelic" "github.com/hodlgap/captive-portal/pkg/config" @@ -12,9 +13,9 @@ func NewApp(c config.Config, nr *newrelic.Application) (*echo.Echo, error) { app := echo.New() app.Logger.SetLevel(c.App.LogLevel.ToLevel()) - //app.Use(middleware.Logger()) + app.Use(middleware.Logger()) app.Use(middleware.Recover()) - //app.Use(nrecho.Middleware(nr)) + app.Use(nrecho.Middleware(nr)) return app, nil } diff --git a/pkg/config/parse.go b/pkg/config/parse.go index 6efdf3c..3c4028e 100644 --- a/pkg/config/parse.go +++ b/pkg/config/parse.go @@ -6,7 +6,7 @@ import ( "github.com/pkg/errors" log "github.com/sirupsen/logrus" - "gopkg.in/yaml.v3" + yaml "gopkg.in/yaml.v3" ) type Openwrt struct { diff --git a/pkg/handler/captiveportal/auth.go b/pkg/handler/captiveportal/auth.go index cf46ea3..4750cb4 100644 --- a/pkg/handler/captiveportal/auth.go +++ b/pkg/handler/captiveportal/auth.go @@ -12,10 +12,9 @@ import ( "net/url" "strings" - "github.com/redis/go-redis/v9" - - "github.com/labstack/echo/v4" + echo "github.com/labstack/echo/v4" "github.com/pkg/errors" + redis "github.com/redis/go-redis/v9" ) type RawAuthRequest struct { diff --git a/pkg/handler/captiveportal/auth_test.go b/pkg/handler/captiveportal/auth_test.go index f2419cd..41e5ac6 100644 --- a/pkg/handler/captiveportal/auth_test.go +++ b/pkg/handler/captiveportal/auth_test.go @@ -10,11 +10,10 @@ import ( "strings" "testing" - "github.com/labstack/echo/v4" + redismock "github.com/go-redis/redismock/v9" + echo "github.com/labstack/echo/v4" "github.com/pkg/errors" "github.com/stretchr/testify/assert" - - "github.com/go-redis/redismock/v9" ) // nolint:unused diff --git a/pkg/handler/captiveportal/get.go b/pkg/handler/captiveportal/get.go index ca87a12..7cc6e70 100644 --- a/pkg/handler/captiveportal/get.go +++ b/pkg/handler/captiveportal/get.go @@ -6,9 +6,9 @@ import ( "net/http" "strings" - "github.com/labstack/echo/v4" + echo "github.com/labstack/echo/v4" "github.com/pkg/errors" - "github.com/redis/go-redis/v9" + redis "github.com/redis/go-redis/v9" ) /* Send and/or clear the Auth List when requested by openNDS diff --git a/pkg/handler/route.go b/pkg/handler/route.go index 3d3bb80..76359dd 100644 --- a/pkg/handler/route.go +++ b/pkg/handler/route.go @@ -1,8 +1,8 @@ package handler import ( - "github.com/labstack/echo/v4" - "github.com/redis/go-redis/v9" + echo "github.com/labstack/echo/v4" + redis "github.com/redis/go-redis/v9" "github.com/hodlgap/captive-portal/pkg/config" "github.com/hodlgap/captive-portal/pkg/handler/captiveportal" diff --git a/vendor/github.com/newrelic/go-agent/v3/integrations/nrecho-v4/LICENSE.txt b/vendor/github.com/newrelic/go-agent/v3/integrations/nrecho-v4/LICENSE.txt new file mode 100644 index 0000000..cee548c --- /dev/null +++ b/vendor/github.com/newrelic/go-agent/v3/integrations/nrecho-v4/LICENSE.txt @@ -0,0 +1,206 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + +Versions 3.8.0 and above for this project are licensed under Apache 2.0. For +prior versions of this project, please see the LICENCE.txt file in the root +directory of that version for more information. diff --git a/vendor/github.com/newrelic/go-agent/v3/integrations/nrecho-v4/README.md b/vendor/github.com/newrelic/go-agent/v3/integrations/nrecho-v4/README.md new file mode 100644 index 0000000..1318537 --- /dev/null +++ b/vendor/github.com/newrelic/go-agent/v3/integrations/nrecho-v4/README.md @@ -0,0 +1,10 @@ +# v3/integrations/nrecho-v4 [![GoDoc](https://godoc.org/github.com/newrelic/go-agent/v3/integrations/nrecho-v4?status.svg)](https://godoc.org/github.com/newrelic/go-agent/v3/integrations/nrecho-v4) + +Package `nrecho` instruments applications using https://github.com/labstack/echo v4. + +```go +import "github.com/newrelic/go-agent/v3/integrations/nrecho-v4" +``` + +For more information, see +[godocs](https://godoc.org/github.com/newrelic/go-agent/v3/integrations/nrecho-v4). diff --git a/vendor/github.com/newrelic/go-agent/v3/integrations/nrecho-v4/nrecho.go b/vendor/github.com/newrelic/go-agent/v3/integrations/nrecho-v4/nrecho.go new file mode 100644 index 0000000..4c3b8cd --- /dev/null +++ b/vendor/github.com/newrelic/go-agent/v3/integrations/nrecho-v4/nrecho.go @@ -0,0 +1,133 @@ +// Copyright 2020 New Relic Corporation. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +// Package nrecho instruments applications using +// https://github.com/labstack/echo v4. +// +// Use this package to instrument inbound requests handled by an echo.Echo +// instance. +// +// e := echo.New() +// // Add the nrecho middleware before other middlewares or routes: +// e.Use(nrecho.Middleware(app)) +// +// Example: https://github.com/newrelic/go-agent/tree/master/v3/integrations/nrecho-v4/example/main.go +package nrecho + +import ( + "net/http" + "reflect" + + "github.com/labstack/echo/v4" + "github.com/newrelic/go-agent/v3/internal" + newrelic "github.com/newrelic/go-agent/v3/newrelic" +) + +func init() { internal.TrackUsage("integration", "framework", "echo") } + +// FromContext returns the Transaction from the context if present, and nil +// otherwise. +func FromContext(c echo.Context) *newrelic.Transaction { + return newrelic.FromContext(c.Request().Context()) +} + +func handlerPointer(handler echo.HandlerFunc) uintptr { + return reflect.ValueOf(handler).Pointer() +} + +func transactionName(c echo.Context) string { + ptr := handlerPointer(c.Handler()) + if ptr == handlerPointer(echo.NotFoundHandler) { + return "NotFoundHandler" + } + if ptr == handlerPointer(echo.MethodNotAllowedHandler) { + return "MethodNotAllowedHandler" + } + return c.Request().Method + " " + c.Path() +} + +// Skipper defines a function to skip middleware. Returning true skips processing +// the middleware. +type Skipper func(c echo.Context) bool + +// Config defines the config for the middleware. +type Config struct { + // App contains newrelic application. + App *newrelic.Application + + // Skipper defines a function to skip middleware. + Skipper Skipper +} + +type ConfigOption func(*Config) + +func WithSkipper(skipper Skipper) ConfigOption { + return func(cfg *Config) { cfg.Skipper = skipper } +} + +// Middleware creates Echo middleware with provided config that +// instruments requests. +// +// e := echo.New() +// // Add the nrecho middleware before other middlewares or routes: +// e.Use(nrecho.MiddlewareWithConfig(nrecho.Config{App: app})) +// +func Middleware(app *newrelic.Application, opts ...ConfigOption) func(echo.HandlerFunc) echo.HandlerFunc { + if app == nil { + return func(next echo.HandlerFunc) echo.HandlerFunc { + return next + } + } + + config := Config{ + App: app, + } + + for _, opt := range opts { + opt(&config) + } + + if config.Skipper == nil { + // set default skipper + config.Skipper = func(echo.Context) bool { + return false + } + } + + return func(next echo.HandlerFunc) echo.HandlerFunc { + return func(c echo.Context) (err error) { + if config.Skipper(c) { + return next(c) + } + + rw := c.Response().Writer + txn := config.App.StartTransaction(transactionName(c)) + defer txn.End() + + txn.SetWebRequestHTTP(c.Request()) + + c.Response().Writer = txn.SetWebResponse(rw) + + // Add txn to c.Request().Context() + c.SetRequest(c.Request().WithContext(newrelic.NewContext(c.Request().Context(), txn))) + + err = next(c) + + // Record the response code. The response headers are not captured + // in this case because they are set after this middleware returns. + // Designed to mimic the logic in echo.DefaultHTTPErrorHandler. + if nil != err && !c.Response().Committed { + + c.Response().Writer = rw + + if httperr, ok := err.(*echo.HTTPError); ok { + txn.SetWebResponse(nil).WriteHeader(httperr.Code) + } else { + txn.SetWebResponse(nil).WriteHeader(http.StatusInternalServerError) + } + } + + return + } + } +} diff --git a/vendor/github.com/newrelic/go-agent/v3/integrations/nrredis-v9/LICENSE.txt b/vendor/github.com/newrelic/go-agent/v3/integrations/nrredis-v9/LICENSE.txt new file mode 100644 index 0000000..cee548c --- /dev/null +++ b/vendor/github.com/newrelic/go-agent/v3/integrations/nrredis-v9/LICENSE.txt @@ -0,0 +1,206 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + +Versions 3.8.0 and above for this project are licensed under Apache 2.0. For +prior versions of this project, please see the LICENCE.txt file in the root +directory of that version for more information. diff --git a/vendor/github.com/newrelic/go-agent/v3/integrations/nrredis-v9/README.md b/vendor/github.com/newrelic/go-agent/v3/integrations/nrredis-v9/README.md new file mode 100644 index 0000000..b4214a9 --- /dev/null +++ b/vendor/github.com/newrelic/go-agent/v3/integrations/nrredis-v9/README.md @@ -0,0 +1,10 @@ +# v3/integrations/nrredis-v9 [![pkg.go.dev](https://pkg.go.dev/github.com/newrelic/go-agent/v3/integrations/nrredis-v9?status.svg)](https://pkg.go.dev/github.com/newrelic/go-agent/v3/integrations/nrredis-v9) + +Package `nrredis` instruments `"github.com/redis/go-redis/v9"`. + +```go +import nrredis "github.com/newrelic/go-agent/v3/integrations/nrredis-v9" +``` + +For more information, see +[pkg.go.dev](https://pkg.go.dev/github.com/newrelic/go-agent/v3/integrations/nrredis-v9). diff --git a/vendor/github.com/newrelic/go-agent/v3/integrations/nrredis-v9/nrredis.go b/vendor/github.com/newrelic/go-agent/v3/integrations/nrredis-v9/nrredis.go new file mode 100644 index 0000000..6f563ac --- /dev/null +++ b/vendor/github.com/newrelic/go-agent/v3/integrations/nrredis-v9/nrredis.go @@ -0,0 +1,109 @@ +// Copyright 2023 New Relic Corporation. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +// Package nrredis instruments github.com/redis/go-redis/v9. +// +// Use this package to instrument your redis/go-redis/v9 calls without having to +// manually create DatastoreSegments. +package nrredis + +import ( + "context" + "net" + "strings" + + "github.com/newrelic/go-agent/v3/internal" + newrelic "github.com/newrelic/go-agent/v3/newrelic" + redis "github.com/redis/go-redis/v9" +) + +func init() { internal.TrackUsage("integration", "datastore", "redis") } + +type contextKeyType struct{} + +type hook struct { + segment newrelic.DatastoreSegment +} + +var _ redis.Hook = (*hook)(nil) + +var ( + segmentContextKey = contextKeyType(struct{}{}) +) + +// NewHook creates a redis.Hook to instrument Redis calls. Add it to your +// client, then ensure that all calls contain a context which includes the +// transaction. The options are optional. Provide them to get instance metrics +// broken out by host and port. The hook returned can be used with +// redis.Client, redis.ClusterClient, and redis.Ring. +func NewHook(opts *redis.Options) redis.Hook { + h := hook{} + h.segment.Product = newrelic.DatastoreRedis + if opts == nil { + return h + } + + // Per https://pkg.go.dev/github.com/redis/go-redis#Options the + // network should either be tcp or unix, and the default is tcp. + if opts.Network == "unix" { + h.segment.Host = "localhost" + h.segment.PortPathOrID = opts.Addr + return h + } + if host, port, err := net.SplitHostPort(opts.Addr); err == nil { + if host == "" { + host = "localhost" + } + h.segment.Host = host + h.segment.PortPathOrID = port + } + return h +} + +func (h hook) before(ctx context.Context, operation string) context.Context { + txn := newrelic.FromContext(ctx) + if txn == nil { + return ctx + } + s := h.segment + s.StartTime = txn.StartSegmentNow() + s.Operation = operation + ctx = context.WithValue(ctx, segmentContextKey, &s) + return ctx +} + +func (h hook) after(ctx context.Context) { + if segment, ok := ctx.Value(segmentContextKey).(interface{ End() }); ok { + segment.End() + } +} + +func pipelineOperation(cmds []redis.Cmder) string { + operations := make([]string, 0, len(cmds)) + for _, cmd := range cmds { + operations = append(operations, cmd.Name()) + } + return "pipeline:" + strings.Join(operations, ",") +} + +func (h hook) DialHook(next redis.DialHook) redis.DialHook { + return next // just continue the hook +} + +func (h hook) ProcessHook(next redis.ProcessHook) redis.ProcessHook { + return func(ctx context.Context, cmd redis.Cmder) error { + ctx = h.before(ctx, cmd.Name()) + err := next(ctx, cmd) + h.after(ctx) + return err + } +} + +func (h hook) ProcessPipelineHook(next redis.ProcessPipelineHook) redis.ProcessPipelineHook { + return func(ctx context.Context, cmds []redis.Cmder) error { + ctx = h.before(ctx, pipelineOperation(cmds)) + err := next(ctx, cmds) + h.after(ctx) + return err + } +} diff --git a/vendor/modules.txt b/vendor/modules.txt index a30e1f5..6dcf90f 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -59,6 +59,12 @@ github.com/newrelic/go-agent/v3/internal/logger github.com/newrelic/go-agent/v3/internal/sysinfo github.com/newrelic/go-agent/v3/internal/utilization github.com/newrelic/go-agent/v3/newrelic +# github.com/newrelic/go-agent/v3/integrations/nrecho-v4 v1.0.4 +## explicit; go 1.19 +github.com/newrelic/go-agent/v3/integrations/nrecho-v4 +# github.com/newrelic/go-agent/v3/integrations/nrredis-v9 v1.0.0 +## explicit; go 1.17 +github.com/newrelic/go-agent/v3/integrations/nrredis-v9 # github.com/pkg/errors v0.9.1 ## explicit github.com/pkg/errors