Skip to content

Commit

Permalink
add build tag to disable metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
drakkan committed Jun 19, 2020
1 parent b30614e commit 23a80b0
Show file tree
Hide file tree
Showing 29 changed files with 189 additions and 109 deletions.
4 changes: 2 additions & 2 deletions cmd/portable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions cmd/portable_disabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
5 changes: 2 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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}}
`)
}
Expand Down
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion dataprovider/bolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -57,7 +58,7 @@ type compatUserV2 struct {
}

func init() {
utils.AddFeature("+bolt")
version.AddFeature("+bolt")
}

func initializeBoltProvider(basePath string) error {
Expand Down
4 changes: 2 additions & 2 deletions dataprovider/bolt_disabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions dataprovider/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -45,7 +45,7 @@ type MySQLProvider struct {
}

func init() {
utils.AddFeature("+mysql")
version.AddFeature("+mysql")
}

func initializeMySQLProvider() error {
Expand Down
4 changes: 2 additions & 2 deletions dataprovider/mysql_disabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions dataprovider/pgsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -44,7 +44,7 @@ type PGSQLProvider struct {
}

func init() {
utils.AddFeature("+pgsql")
version.AddFeature("+pgsql")
}

func initializePGSQLProvider() error {
Expand Down
4 changes: 2 additions & 2 deletions dataprovider/pgsql_disabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion dataprovider/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -69,7 +70,7 @@ type SQLiteProvider struct {
}

func init() {
utils.AddFeature("+sqlite")
version.AddFeature("+sqlite")
}

func initializeSQLiteProvider(basePath string) error {
Expand Down
4 changes: 2 additions & 2 deletions dataprovider/sqlite_disabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion docker/sftpgo/alpine/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion docker/sftpgo/debian/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions docs/build-from-source.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
```
11 changes: 6 additions & 5 deletions httpd/api_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions httpd/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions httpd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -143,7 +144,6 @@ func loadTemplates(templatesPath string) {
}

func getBasePageData(title, currentURL string) basePage {
version := utils.GetAppVersion()
return basePage{
Title: title,
CurrentURL: currentURL,
Expand All @@ -160,7 +160,7 @@ func getBasePageData(title, currentURL string) basePage {
UsersTitle: pageUsersTitle,
ConnectionsTitle: pageConnectionsTitle,
FoldersTitle: pageFoldersTitle,
Version: version.GetVersionAsString(),
Version: version.GetAsString(),
}
}

Expand Down
14 changes: 11 additions & 3 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build !nometrics

// Package metrics provides Prometheus metrics support
package metrics

Expand All @@ -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 (
Expand All @@ -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 (
Expand Down Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit 23a80b0

Please sign in to comment.