Skip to content

Commit

Permalink
add instance metrics (#711)
Browse files Browse the repository at this point in the history
  • Loading branch information
wbrowne authored Jun 30, 2023
1 parent 8ea68e1 commit 7dd9759
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
12 changes: 12 additions & 0 deletions backend/datasource/instance_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@ import (
"context"
"fmt"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"

"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
"github.com/grafana/grafana-plugin-sdk-go/internal/tenant"
)

var (
datasourceInstancesCreated = promauto.NewCounter(prometheus.CounterOpts{
Namespace: "plugins",
Name: "datasource_instances_total",
Help: "The total number of data source instances created",
})
)

// InstanceFactoryFunc factory method for creating data source instances.
type InstanceFactoryFunc func(settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error)

Expand Down Expand Up @@ -62,5 +73,6 @@ func (ip *instanceProvider) NeedsUpdate(_ context.Context, pluginContext backend
}

func (ip *instanceProvider) NewInstance(_ context.Context, pluginContext backend.PluginContext) (instancemgmt.Instance, error) {
datasourceInstancesCreated.Inc()
return ip.factory(*pluginContext.DataSourceInstanceSettings)
}
13 changes: 13 additions & 0 deletions backend/instancemgmt/instance_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@ import (
"reflect"
"sync"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"

"github.com/grafana/grafana-plugin-sdk-go/backend"
)

var (
activeInstances = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "plugins",
Name: "active_instances",
Help: "The number of active plugin instances",
})
)

// Instance is a marker interface for an instance.
type Instance interface{}

Expand Down Expand Up @@ -109,6 +120,7 @@ func (im *instanceManager) Get(ctx context.Context, pluginContext backend.Plugin

if disposer, valid := ci.instance.(InstanceDisposer); valid {
disposer.Dispose()
activeInstances.Dec()
}
}

Expand All @@ -120,6 +132,7 @@ func (im *instanceManager) Get(ctx context.Context, pluginContext backend.Plugin
PluginContext: pluginContext,
instance: instance,
})
activeInstances.Inc()

return instance, nil
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/chromedp/cdproto v0.0.0-20220208224320-6efb837e6bc2
github.com/elazarl/goproxy v0.0.0-20220115173737-adb46da277ac
github.com/getkin/kin-openapi v0.112.0
github.com/go-jose/go-jose/v3 v3.0.0
github.com/google/uuid v1.3.0
github.com/unknwon/bra v0.0.0-20200517080246-1e3013ecaff8
github.com/urfave/cli v1.22.12
Expand All @@ -56,7 +57,6 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/elazarl/goproxy/ext v0.0.0-20220115173737-adb46da277ac // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/go-jose/go-jose/v3 v3.0.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
Expand Down

0 comments on commit 7dd9759

Please sign in to comment.