Skip to content

Commit

Permalink
Fix metrics handler signature (#463)
Browse files Browse the repository at this point in the history
* concept

* remove from ServeOpts and do breaking change

* use correct cast

* remove from manager

* add protobuf converters
  • Loading branch information
wbrowne authored Feb 9, 2022
1 parent c3c4584 commit 90b26ff
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
7 changes: 7 additions & 0 deletions backend/convert_from_protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@ func (f ConvertFromProtobuf) CheckHealthResponse(protoResp *pluginv2.CheckHealth
}
}

// CollectMetricsRequest converts protobuf version of a CollectMetricsRequest to the SDK version.
func (f ConvertFromProtobuf) CollectMetricsRequest(protoReq *pluginv2.CollectMetricsRequest) *CollectMetricsRequest {
return &CollectMetricsRequest{
PluginContext: f.PluginContext(protoReq.PluginContext),
}
}

// CollectMetricsResponse converts protobuf version of a CollectMetricsResponse to the SDK version.
func (f ConvertFromProtobuf) CollectMetricsResponse(protoResp *pluginv2.CollectMetricsResponse) *CollectMetricsResult {
var prometheusMetrics []byte
Expand Down
16 changes: 16 additions & 0 deletions backend/convert_to_protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,19 @@ func (t ConvertToProtobuf) StreamPacket(p *StreamPacket) *pluginv2.StreamPacket
}
return protoReq
}

// CollectMetricsRequest converts the SDK version of a CollectMetricsRequest to the protobuf version.
func (t ConvertToProtobuf) CollectMetricsRequest(req *CollectMetricsRequest) *pluginv2.CollectMetricsRequest {
return &pluginv2.CollectMetricsRequest{
PluginContext: t.PluginContext(req.PluginContext),
}
}

// CollectMetricsResult converts the SDK version of a CollectMetricsResult to the protobuf version.
func (t ConvertToProtobuf) CollectMetricsResult(res *CollectMetricsResult) *pluginv2.CollectMetricsResponse {
return &pluginv2.CollectMetricsResponse{
Metrics: &pluginv2.CollectMetricsResponse_Payload{
Prometheus: res.PrometheusMetrics,
},
}
}
13 changes: 9 additions & 4 deletions backend/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,23 @@ type CheckHealthResult struct {

// CollectMetricsHandler handles metric collection.
type CollectMetricsHandler interface {
CollectMetrics(ctx context.Context) (*CollectMetricsResult, error)
CollectMetrics(ctx context.Context, req *CollectMetricsRequest) (*CollectMetricsResult, error)
}

// CollectMetricsHandlerFunc is an adapter to allow the use of
// ordinary functions as backend.CollectMetricsHandler. If f is a function
// with the appropriate signature, CollectMetricsHandlerFunc(f) is a
// Handler that calls f.
type CollectMetricsHandlerFunc func(ctx context.Context) (*CollectMetricsResult, error)
type CollectMetricsHandlerFunc func(ctx context.Context, req *CollectMetricsRequest) (*CollectMetricsResult, error)

// CollectMetrics calls fn(ctx, req).
func (fn CollectMetricsHandlerFunc) CollectMetrics(ctx context.Context) (*CollectMetricsResult, error) {
return fn(ctx)
func (fn CollectMetricsHandlerFunc) CollectMetrics(ctx context.Context, req *CollectMetricsRequest) (*CollectMetricsResult, error) {
return fn(ctx, req)
}

// CollectMetricsRequest contains the metrics request
type CollectMetricsRequest struct {
PluginContext PluginContext
}

// CollectMetricsResult collect metrics result.
Expand Down

0 comments on commit 90b26ff

Please sign in to comment.