2022-06-23 07:48:16 -05:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2023-10-03 07:54:20 -05:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2022-06-23 07:48:16 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetJsonData just gets the json in easier to work with type. It's used on multiple places which isn't super effective
|
|
|
|
// but only when creating a client which should not happen often anyway.
|
2023-08-30 10:46:47 -05:00
|
|
|
func GetJsonData(settings backend.DataSourceInstanceSettings) (map[string]any, error) {
|
|
|
|
var jsonData map[string]any
|
2022-06-23 07:48:16 -05:00
|
|
|
err := json.Unmarshal(settings.JSONData, &jsonData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error unmarshalling JSONData: %w", err)
|
|
|
|
}
|
|
|
|
return jsonData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartTrace setups a trace but does not panic if tracer is nil which helps with testing
|
2023-10-23 12:11:12 -05:00
|
|
|
func StartTrace(ctx context.Context, tracer trace.Tracer, name string, attributes ...attribute.KeyValue) (context.Context, func()) {
|
2022-06-23 07:48:16 -05:00
|
|
|
if tracer == nil {
|
|
|
|
return ctx, func() {}
|
|
|
|
}
|
2023-10-03 07:54:20 -05:00
|
|
|
ctx, span := tracer.Start(ctx, name, trace.WithAttributes(attributes...))
|
2022-06-23 07:48:16 -05:00
|
|
|
return ctx, func() {
|
|
|
|
span.End()
|
|
|
|
}
|
|
|
|
}
|