Skip to content

Commit

Permalink
Merge branch 'master' into 12918-only-arrow-functions
Browse files Browse the repository at this point in the history
  • Loading branch information
torkelo committed Sep 5, 2018
2 parents 0e007d5 + 72ab24f commit 151e950
Show file tree
Hide file tree
Showing 111 changed files with 3,480 additions and 686 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* **Alerting**: Notification reminders [#7330](https://github.com/grafana/grafana/issues/7330), thx [@jbaublitz](https://github.com/jbaublitz)
* **Dashboard**: TV & Kiosk mode changes, new cycle view mode button in dashboard toolbar [#13025](https://github.com/grafana/grafana/pull/13025)
* **OAuth**: Gitlab OAuth with support for filter by groups [#5623](https://github.com/grafana/grafana/issues/5623), thx [@BenoitKnecht](https://github.com/BenoitKnecht)
* **Postgres**: Graphical query builder [#10095](https://github.com/grafana/grafana/issues/10095), thx [svenklemm](https://github.com/svenklemm)

### New Features

Expand All @@ -19,7 +20,9 @@

### Minor

* **Units**: Adds bitcoin axes unit. [#13125](https://github.com/grafana/grafana/pull/13125)
* **GrafanaCli**: Fixed issue with grafana-cli install plugin resulting in corrupt http response from source error. Fixes [#13079](https://github.com/grafana/grafana/issues/13079)
* **Logging**: Reopen log files after receiving a SIGHUP signal [#13112](https://github.com/grafana/grafana/pull/13112), thx [@filewalkwithme](https://github.com/filewalkwithme)
* **Api**: Delete nonexistent datasource should return 404 [#12313](https://github.com/grafana/grafana/issues/12313), thx [@AustinWinstanley](https://github.com/AustinWinstanley)
* **Dashboard**: Fix selecting current dashboard from search should not reload dashboard [#12248](https://github.com/grafana/grafana/issues/12248)
* **Dashboard**: Use uid when linking to dashboards internally in a dashboard [#10705](https://github.com/grafana/grafana/issues/10705)
Expand Down
5 changes: 5 additions & 0 deletions conf/defaults.ini
Original file line number Diff line number Diff line change
Expand Up @@ -538,3 +538,8 @@ container_name =

[external_image_storage.local]
# does not require any configuration

[rendering]
# Options to configure external image rendering server like https://github.com/grafana/grafana-image-renderer
server_url =
callback_url =
5 changes: 5 additions & 0 deletions conf/sample.ini
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,8 @@ log_queries =

[external_image_storage.local]
# does not require any configuration

[rendering]
# Options to configure external image rendering server like https://github.com/grafana/grafana-image-renderer
;server_url =
;callback_url =
4 changes: 3 additions & 1 deletion docs/sources/administration/provisioning.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ Since not all datasources have the same configuration settings we only have the
| tlsSkipVerify | boolean | *All* | Controls whether a client verifies the server's certificate chain and host name. |
| graphiteVersion | string | Graphite | Graphite version |
| timeInterval | string | Elastic, InfluxDB & Prometheus | Lowest interval/step value that should be used for this data source |
| esVersion | number | Elastic | Elasticsearch version as an number (2/5/56) |
| esVersion | number | Elastic | Elasticsearch version as a number (2/5/56) |
| timeField | string | Elastic | Which field that should be used as timestamp |
| interval | string | Elastic | Index date time format |
| authType | string | Cloudwatch | Auth provider. keys/credentials/arn |
Expand All @@ -165,6 +165,8 @@ Since not all datasources have the same configuration settings we only have the
| tsdbVersion | string | OpenTSDB | Version |
| tsdbResolution | string | OpenTSDB | Resolution |
| sslmode | string | PostgreSQL | SSLmode. 'disable', 'require', 'verify-ca' or 'verify-full' |
| postgresVersion | number | PostgreSQL | Postgres version as a number (903/904/905/906/1000) meaning v9.3, v9.4, ..., v10 |
| timescaledb | boolean | PostgreSQL | Enable usage of TimescaleDB extension |

#### Secure Json Data

Expand Down
5 changes: 4 additions & 1 deletion docs/sources/features/datasources/postgres.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ Name | Description
*User* | Database user's login/username
*Password* | Database user's password
*SSL Mode* | This option determines whether or with what priority a secure SSL TCP/IP connection will be negotiated with the server.
*TimescaleDB* | With this option enabled Grafana will use TimescaleDB features, e.g. use ```time_bucket``` for grouping by time (only available in Grafana 5.3+).
*Version* | This option determines which functions are available in the query builder (only available in Grafana 5.3+).
*TimescaleDB* | TimescaleDB is a time-series database built as a PostgreSQL extension. If enabled, Grafana will use `time_bucket` in the `$__timeGroup` macro and display TimescaleDB specific aggregate functions in the query builder (only available in Grafana 5.3+).


### Database User Permissions (Important!)

Expand Down Expand Up @@ -292,5 +294,6 @@ datasources:
password: "Password!"
jsonData:
sslmode: "disable" # disable/require/verify-ca/verify-full
postgresVersion: 903 # 903=9.3, 904=9.4, 905=9.5, 906=9.6, 1000=10
timescaledb: false
```
14 changes: 9 additions & 5 deletions pkg/cmd/grafana-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,17 @@ func main() {

func listenToSystemSignals(server *GrafanaServerImpl) {
signalChan := make(chan os.Signal, 1)
ignoreChan := make(chan os.Signal, 1)
sighupChan := make(chan os.Signal, 1)

signal.Notify(ignoreChan, syscall.SIGHUP)
signal.Notify(sighupChan, syscall.SIGHUP)
signal.Notify(signalChan, os.Interrupt, os.Kill, syscall.SIGTERM)

select {
case sig := <-signalChan:
server.Shutdown(fmt.Sprintf("System signal: %s", sig))
for {
select {
case _ = <-sighupChan:
log.Reload()
case sig := <-signalChan:
server.Shutdown(fmt.Sprintf("System signal: %s", sig))
}
}
}
17 changes: 17 additions & 0 deletions pkg/log/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,20 @@ func (w *FileLogWriter) Close() {
func (w *FileLogWriter) Flush() {
w.mw.fd.Sync()
}

// Reload file logger
func (w *FileLogWriter) Reload() {
// block Logger's io.Writer
w.mw.Lock()
defer w.mw.Unlock()

// Close
fd := w.mw.fd
fd.Close()

// Open again
err := w.StartLogger()
if err != nil {
fmt.Fprintf(os.Stderr, "Reload StartLogger: %s\n", err)
}
}
4 changes: 4 additions & 0 deletions pkg/log/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ package log
type DisposableHandler interface {
Close()
}

type ReloadableHandler interface {
Reload()
}
9 changes: 9 additions & 0 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import (

var Root log15.Logger
var loggersToClose []DisposableHandler
var loggersToReload []ReloadableHandler
var filters map[string]log15.Lvl

func init() {
loggersToClose = make([]DisposableHandler, 0)
loggersToReload = make([]ReloadableHandler, 0)
Root = log15.Root()
Root.SetHandler(log15.DiscardHandler())
}
Expand Down Expand Up @@ -115,6 +117,12 @@ func Close() {
loggersToClose = make([]DisposableHandler, 0)
}

func Reload() {
for _, logger := range loggersToReload {
logger.Reload()
}
}

func GetLogLevelFor(name string) Lvl {
if level, ok := filters[name]; ok {
switch level {
Expand Down Expand Up @@ -230,6 +238,7 @@ func ReadLoggingConfig(modes []string, logsPath string, cfg *ini.File) {
fileHandler.Init()

loggersToClose = append(loggersToClose, fileHandler)
loggersToReload = append(loggersToReload, fileHandler)
handler = fileHandler
case "syslog":
sysLogHandler := NewSyslog(sec, format)
Expand Down
46 changes: 37 additions & 9 deletions pkg/services/rendering/http_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rendering

import (
"context"
"fmt"
"io"
"net"
"net/http"
Expand All @@ -20,25 +21,24 @@ var netTransport = &http.Transport{
TLSHandshakeTimeout: 5 * time.Second,
}

var netClient = &http.Client{
Transport: netTransport,
}

func (rs *RenderingService) renderViaHttp(ctx context.Context, opts Opts) (*RenderResult, error) {
filePath := rs.getFilePathForNewImage()

var netClient = &http.Client{
Timeout: opts.Timeout,
Transport: netTransport,
}

rendererUrl, err := url.Parse(rs.Cfg.RendererUrl)
if err != nil {
return nil, err
}

queryParams := rendererUrl.Query()
queryParams.Add("url", rs.getURL(opts.Path))
queryParams.Add("renderKey", rs.getRenderKey(opts.UserId, opts.OrgId, opts.OrgRole))
queryParams.Add("renderKey", rs.getRenderKey(opts.OrgId, opts.UserId, opts.OrgRole))
queryParams.Add("width", strconv.Itoa(opts.Width))
queryParams.Add("height", strconv.Itoa(opts.Height))
queryParams.Add("domain", rs.getLocalDomain())
queryParams.Add("domain", rs.domain)
queryParams.Add("timezone", isoTimeOffsetToPosixTz(opts.Timezone))
queryParams.Add("encoding", opts.Encoding)
queryParams.Add("timeout", strconv.Itoa(int(opts.Timeout.Seconds())))
Expand All @@ -49,20 +49,48 @@ func (rs *RenderingService) renderViaHttp(ctx context.Context, opts Opts) (*Rend
return nil, err
}

reqContext, cancel := context.WithTimeout(ctx, opts.Timeout+time.Second*2)
defer cancel()

req = req.WithContext(reqContext)

// make request to renderer server
resp, err := netClient.Do(req)
if err != nil {
return nil, err
rs.log.Error("Failed to send request to remote rendering service.", "error", err)
return nil, fmt.Errorf("Failed to send request to remote rendering service. %s", err)
}

// save response to file
defer resp.Body.Close()

// check for timeout first
if reqContext.Err() == context.DeadlineExceeded {
rs.log.Info("Rendering timed out")
return nil, ErrTimeout
}

// if we didnt get a 200 response, something went wrong.
if resp.StatusCode != http.StatusOK {
rs.log.Error("Remote rendering request failed", "error", resp.Status)
return nil, fmt.Errorf("Remote rendering request failed. %d: %s", resp.StatusCode, resp.Status)
}

out, err := os.Create(filePath)
if err != nil {
return nil, err
}
defer out.Close()
io.Copy(out, resp.Body)
_, err = io.Copy(out, resp.Body)
if err != nil {
// check that we didnt timeout while receiving the response.
if reqContext.Err() == context.DeadlineExceeded {
rs.log.Info("Rendering timed out")
return nil, ErrTimeout
}
rs.log.Error("Remote rendering request failed", "error", err)
return nil, fmt.Errorf("Remote rendering request failed. %s", err)
}

return &RenderResult{FilePath: filePath}, err
}
2 changes: 1 addition & 1 deletion pkg/services/rendering/phantomjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (rs *RenderingService) renderViaPhantomJS(ctx context.Context, opts Opts) (
fmt.Sprintf("width=%v", opts.Width),
fmt.Sprintf("height=%v", opts.Height),
fmt.Sprintf("png=%v", pngPath),
fmt.Sprintf("domain=%v", rs.getLocalDomain()),
fmt.Sprintf("domain=%v", rs.domain),
fmt.Sprintf("timeout=%v", opts.Timeout.Seconds()),
fmt.Sprintf("renderKey=%v", renderKey),
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/services/rendering/plugin_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ func (rs *RenderingService) renderViaPlugin(ctx context.Context, opts Opts) (*Re
Height: int32(opts.Height),
FilePath: pngPath,
Timeout: int32(opts.Timeout.Seconds()),
RenderKey: rs.getRenderKey(opts.UserId, opts.OrgId, opts.OrgRole),
RenderKey: rs.getRenderKey(opts.OrgId, opts.UserId, opts.OrgRole),
Encoding: opts.Encoding,
Timezone: isoTimeOffsetToPosixTz(opts.Timezone),
Domain: rs.getLocalDomain(),
Domain: rs.domain,
})

if err != nil {
Expand Down
38 changes: 30 additions & 8 deletions pkg/services/rendering/rendering.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package rendering
import (
"context"
"fmt"
"net/url"
"os"
"path/filepath"

plugin "github.com/hashicorp/go-plugin"
Expand All @@ -27,12 +29,31 @@ type RenderingService struct {
grpcPlugin pluginModel.RendererPlugin
pluginInfo *plugins.RendererPlugin
renderAction renderFunc
domain string

Cfg *setting.Cfg `inject:""`
}

func (rs *RenderingService) Init() error {
rs.log = log.New("rendering")

// ensure ImagesDir exists
err := os.MkdirAll(rs.Cfg.ImagesDir, 0700)
if err != nil {
return err
}

// set value used for domain attribute of renderKey cookie
if rs.Cfg.RendererUrl != "" {
// RendererCallbackUrl has already been passed, it wont generate an error.
u, _ := url.Parse(rs.Cfg.RendererCallbackUrl)
rs.domain = u.Hostname()
} else if setting.HttpAddr != setting.DEFAULT_HTTP_ADDR {
rs.domain = setting.HttpAddr
} else {
rs.domain = "localhost"
}

return nil
}

Expand Down Expand Up @@ -82,16 +103,17 @@ func (rs *RenderingService) getFilePathForNewImage() string {
}

func (rs *RenderingService) getURL(path string) string {
// &render=1 signals to the legacy redirect layer to
return fmt.Sprintf("%s://%s:%s/%s&render=1", setting.Protocol, rs.getLocalDomain(), setting.HttpPort, path)
}
if rs.Cfg.RendererUrl != "" {
// The backend rendering service can potentially be remote.
// So we need to use the root_url to ensure the rendering service
// can reach this Grafana instance.

func (rs *RenderingService) getLocalDomain() string {
if setting.HttpAddr != setting.DEFAULT_HTTP_ADDR {
return setting.HttpAddr
}
// &render=1 signals to the legacy redirect layer to
return fmt.Sprintf("%s%s&render=1", rs.Cfg.RendererCallbackUrl, path)

return "localhost"
}
// &render=1 signals to the legacy redirect layer to
return fmt.Sprintf("%s://%s:%s/%s&render=1", setting.Protocol, rs.domain, setting.HttpPort, path)
}

func (rs *RenderingService) getRenderKey(orgId, userId int64, orgRole models.RoleType) string {
Expand Down
13 changes: 13 additions & 0 deletions pkg/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ type Cfg struct {
ImagesDir string
PhantomDir string
RendererUrl string
RendererCallbackUrl string
DisableBruteForceLoginProtection bool

TempDataLifetime time.Duration
Expand Down Expand Up @@ -641,6 +642,18 @@ func (cfg *Cfg) Load(args *CommandLineArgs) error {
// Rendering
renderSec := iniFile.Section("rendering")
cfg.RendererUrl = renderSec.Key("server_url").String()
cfg.RendererCallbackUrl = renderSec.Key("callback_url").String()
if cfg.RendererCallbackUrl == "" {
cfg.RendererCallbackUrl = AppUrl
} else {
if cfg.RendererCallbackUrl[len(cfg.RendererCallbackUrl)-1] != '/' {
cfg.RendererCallbackUrl += "/"
}
_, err := url.Parse(cfg.RendererCallbackUrl)
if err != nil {
log.Fatal(4, "Invalid callback_url(%s): %s", cfg.RendererCallbackUrl, err)
}
}
cfg.ImagesDir = filepath.Join(DataPath, "png")
cfg.PhantomDir = filepath.Join(HomePath, "tools/phantomjs")
cfg.TempDataLifetime = iniFile.Section("paths").Key("temp_data_lifetime").MustDuration(time.Second * 3600 * 24)
Expand Down
11 changes: 11 additions & 0 deletions pkg/setting/setting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestLoadingSettings(t *testing.T) {
So(err, ShouldBeNil)

So(AdminUser, ShouldEqual, "admin")
So(cfg.RendererCallbackUrl, ShouldEqual, "http://localhost:3000/")
})

Convey("Should be able to override via environment variables", func() {
Expand Down Expand Up @@ -178,5 +179,15 @@ func TestLoadingSettings(t *testing.T) {
So(InstanceName, ShouldEqual, hostname)
})

Convey("Reading callback_url should add trailing slash", func() {
cfg := NewCfg()
cfg.Load(&CommandLineArgs{
HomePath: "../../",
Args: []string{"cfg:rendering.callback_url=http://myserver/renderer"},
})

So(cfg.RendererCallbackUrl, ShouldEqual, "http://myserver/renderer/")
})

})
}
Loading

0 comments on commit 151e950

Please sign in to comment.