Skip to content

Commit

Permalink
Merge pull request #192 from gatewayd-io/report-usage
Browse files Browse the repository at this point in the history
Report usage statistics
  • Loading branch information
mostafa authored Mar 7, 2023
2 parents e2cd76a + d5512e8 commit c0ebb12
Show file tree
Hide file tree
Showing 8 changed files with 846 additions and 12 deletions.
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
PACKAGE_NAME=github.com/gatewayd-io/gatewayd/config
PROJECT_URL=github.com/gatewayd-io/gatewayd
CONFIG_PACKAGE=${PROJECT_URL}/config
CMD_PACKAGE=${PROJECT_URL}/cmd
LAST_TAGGED_COMMIT=$(shell git rev-list --tags --max-count=1)
LAST_TAGGED_COMMIT_SHORT=$(shell git rev-parse --short ${LAST_TAGGED_COMMIT})
# LAST_TAGGED_COMMIT_SHORT=$(shell git rev-parse --short HEAD)
VERSION=$(shell git describe --tags ${LAST_TAGGED_COMMIT})
TIMESTAMP=$(shell date -u +"%FT%T%z")
VERSION_DETAILS=${TIMESTAMP}/${LAST_TAGGED_COMMIT_SHORT}
EXTRA_LDFLAGS=-X ${PACKAGE_NAME}.Version=${VERSION} -X ${PACKAGE_NAME}.VersionDetails=${VERSION_DETAILS}
EXTRA_LDFLAGS=-X ${CONFIG_PACKAGE}.Version=${VERSION} -X ${CONFIG_PACKAGE}.VersionDetails=${VERSION_DETAILS} -X ${CMD_PACKAGE}.UsageReportURL=usage.gatewayd.io:59091
FILES=gatewayd README.md LICENSE gatewayd.yaml gatewayd_plugins.yaml

tidy:
@go mod tidy

build-dev:
@go mod tidy && CGO_ENABLED=0 go build -tags embed_swagger -trimpath -ldflags "-s -w -X ${PACKAGE_NAME}.Version=${VERSION}"
@go mod tidy && CGO_ENABLED=0 go build -tags embed_swagger -trimpath -ldflags "-s -w -X ${CONFIG_PACKAGE}.Version=${VERSION} -X ${CMD_PACKAGE}.UsageReportURL=localhost:59091"

build-release: tidy
@mkdir -p dist
Expand Down
70 changes: 62 additions & 8 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package cmd

import (
"context"
"crypto/tls"
"fmt"
"log"
"net/http"
"net/url"
"os"
"os/signal"
"runtime"
"strconv"
"syscall"
"time"
Expand All @@ -23,6 +25,7 @@ import (
"github.com/gatewayd-io/gatewayd/plugin"
"github.com/gatewayd-io/gatewayd/pool"
"github.com/gatewayd-io/gatewayd/tracing"
usage "github.com/gatewayd-io/gatewayd/usagereport/v1"
"github.com/getsentry/sentry-go"
"github.com/go-co-op/gocron"
"github.com/panjf2000/gnet/v2"
Expand All @@ -33,17 +36,22 @@ import (
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)

var (
enableTracing bool
collectorURL string
enableSentry bool
devMode bool
pluginConfigFile string
globalConfigFile string
conf *config.Config
pluginRegistry *plugin.Registry
enableTracing bool
collectorURL string
enableSentry bool
devMode bool
enableUsageReport bool
pluginConfigFile string
globalConfigFile string
conf *config.Config
pluginRegistry *plugin.Registry

UsageReportURL = "localhost:59091"

loggers = make(map[string]zerolog.Logger)
pools = make(map[string]*pool.Pool)
Expand Down Expand Up @@ -539,6 +547,50 @@ var runCmd = &cobra.Command{
).Msg("Started the gRPC API")
}

// Report usage statistics.
if enableUsageReport {
go func() {
conn, err := grpc.Dial(UsageReportURL,
grpc.WithTransportCredentials(
credentials.NewTLS(
&tls.Config{
MinVersion: tls.VersionTLS12,
},
),
),
)
if err != nil {
logger.Trace().Err(err).Msg(
"Failed to dial to the gRPC server for usage reporting")
}
defer conn.Close()

client := usage.NewUsageReportServiceClient(conn)
report := usage.UsageReportRequest{
Version: config.Version,
RuntimeVersion: runtime.Version(),
Goos: runtime.GOOS,
Goarch: runtime.GOARCH,
Service: "gatewayd",
DevMode: devMode,
Plugins: []*usage.Plugin{},
}
pluginRegistry.ForEach(
func(identifier sdkPlugin.Identifier, plugin *plugin.Plugin) {
report.Plugins = append(report.Plugins, &usage.Plugin{
Name: identifier.Name,
Version: identifier.Version,
Checksum: identifier.Checksum,
})
},
)
_, err = client.Report(context.Background(), &report)
if err != nil {
logger.Trace().Err(err).Msg("Failed to report usage statistics")
}
}()
}

// Shutdown the server gracefully.
var signals []os.Signal
signals = append(signals,
Expand Down Expand Up @@ -648,4 +700,6 @@ func init() {
&enableSentry, "sentry", true, "Enable Sentry")
rootCmd.PersistentFlags().BoolVar(
&devMode, "dev", false, "Enable development mode for plugin development")
rootCmd.PersistentFlags().BoolVar(
&enableUsageReport, "usage-report", true, "Enable usage report")
}
2 changes: 1 addition & 1 deletion config/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const Name = "GatewayD"

var (
// Version is the semantic version of GatewayD.
Version = ""
Version = "0.0.0"
// VersionDetails is the build timestamp and the tagged commit hash.
VersionDetails = ""
)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.19
require (
github.com/Masterminds/semver/v3 v3.2.0
github.com/NYTimes/gziphandler v1.1.1
github.com/envoyproxy/protoc-gen-validate v0.9.1
github.com/gatewayd-io/gatewayd-plugin-sdk v0.0.9
github.com/getsentry/sentry-go v0.18.0
github.com/go-co-op/gocron v1.18.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.m
github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.mod h1:hliV/p42l8fGbc6Y9bQ70uLwIvmJyVE5k4iMKlh8wCQ=
github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/envoyproxy/protoc-gen-validate v0.9.1 h1:PS7VIOgmSVhWUEeZwTe7z7zouA22Cr590PzXKbZHOVY=
github.com/envoyproxy/protoc-gen-validate v0.9.1/go.mod h1:OKNgG7TCp5pF4d6XftA0++PMirau2/yoOwVac3AbF2w=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
Expand Down
Loading

0 comments on commit c0ebb12

Please sign in to comment.