Skip to content

Commit

Permalink
fix logs
Browse files Browse the repository at this point in the history
Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
  • Loading branch information
jkroepke committed Jul 13, 2024
1 parent 8a8dac3 commit b005311
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 32 deletions.
1 change: 1 addition & 0 deletions .ci/config/tls/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ apps:
logo: ''
maxRenderWorkers: 2
persistData: false
remoteChromeAddr: ''
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true

[*.tsx]
indent_style = space
indent_size = 2
5 changes: 5 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,8 @@ services:
- IGNORE_HTTPS_ERRORS=true
ports:
- 8081
chrome:
image: chromedp/headless-shell:latest
shm_size: 2G
ports:
- 9222
2 changes: 1 addition & 1 deletion pkg/plugin/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func NewDashboardReporterApp(ctx context.Context, settings backend.AppInstanceSe
return nil, fmt.Errorf("error in http client options: %w", err)
}

if opts.TLS != nil {
if opts.TLS == nil {
opts.TLS = &httpclient.TLSOptions{}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/plugin/internal/chrome/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/chromedp/chromedp"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/mahendrapaipuri/grafana-dashboard-reporter-app/pkg/plugin/internal/config"
"golang.org/x/net/context"
)

Expand Down Expand Up @@ -56,7 +57,6 @@ func NewLocalBrowserInstance(ctx context.Context, logger log.Logger, insecureSki
// start a browser (and an empty tab) so we can add more tabs to the browser
chromeLogger := logger.With("subsystem", "chromium")
browserCtx, browserCtxCancel := chromedp.NewContext(allocCtx,
chromedp.WithDebugf(chromeLogger.Debug),
chromedp.WithErrorf(chromeLogger.Error),
chromedp.WithLogf(chromeLogger.Debug),
)
Expand All @@ -72,7 +72,7 @@ func NewLocalBrowserInstance(ctx context.Context, logger log.Logger, insecureSki
}, nil
}

func (i *LocalInstance) NewTab(_ context.Context, _ log.Logger) *Tab {
func (i *LocalInstance) NewTab(ctx context.Context, logger log.Logger, _ config.Config) *Tab {
// start a browser (and an empty tab) so we can add more tabs to the browser
ctx, cancel := chromedp.NewContext(i.browserCtx)

Expand Down
3 changes: 2 additions & 1 deletion pkg/plugin/internal/chrome/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/mahendrapaipuri/grafana-dashboard-reporter-app/pkg/plugin/internal/config"
"golang.org/x/net/context"
)

Expand All @@ -29,7 +30,7 @@ type PDFOptions struct {
}

type Instance interface {
NewTab(ctx context.Context, logger log.Logger) *Tab
NewTab(ctx context.Context, logger log.Logger, conf config.Config) *Tab
Close()
}

Expand Down
39 changes: 39 additions & 0 deletions pkg/plugin/internal/chrome/remote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package chrome

import (
"github.com/chromedp/chromedp"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/mahendrapaipuri/grafana-dashboard-reporter-app/pkg/plugin/internal/config"
"golang.org/x/net/context"
)

type RemoteInstance struct {
browserCtx context.Context

browserCtxCancel context.CancelFunc
allocCtxCancel context.CancelFunc
}

// NewRemoteBrowserInstance creates a new remote browser instance
func NewRemoteBrowserInstance(_ context.Context, _ log.Logger, _ bool) (*RemoteInstance, error) {
// we don't need to do anything here. We are connecting to a remote browser if needed.
return &RemoteInstance{}, nil
}

func (i *RemoteInstance) NewTab(ctx context.Context, logger log.Logger, conf config.Config) *Tab {
allocCtx, allocCtxCancel := chromedp.NewRemoteAllocator(ctx, conf.RemoteChromeAddr)

chromeLogger := logger.With("subsystem", "chromium")
browserCtx, browserCtxCancel := chromedp.NewContext(allocCtx,
chromedp.WithErrorf(chromeLogger.Error),
chromedp.WithLogf(chromeLogger.Debug),
)

return &Tab{
parentCtxCancel: allocCtxCancel,
ctx: browserCtx,
cancel: browserCtxCancel,
}
}

func (i *RemoteInstance) Close() {}
9 changes: 7 additions & 2 deletions pkg/plugin/internal/chrome/tab.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ import (
)

type Tab struct {
ctx context.Context
cancel context.CancelFunc
parentCtxCancel context.CancelFunc
ctx context.Context
cancel context.CancelFunc
}

func (t *Tab) Close() {
if t.cancel != nil {
t.cancel()
}

if t.parentCtxCancel != nil {
t.cancel()
}
}

func (t *Tab) Run(actions chromedp.Action) error {
Expand Down
6 changes: 3 additions & 3 deletions pkg/plugin/internal/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func init() {
getPanelRetrySleepTime = time.Duration(1) * time.Millisecond
}

func TestGrafanaClientFetchesDashboard(t *testing.T) {
func TestGrafanaClientFetchesDashboardWithLocalChrome(t *testing.T) {
// Skip test if chrome is not available
_, err := exec.LookPath("chrome")
if err != nil {
Expand All @@ -44,7 +44,7 @@ func TestGrafanaClientFetchesDashboard(t *testing.T) {
defer ts.Close()

Convey("When using the Grafana httpClient", func() {
credential := Credential{HeaderName: backend.CookiesHeaderName, HeaderValue: requestCookie}
credential := Credential{HeaderName: backend.CookiesHeaderName, HeaderValue: "cookie"}
conf := &config.Config{
Layout: "simple",
DashboardMode: "default",
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestGrafanaClientFetchesPanelPNG(t *testing.T) {
}))
defer ts.Close()

credential := Credential{HeaderName: backend.OAuthIdentityTokenHeaderName, HeaderValue: "token"}
credential := Credential{HeaderName: backend.OAuthIdentityTokenHeaderName, HeaderValue: "Bearer token"}
conf := &config.Config{
Layout: "simple",
DashboardMode: "default",
Expand Down
3 changes: 3 additions & 0 deletions pkg/plugin/internal/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import (

// Config contains plugin settings
type Config struct {
URL string `json:"url"`
TLSSkipVerify string `json:"tlsSkipVerify"`
Orientation string `json:"orientation"`
Layout string `json:"layout"`
DashboardMode string `json:"dashboardMode"`
TimeZone string `json:"timeZone"`
EncodedLogo string `json:"logo"`
MaxRenderWorkers int `json:"maxRenderWorkers"`
RemoteChromeAddr string `json:"remoteChromeAddr"`
IncludePanelIDs []int
ExcludePanelIDs []int

Expand Down
22 changes: 13 additions & 9 deletions pkg/plugin/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,6 @@ func (a *App) handleReport(w http.ResponseWriter, req *http.Request) {

// Get Grafana config from context
grafanaConfig := backend.GrafanaConfigFromContext(req.Context())

grafanaAppURL, err := grafanaConfig.AppURL()
if err != nil {
ctxLogger.Error("failed to get app URL", "err", err)
http.Error(w, "failed to get app URL", http.StatusInternalServerError)

return
}

conf, err := config.Load(
pluginConfig.AppInstanceSettings.JSONData,
pluginConfig.AppInstanceSettings.DecryptedSecureJSONData,
Expand All @@ -85,6 +76,19 @@ func (a *App) handleReport(w http.ResponseWriter, req *http.Request) {
return
}

var grafanaAppURL string
if conf.URL != "" {
grafanaAppURL = conf.URL
} else {
grafanaAppURL, err = grafanaConfig.AppURL()
if err != nil {
ctxLogger.Error("failed to get app URL", "err", err)
http.Error(w, "failed to get app URL", http.StatusInternalServerError)

return
}
}

var credential client.Credential

switch {
Expand Down
1 change: 0 additions & 1 deletion pkg/plugin/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func (m mockReport) Title() string { return "title" }
func TestReportResource(t *testing.T) {
// Set appURL env variable
t.Setenv("GF_APP_URL", "http://localhost:3000")
t.Setenv("GF_PATHS_DATA", t.TempDir())

// Initialize app
inst, err := NewDashboardReporterApp(context.Background(), backend.AppInstanceSettings{})
Expand Down
3 changes: 3 additions & 0 deletions provisioning/plugins/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,6 @@ apps:
# applies globally to all generated reports
#
maxRenderWorkers: 2

# A remote address for a running chrome instance. If empty, a local chrome browser will be executed
remoteChromeAddr: 'ws://chrome:9222/'
Loading

0 comments on commit b005311

Please sign in to comment.