add build tag to disable metrics

This commit is contained in:
Nicola Murino 2020-06-19 17:08:51 +02:00
parent b30614e9d8
commit 23a80b01b6
29 changed files with 189 additions and 109 deletions

View file

@ -16,7 +16,7 @@ import (
"github.com/drakkan/sftpgo/dataprovider"
"github.com/drakkan/sftpgo/service"
"github.com/drakkan/sftpgo/sftpd"
"github.com/drakkan/sftpgo/utils"
"github.com/drakkan/sftpgo/version"
"github.com/drakkan/sftpgo/vfs"
)
@ -141,7 +141,7 @@ Please take a look at the usage below to customize the serving parameters`,
)
func init() {
utils.AddFeature("+portable")
version.AddFeature("+portable")
portableCmd.Flags().StringVarP(&directoryToServe, "directory", "d", ".",
"Path to the directory to serve. This can be an absolute path or a path relative to the current directory")

View file

@ -2,8 +2,8 @@
package cmd
import "github.com/drakkan/sftpgo/utils"
import "github.com/drakkan/sftpgo/version"
func init() {
utils.AddFeature("-portable")
version.AddFeature("-portable")
}

View file

@ -9,7 +9,7 @@ import (
"github.com/spf13/viper"
"github.com/drakkan/sftpgo/config"
"github.com/drakkan/sftpgo/utils"
"github.com/drakkan/sftpgo/version"
)
const (
@ -60,9 +60,8 @@ var (
)
func init() {
version := utils.GetAppVersion()
rootCmd.Flags().BoolP("version", "v", false, "")
rootCmd.Version = version.GetVersionAsString()
rootCmd.Version = version.GetAsString()
rootCmd.SetVersionTemplate(`{{printf "SFTPGo "}}{{printf "%s" .Version}}
`)
}

View file

@ -17,6 +17,7 @@ import (
"github.com/drakkan/sftpgo/logger"
"github.com/drakkan/sftpgo/sftpd"
"github.com/drakkan/sftpgo/utils"
"github.com/drakkan/sftpgo/version"
)
const (
@ -31,7 +32,7 @@ const (
var (
globalConf globalConfig
defaultBanner = fmt.Sprintf("SFTPGo_%v", utils.GetAppVersion().Version)
defaultBanner = fmt.Sprintf("SFTPGo_%v", version.Get().Version)
)
type globalConfig struct {

View file

@ -14,6 +14,7 @@ import (
"github.com/drakkan/sftpgo/logger"
"github.com/drakkan/sftpgo/utils"
"github.com/drakkan/sftpgo/version"
"github.com/drakkan/sftpgo/vfs"
)
@ -57,7 +58,7 @@ type compatUserV2 struct {
}
func init() {
utils.AddFeature("+bolt")
version.AddFeature("+bolt")
}
func initializeBoltProvider(basePath string) error {

View file

@ -5,11 +5,11 @@ package dataprovider
import (
"errors"
"github.com/drakkan/sftpgo/utils"
"github.com/drakkan/sftpgo/version"
)
func init() {
utils.AddFeature("-bolt")
version.AddFeature("-bolt")
}
func initializeBoltProvider(basePath string) error {

View file

@ -12,7 +12,7 @@ import (
_ "github.com/go-sql-driver/mysql"
"github.com/drakkan/sftpgo/logger"
"github.com/drakkan/sftpgo/utils"
"github.com/drakkan/sftpgo/version"
"github.com/drakkan/sftpgo/vfs"
)
@ -45,7 +45,7 @@ type MySQLProvider struct {
}
func init() {
utils.AddFeature("+mysql")
version.AddFeature("+mysql")
}
func initializeMySQLProvider() error {

View file

@ -5,11 +5,11 @@ package dataprovider
import (
"errors"
"github.com/drakkan/sftpgo/utils"
"github.com/drakkan/sftpgo/version"
)
func init() {
utils.AddFeature("-mysql")
version.AddFeature("-mysql")
}
func initializeMySQLProvider() error {

View file

@ -11,7 +11,7 @@ import (
_ "github.com/lib/pq"
"github.com/drakkan/sftpgo/logger"
"github.com/drakkan/sftpgo/utils"
"github.com/drakkan/sftpgo/version"
"github.com/drakkan/sftpgo/vfs"
)
@ -44,7 +44,7 @@ type PGSQLProvider struct {
}
func init() {
utils.AddFeature("+pgsql")
version.AddFeature("+pgsql")
}
func initializePGSQLProvider() error {

View file

@ -5,11 +5,11 @@ package dataprovider
import (
"errors"
"github.com/drakkan/sftpgo/utils"
"github.com/drakkan/sftpgo/version"
)
func init() {
utils.AddFeature("-pgsql")
version.AddFeature("-pgsql")
}
func initializePGSQLProvider() error {

View file

@ -13,6 +13,7 @@ import (
"github.com/drakkan/sftpgo/logger"
"github.com/drakkan/sftpgo/utils"
"github.com/drakkan/sftpgo/version"
"github.com/drakkan/sftpgo/vfs"
)
@ -69,7 +70,7 @@ type SQLiteProvider struct {
}
func init() {
utils.AddFeature("+sqlite")
version.AddFeature("+sqlite")
}
func initializeSQLiteProvider(basePath string) error {

View file

@ -5,11 +5,11 @@ package dataprovider
import (
"errors"
"github.com/drakkan/sftpgo/utils"
"github.com/drakkan/sftpgo/version"
)
func init() {
utils.AddFeature("-sqlite")
version.AddFeature("-sqlite")
}
func initializeSQLiteProvider(basePath string) error {

View file

@ -7,7 +7,7 @@ ARG TAG
ARG FEATURES
# Use --build-arg TAG=LATEST for latest tag. Use e.g. --build-arg TAG=0.9.6 for a specific tag/commit. Otherwise HEAD (master) is built.
RUN git checkout $(if [ "${TAG}" = LATEST ]; then echo `git rev-list --tags --max-count=1`; elif [ -n "${TAG}" ]; then echo "${TAG}"; else echo HEAD; fi)
RUN go build -i $(if [ -n "${FEATURES}" ]; then echo "-tags ${FEATURES}"; fi) -ldflags "-s -w -X github.com/drakkan/sftpgo/utils.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/utils.date=`date -u +%FT%TZ`" -o /go/bin/sftpgo
RUN go build -i $(if [ -n "${FEATURES}" ]; then echo "-tags ${FEATURES}"; fi) -ldflags "-s -w -X github.com/drakkan/sftpgo/version.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/version.date=`date -u +%FT%TZ`" -o /go/bin/sftpgo
FROM alpine:latest

View file

@ -7,7 +7,7 @@ ARG TAG
ARG FEATURES
# Use --build-arg TAG=LATEST for latest tag. Use e.g. --build-arg TAG=0.9.6 for a specific tag/commit. Otherwise HEAD (master) is built.
RUN git checkout $(if [ "${TAG}" = LATEST ]; then echo `git rev-list --tags --max-count=1`; elif [ -n "${TAG}" ]; then echo "${TAG}"; else echo HEAD; fi)
RUN go build -i $(if [ -n "${FEATURES}" ]; then echo "-tags ${FEATURES}"; fi) -ldflags "-s -w -X github.com/drakkan/sftpgo/utils.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/utils.date=`date -u +%FT%TZ`" -o sftpgo
RUN go build -i $(if [ -n "${FEATURES}" ]; then echo "-tags ${FEATURES}"; fi) -ldflags "-s -w -X github.com/drakkan/sftpgo/version.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/version.date=`date -u +%FT%TZ`" -o sftpgo
# now define the run environment
FROM debian:latest

View file

@ -11,12 +11,13 @@ Make sure [Git](https://git-scm.com/downloads) is installed on your machine and
The following build tags are available:
- `nogcs`, disable Google Cloud Storage backend, default enabled
- `nos3`, disable S3 Compabible Object Storage backends, , default enabled
- `nobolt`, disable Bolt data provider, , default enabled
- `nos3`, disable S3 Compabible Object Storage backends, default enabled
- `nobolt`, disable Bolt data provider, default enabled
- `nomysql`, disable MySQL data provider, default enabled
- `nopgsql`, disable PostgreSQL data provider, default enabled
- `nosqlite`, disable SQLite data provider, default enabled
- `noportable`, disable portable mode, default enabled
- `nometrics`, disable Prometheus metrics, default enabled
If no build tag is specified the build will include the default features.
@ -27,18 +28,18 @@ The compiler is a build time only dependency. It is not required at runtime.
Version info, such as git commit and build date, can be embedded setting the following string variables at build time:
- `github.com/drakkan/sftpgo/utils.commit`
- `github.com/drakkan/sftpgo/utils.date`
- `github.com/drakkan/sftpgo/version.commit`
- `github.com/drakkan/sftpgo/version.date`
For example, you can build using the following command:
```bash
go build -i -tags nogcs,nos3,nosqlite -ldflags "-s -w -X github.com/drakkan/sftpgo/utils.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/utils.date=`date -u +%FT%TZ`" -o sftpgo
go build -i -tags nogcs,nos3,nosqlite -ldflags "-s -w -X github.com/drakkan/sftpgo/version.commit=`git describe --always --dirty` -X github.com/drakkan/sftpgo/version.date=`date -u +%FT%TZ`" -o sftpgo
```
You should get a version that includes git commit, build date and available features like this one:
```bash
$ ./sftpgo -v
SFTPGo 0.9.6-dev-15298b0-dirty-2020-05-22T21:25:51Z -gcs -s3 +bolt +mysql +pgsql -sqlite +portable
SFTPGo 0.9.6-dev-b30614e-dirty-2020-06-19T11:04:56Z +metrics -gcs -s3 +bolt +mysql +pgsql -sqlite +portable
```

View file

@ -22,6 +22,7 @@ import (
"github.com/drakkan/sftpgo/httpclient"
"github.com/drakkan/sftpgo/sftpd"
"github.com/drakkan/sftpgo/utils"
"github.com/drakkan/sftpgo/version"
"github.com/drakkan/sftpgo/vfs"
)
@ -370,21 +371,21 @@ func StartFolderQuotaScan(folder vfs.BaseVirtualFolder, expectedStatusCode int)
}
// GetVersion returns version details
func GetVersion(expectedStatusCode int) (utils.VersionInfo, []byte, error) {
var version utils.VersionInfo
func GetVersion(expectedStatusCode int) (version.Info, []byte, error) {
var appVersion version.Info
var body []byte
resp, err := sendHTTPRequest(http.MethodGet, buildURLRelativeToBase(versionPath), nil, "")
if err != nil {
return version, body, err
return appVersion, body, err
}
defer resp.Body.Close()
err = checkResponse(resp.StatusCode, expectedStatusCode)
if err == nil && expectedStatusCode == http.StatusOK {
err = render.DecodeJSON(resp.Body, &version)
err = render.DecodeJSON(resp.Body, &appVersion)
} else {
body, _ = getResponseBody(resp)
}
return version, body, err
return appVersion, body, err
}
// GetProviderStatus returns provider status

View file

@ -12,7 +12,7 @@ import (
"github.com/drakkan/sftpgo/logger"
"github.com/drakkan/sftpgo/metrics"
"github.com/drakkan/sftpgo/sftpd"
"github.com/drakkan/sftpgo/utils"
"github.com/drakkan/sftpgo/version"
)
// GetHTTPRouter returns the configured HTTP handler
@ -55,7 +55,7 @@ func initializeRouter(staticFilesPath string, enableProfiler, enableWebAdmin boo
metrics.AddMetricsEndpoint(metricsPath, router)
router.Get(versionPath, func(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, utils.GetAppVersion())
render.JSON(w, r, version.Get())
})
router.Get(providerStatusPath, func(w http.ResponseWriter, r *http.Request) {

View file

@ -18,6 +18,7 @@ import (
"github.com/drakkan/sftpgo/dataprovider"
"github.com/drakkan/sftpgo/sftpd"
"github.com/drakkan/sftpgo/utils"
"github.com/drakkan/sftpgo/version"
"github.com/drakkan/sftpgo/vfs"
)
@ -143,7 +144,6 @@ func loadTemplates(templatesPath string) {
}
func getBasePageData(title, currentURL string) basePage {
version := utils.GetAppVersion()
return basePage{
Title: title,
CurrentURL: currentURL,
@ -160,7 +160,7 @@ func getBasePageData(title, currentURL string) basePage {
UsersTitle: pageUsersTitle,
ConnectionsTitle: pageConnectionsTitle,
FoldersTitle: pageFoldersTitle,
Version: version.GetVersionAsString(),
Version: version.GetAsString(),
}
}

View file

@ -1,3 +1,5 @@
// +build !nometrics
// Package metrics provides Prometheus metrics support
package metrics
@ -6,6 +8,8 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/drakkan/sftpgo/version"
)
const (
@ -15,9 +19,8 @@ const (
loginMethodKeyAndKeyboardInt = "publickey+keyboard-interactive"
)
// AddMetricsEndpoint exposes metrics to the specified endpoint
func AddMetricsEndpoint(metricsPath string, handler chi.Router) {
handler.Handle(metricsPath, promhttp.Handler())
func init() {
version.AddFeature("+metrics")
}
var (
@ -393,6 +396,11 @@ var (
})
)
// AddMetricsEndpoint exposes metrics to the specified endpoint
func AddMetricsEndpoint(metricsPath string, handler chi.Router) {
handler.Handle(metricsPath, promhttp.Handler())
}
// TransferCompleted updates metrics after an upload or a download
func TransferCompleted(bytesSent, bytesReceived int64, transferKind int, err error) {
if transferKind == 0 {

View file

@ -0,0 +1,67 @@
// +build nometrics
package metrics
import (
"github.com/go-chi/chi"
"github.com/drakkan/sftpgo/version"
)
func init() {
version.AddFeature("-metrics")
}
// AddMetricsEndpoint exposes metrics to the specified endpoint
func AddMetricsEndpoint(metricsPath string, handler chi.Router) {}
// TransferCompleted updates metrics after an upload or a download
func TransferCompleted(bytesSent, bytesReceived int64, transferKind int, err error) {}
// S3TransferCompleted updates metrics after an S3 upload or a download
func S3TransferCompleted(bytes int64, transferKind int, err error) {}
// S3ListObjectsCompleted updates metrics after an S3 list objects request terminates
func S3ListObjectsCompleted(err error) {}
// S3CopyObjectCompleted updates metrics after an S3 copy object request terminates
func S3CopyObjectCompleted(err error) {}
// S3DeleteObjectCompleted updates metrics after an S3 delete object request terminates
func S3DeleteObjectCompleted(err error) {}
// S3HeadBucketCompleted updates metrics after an S3 head bucket request terminates
func S3HeadBucketCompleted(err error) {}
// GCSTransferCompleted updates metrics after a GCS upload or a download
func GCSTransferCompleted(bytes int64, transferKind int, err error) {}
// GCSListObjectsCompleted updates metrics after a GCS list objects request terminates
func GCSListObjectsCompleted(err error) {}
// GCSCopyObjectCompleted updates metrics after a GCS copy object request terminates
func GCSCopyObjectCompleted(err error) {}
// GCSDeleteObjectCompleted updates metrics after a GCS delete object request terminates
func GCSDeleteObjectCompleted(err error) {}
// GCSHeadBucketCompleted updates metrics after a GCS head bucket request terminates
func GCSHeadBucketCompleted(err error) {}
// SSHCommandCompleted update metrics after an SSH command terminates
func SSHCommandCompleted(err error) {}
// UpdateDataProviderAvailability updates the metric for the data provider availability
func UpdateDataProviderAvailability(err error) {}
// AddLoginAttempt increments the metrics for login attempts
func AddLoginAttempt(authMethod string) {}
// AddLoginResult increments the metrics for login results
func AddLoginResult(authMethod string, err error) {}
// HTTPRequestServed increments the metrics for HTTP requests
func HTTPRequestServed(status int) {}
// UpdateActiveConnectionsSize sets the metric for active connections
func UpdateActiveConnectionsSize(size int) {}

View file

@ -12,6 +12,7 @@ import (
"github.com/drakkan/sftpgo/logger"
"github.com/drakkan/sftpgo/sftpd"
"github.com/drakkan/sftpgo/utils"
"github.com/drakkan/sftpgo/version"
)
const (
@ -54,9 +55,8 @@ func (s *Service) Start() error {
logger.DisableLogger()
}
}
version := utils.GetAppVersion()
logger.Info(logSender, "", "starting SFTPGo %v, config dir: %v, config file: %v, log max size: %v log max backups: %v "+
"log max age: %v log verbose: %v, log compress: %v, profile: %v", version.GetVersionAsString(), s.ConfigDir, s.ConfigFile,
"log max age: %v log verbose: %v, log compress: %v, profile: %v", version.GetAsString(), s.ConfigDir, s.ConfigFile,
s.LogMaxSize, s.LogMaxBackups, s.LogMaxAge, s.LogVerbose, s.LogCompress, s.Profiler)
// in portable mode we don't read configuration from file
if s.PortableMode != 1 {

View file

@ -19,6 +19,7 @@ import (
"github.com/drakkan/sftpgo/logger"
"github.com/drakkan/sftpgo/sftpd"
"github.com/drakkan/sftpgo/utils"
"github.com/drakkan/sftpgo/version"
)
// StartPortableMode starts the service in portable mode
@ -67,9 +68,8 @@ func (s *Service) StartPortableMode(sftpdPort int, enabledSSHCommands []string,
}
var mDNSService *zeroconf.Server
if advertiseService {
version := utils.GetAppVersion()
meta := []string{
fmt.Sprintf("version=%v", version.GetVersionAsString()),
fmt.Sprintf("version=%v", version.GetAsString()),
}
if advertiseCredentials {
logger.InfoToConsole("Advertising credentials via multicast DNS")

View file

@ -62,11 +62,6 @@ func GetTimeFromMsecSinceEpoch(msec int64) time.Time {
return time.Unix(0, msec*1000000)
}
// GetAppVersion returns VersionInfo struct
func GetAppVersion() VersionInfo {
return versionInfo
}
// GetDurationAsString returns a string representation for a time.Duration
func GetDurationAsString(d time.Duration) string {
d = d.Round(time.Second)

View file

@ -1,51 +0,0 @@
package utils
import "strings"
const version = "0.9.6-dev"
var (
commit = ""
date = ""
versionInfo VersionInfo
)
// VersionInfo defines version details
type VersionInfo struct {
Version string `json:"version"`
BuildDate string `json:"build_date"`
CommitHash string `json:"commit_hash"`
Features []string `json:"features"`
}
// GetVersionAsString returns the string representation of the VersionInfo struct
func (v *VersionInfo) GetVersionAsString() string {
var sb strings.Builder
sb.WriteString(v.Version)
if len(v.CommitHash) > 0 {
sb.WriteString("-")
sb.WriteString(v.CommitHash)
}
if len(v.BuildDate) > 0 {
sb.WriteString("-")
sb.WriteString(v.BuildDate)
}
if len(v.Features) > 0 {
sb.WriteString(" ")
sb.WriteString(strings.Join(v.Features, " "))
}
return sb.String()
}
// AddFeature adds a feature description
func AddFeature(feature string) {
versionInfo.Features = append(versionInfo.Features, feature)
}
func init() {
versionInfo = VersionInfo{
Version: version,
CommitHash: commit,
BuildDate: date,
}
}

56
version/version.go Normal file
View file

@ -0,0 +1,56 @@
package version
import "strings"
const version = "0.9.6-dev"
var (
commit = ""
date = ""
info Info
)
// Info defines version details
type Info struct {
Version string `json:"version"`
BuildDate string `json:"build_date"`
CommitHash string `json:"commit_hash"`
Features []string `json:"features"`
}
// GetAsString returns the string representation of the version
func GetAsString() string {
var sb strings.Builder
sb.WriteString(info.Version)
if len(info.CommitHash) > 0 {
sb.WriteString("-")
sb.WriteString(info.CommitHash)
}
if len(info.BuildDate) > 0 {
sb.WriteString("-")
sb.WriteString(info.BuildDate)
}
if len(info.Features) > 0 {
sb.WriteString(" ")
sb.WriteString(strings.Join(info.Features, " "))
}
return sb.String()
}
func init() {
info = Info{
Version: version,
CommitHash: commit,
BuildDate: date,
}
}
// AddFeature adds a feature description
func AddFeature(feature string) {
info.Features = append(info.Features, feature)
}
// Get returns the Info struct
func Get() Info {
return info
}

View file

@ -21,7 +21,7 @@ import (
"github.com/drakkan/sftpgo/logger"
"github.com/drakkan/sftpgo/metrics"
"github.com/drakkan/sftpgo/utils"
"github.com/drakkan/sftpgo/version"
)
var (
@ -41,7 +41,7 @@ type GCSFs struct {
}
func init() {
utils.AddFeature("+gcs")
version.AddFeature("+gcs")
}
// NewGCSFs returns an GCSFs object that allows to interact with Google Cloud Storage

View file

@ -5,11 +5,11 @@ package vfs
import (
"errors"
"github.com/drakkan/sftpgo/utils"
"github.com/drakkan/sftpgo/version"
)
func init() {
utils.AddFeature("-gcs")
version.AddFeature("-gcs")
}
// NewGCSFs returns an error, GCS is disabled

View file

@ -22,6 +22,7 @@ import (
"github.com/drakkan/sftpgo/logger"
"github.com/drakkan/sftpgo/metrics"
"github.com/drakkan/sftpgo/utils"
"github.com/drakkan/sftpgo/version"
)
// S3Fs is a Fs implementation for Amazon S3 compatible object storage.
@ -35,7 +36,7 @@ type S3Fs struct {
}
func init() {
utils.AddFeature("+s3")
version.AddFeature("+s3")
}
// NewS3Fs returns an S3Fs object that allows to interact with an s3 compatible

View file

@ -5,11 +5,11 @@ package vfs
import (
"errors"
"github.com/drakkan/sftpgo/utils"
"github.com/drakkan/sftpgo/version"
)
func init() {
utils.AddFeature("-s3")
version.AddFeature("-s3")
}
// NewS3Fs returns an error, S3 is disabled