Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/go_modules/dagger.io/dagger-0.12.7
Browse files Browse the repository at this point in the history
  • Loading branch information
aangelisc authored Sep 6, 2024
2 parents c1cf707 + 29bacc5 commit 7d40624
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Features

- Datasource OTel configuration will now set default table names for logs and traces.

### Fixes

- Added warning for when `uid` is missing in provisioned datasources.
Expand Down
3 changes: 2 additions & 1 deletion cspell.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"WorkDir",
"elazarl",
"goproxy",
"Nonproxy"
"Nonproxy",
"errorsource"
]
}
13 changes: 7 additions & 6 deletions pkg/macros/macros.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/grafana/sqlds/v3"

"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
)

// Converts a time.Time to a Date
Expand Down Expand Up @@ -48,7 +49,7 @@ func ToTimeFilterMs(query *sqlutil.Query, args []string) (string, error) {

func TimeFilter(query *sqlutil.Query, args []string) (string, error) {
if len(args) != 1 {
return "", fmt.Errorf("%w: expected 1 argument, received %d", sqlds.ErrorBadArgumentCount, len(args))
return "", errorsource.DownstreamError(fmt.Errorf("%w: expected 1 argument, received %d", sqlds.ErrorBadArgumentCount, len(args)), false)
}

var (
Expand All @@ -62,7 +63,7 @@ func TimeFilter(query *sqlutil.Query, args []string) (string, error) {

func TimeFilterMs(query *sqlutil.Query, args []string) (string, error) {
if len(args) != 1 {
return "", fmt.Errorf("%w: expected 1 argument, received %d", sqlds.ErrorBadArgumentCount, len(args))
return "", errorsource.DownstreamError(fmt.Errorf("%w: expected 1 argument, received %d", sqlds.ErrorBadArgumentCount, len(args)), false)
}

var (
Expand All @@ -76,7 +77,7 @@ func TimeFilterMs(query *sqlutil.Query, args []string) (string, error) {

func DateFilter(query *sqlutil.Query, args []string) (string, error) {
if len(args) != 1 {
return "", fmt.Errorf("%w: expected 1 argument, received %d", sqlds.ErrorBadArgumentCount, len(args))
return "", errorsource.DownstreamError(fmt.Errorf("%w: expected 1 argument, received %d", sqlds.ErrorBadArgumentCount, len(args)), false)
}
var (
column = args[0]
Expand All @@ -89,7 +90,7 @@ func DateFilter(query *sqlutil.Query, args []string) (string, error) {

func DateTimeFilter(query *sqlutil.Query, args []string) (string, error) {
if len(args) != 2 {
return "", fmt.Errorf("%w: expected 2 arguments, received %d", sqlds.ErrorBadArgumentCount, len(args))
return "", errorsource.DownstreamError(fmt.Errorf("%w: expected 2 arguments, received %d", sqlds.ErrorBadArgumentCount, len(args)), false)
}
var (
dateColumn = args[0]
Expand All @@ -105,7 +106,7 @@ func DateTimeFilter(query *sqlutil.Query, args []string) (string, error) {

func TimeInterval(query *sqlutil.Query, args []string) (string, error) {
if len(args) != 1 {
return "", fmt.Errorf("%w: expected 1 argument, received %d", sqlds.ErrorBadArgumentCount, len(args))
return "", errorsource.DownstreamError(fmt.Errorf("%w: expected 1 argument, received %d", sqlds.ErrorBadArgumentCount, len(args)), false)
}

seconds := math.Max(query.Interval.Seconds(), 1)
Expand All @@ -114,7 +115,7 @@ func TimeInterval(query *sqlutil.Query, args []string) (string, error) {

func TimeIntervalMs(query *sqlutil.Query, args []string) (string, error) {
if len(args) != 1 {
return "", fmt.Errorf("%w: expected 1 argument, received %d", sqlds.ErrorBadArgumentCount, len(args))
return "", errorsource.DownstreamError(fmt.Errorf("%w: expected 1 argument, received %d", sqlds.ErrorBadArgumentCount, len(args)), false)
}

milliseconds := math.Max(float64(query.Interval.Milliseconds()), 1)
Expand Down
7 changes: 4 additions & 3 deletions pkg/plugin/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/build"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
"github.com/grafana/sqlds/v3"
"github.com/pkg/errors"
"golang.org/x/net/proxy"
Expand All @@ -40,7 +41,7 @@ func getTLSConfig(settings Settings) (*tls.Config, error) {
if settings.TlsAuthWithCACert && len(settings.TlsCACert) > 0 {
caPool := x509.NewCertPool()
if ok := caPool.AppendCertsFromPEM([]byte(settings.TlsCACert)); !ok {
return nil, ErrorInvalidCACertificate
return nil, errorsource.DownstreamError(ErrorInvalidCACertificate, false)
}
tlsConfig.RootCAs = caPool
}
Expand Down Expand Up @@ -120,11 +121,11 @@ func (h *Clickhouse) Connect(ctx context.Context, config backend.DataSourceInsta
}
t, err := strconv.Atoi(settings.DialTimeout)
if err != nil {
return nil, errors.New(fmt.Sprintf("invalid timeout: %s", settings.DialTimeout))
return nil, errorsource.DownstreamError(errors.New(fmt.Sprintf("invalid timeout: %s", settings.DialTimeout)), false)
}
qt, err := strconv.Atoi(settings.QueryTimeout)
if err != nil {
return nil, errors.New(fmt.Sprintf("invalid query timeout: %s", settings.QueryTimeout))
return nil, errorsource.DownstreamError(errors.New(fmt.Sprintf("invalid query timeout: %s", settings.QueryTimeout)), false)
}
protocol := clickhouse.Native
if settings.Protocol == "http" {
Expand Down
17 changes: 9 additions & 8 deletions pkg/plugin/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

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

// Settings - data loaded from grafana settings database
Expand Down Expand Up @@ -52,10 +53,10 @@ const secureHeaderKeyPrefix = "secureHttpHeaders."

func (settings *Settings) isValid() (err error) {
if settings.Host == "" {
return ErrorMessageInvalidHost
return errorsource.DownstreamError(ErrorMessageInvalidHost, false)
}
if settings.Port == 0 {
return ErrorMessageInvalidPort
return errorsource.DownstreamError(ErrorMessageInvalidPort, false)
}
return nil
}
Expand All @@ -79,7 +80,7 @@ func LoadSettings(ctx context.Context, config backend.DataSourceInstanceSettings
if port, ok := jsonData["port"].(string); ok {
settings.Port, err = strconv.ParseInt(port, 0, 64)
if err != nil {
return settings, fmt.Errorf("could not parse port value: %w", err)
return settings, errorsource.DownstreamError(fmt.Errorf("could not parse port value: %w", err), false)
}
} else {
settings.Port = int64(jsonData["port"].(float64))
Expand All @@ -92,7 +93,7 @@ func LoadSettings(ctx context.Context, config backend.DataSourceInstanceSettings
if secure, ok := jsonData["secure"].(string); ok {
settings.Secure, err = strconv.ParseBool(secure)
if err != nil {
return settings, fmt.Errorf("could not parse secure value: %w", err)
return settings, errorsource.DownstreamError(fmt.Errorf("could not parse secure value: %w", err), false)
}
} else {
settings.Secure = jsonData["secure"].(bool)
Expand All @@ -106,7 +107,7 @@ func LoadSettings(ctx context.Context, config backend.DataSourceInstanceSettings
if tlsSkipVerify, ok := jsonData["tlsSkipVerify"].(string); ok {
settings.InsecureSkipVerify, err = strconv.ParseBool(tlsSkipVerify)
if err != nil {
return settings, fmt.Errorf("could not parse tlsSkipVerify value: %w", err)
return settings, errorsource.DownstreamError(fmt.Errorf("could not parse tlsSkipVerify value: %w", err), false)
}
} else {
settings.InsecureSkipVerify = jsonData["tlsSkipVerify"].(bool)
Expand All @@ -116,7 +117,7 @@ func LoadSettings(ctx context.Context, config backend.DataSourceInstanceSettings
if tlsAuth, ok := jsonData["tlsAuth"].(string); ok {
settings.TlsClientAuth, err = strconv.ParseBool(tlsAuth)
if err != nil {
return settings, fmt.Errorf("could not parse tlsAuth value: %w", err)
return settings, errorsource.DownstreamError(fmt.Errorf("could not parse tlsAuth value: %w", err), false)
}
} else {
settings.TlsClientAuth = jsonData["tlsAuth"].(bool)
Expand All @@ -126,7 +127,7 @@ func LoadSettings(ctx context.Context, config backend.DataSourceInstanceSettings
if tlsAuthWithCACert, ok := jsonData["tlsAuthWithCACert"].(string); ok {
settings.TlsAuthWithCACert, err = strconv.ParseBool(tlsAuthWithCACert)
if err != nil {
return settings, fmt.Errorf("could not parse tlsAuthWithCACert value: %w", err)
return settings, errorsource.DownstreamError(fmt.Errorf("could not parse tlsAuthWithCACert value: %w", err), false)
}
} else {
settings.TlsAuthWithCACert = jsonData["tlsAuthWithCACert"].(bool)
Expand Down Expand Up @@ -174,7 +175,7 @@ func LoadSettings(ctx context.Context, config backend.DataSourceInstanceSettings
if forwardGrafanaHeaders, ok := jsonData["forwardGrafanaHeaders"].(string); ok {
settings.ForwardGrafanaHeaders, err = strconv.ParseBool(forwardGrafanaHeaders)
if err != nil {
return settings, fmt.Errorf("could not parse forwardGrafanaHeaders value: %w", err)
return settings, errorsource.DownstreamError(fmt.Errorf("could not parse forwardGrafanaHeaders value: %w", err), false)
}
} else {
settings.ForwardGrafanaHeaders = jsonData["forwardGrafanaHeaders"].(bool)
Expand Down
3 changes: 2 additions & 1 deletion src/components/configEditor/LogsConfig.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { render, fireEvent } from '@testing-library/react';
import { LogsConfig } from './LogsConfig';
import allLabels from 'labels';
import { columnLabelToPlaceholder } from 'data/utils';
import { defaultLogsTable } from 'otel';

describe('LogsConfig', () => {
it('should render', () => {
Expand Down Expand Up @@ -61,7 +62,7 @@ describe('LogsConfig', () => {
);
expect(result.container.firstChild).not.toBeNull();

const input = result.getByPlaceholderText(allLabels.components.Config.LogsConfig.defaultTable.placeholder);
const input = result.getByPlaceholderText(defaultLogsTable);
expect(input).toBeInTheDocument();
fireEvent.change(input, { target: { value: 'changed' } });
fireEvent.blur(input);
Expand Down
4 changes: 2 additions & 2 deletions src/components/configEditor/LogsConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ConfigSection, ConfigSubSection } from 'components/experimental/ConfigS
import { Input, Field } from '@grafana/ui';
import { OtelVersionSelect } from 'components/queryBuilder/OtelVersionSelect';
import { ColumnHint } from 'types/queryBuilder';
import otel from 'otel';
import otel, { defaultLogsTable } from 'otel';
import { LabeledInput } from './LabeledInput';
import { CHLogsConfig } from 'types/config';
import allLabels from 'labels';
Expand Down Expand Up @@ -71,7 +71,7 @@ export const LogsConfig = (props: LogsConfigProps) => {
onChange={e => onDefaultTableChange(e.currentTarget.value)}
label={labels.defaultTable.label}
aria-label={labels.defaultTable.label}
placeholder={labels.defaultTable.placeholder}
placeholder={defaultLogsTable}
/>
</Field>
<ConfigSubSection
Expand Down
3 changes: 2 additions & 1 deletion src/components/configEditor/TracesConfig.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { render, fireEvent } from '@testing-library/react';
import { TracesConfig } from './TracesConfig';
import allLabels from 'labels';
import { columnLabelToPlaceholder } from 'data/utils';
import { defaultTraceTable } from 'otel';

describe('TracesConfig', () => {
it('should render', () => {
Expand Down Expand Up @@ -82,7 +83,7 @@ describe('TracesConfig', () => {
);
expect(result.container.firstChild).not.toBeNull();

const input = result.getByPlaceholderText(allLabels.components.Config.TracesConfig.defaultTable.placeholder);
const input = result.getByPlaceholderText(defaultTraceTable);
expect(input).toBeInTheDocument();
fireEvent.change(input, { target: { value: 'changed' } });
fireEvent.blur(input);
Expand Down
4 changes: 2 additions & 2 deletions src/components/configEditor/TracesConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ConfigSection, ConfigSubSection } from 'components/experimental/ConfigS
import { Input, Field } from '@grafana/ui';
import { OtelVersionSelect } from 'components/queryBuilder/OtelVersionSelect';
import { ColumnHint, TimeUnit } from 'types/queryBuilder';
import otel from 'otel';
import otel, { defaultTraceTable } from 'otel';
import { LabeledInput } from './LabeledInput';
import { DurationUnitSelect } from 'components/queryBuilder/DurationUnitSelect';
import { CHTracesConfig } from 'types/config';
Expand Down Expand Up @@ -90,7 +90,7 @@ export const TracesConfig = (props: TraceConfigProps) => {
onChange={e => onDefaultTableChange(e.currentTarget.value)}
label={labels.defaultTable.label}
aria-label={labels.defaultTable.label}
placeholder={labels.defaultTable.placeholder}
placeholder={defaultTraceTable}
/>
</Field>
<ConfigSubSection
Expand Down
6 changes: 2 additions & 4 deletions src/labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ export default {
defaultTable: {
label: 'Default trace table',
description: 'the default table used by the trace query builder',
name: 'defaultTable',
placeholder: 'otel_traces'
name: 'defaultTable'
},
columns: {
title: 'Default columns',
Expand Down Expand Up @@ -201,8 +200,7 @@ export default {
defaultTable: {
label: 'Default log table',
description: 'the default table used by the logs query builder',
name: 'defaultTable',
placeholder: 'otel_logs'
name: 'defaultTable'
},
columns: {
title: 'Default columns',
Expand Down
4 changes: 2 additions & 2 deletions src/otel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ColumnHint, TimeUnit } from "types/queryBuilder";

const defaultLogsTable = 'otel_logs';
const defaultTraceTable = 'otel_traces';
export const defaultLogsTable = 'otel_logs';
export const defaultTraceTable = 'otel_traces';

export const traceTimestampTableSuffix = '_trace_id_ts';

Expand Down
25 changes: 22 additions & 3 deletions src/views/CHConfigEditorHooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { renderHook } from "@testing-library/react";
import { CHConfig, CHHttpHeader, CHSecureConfig, Protocol } from "types/config";
import { onHttpHeadersChange, useConfigDefaults } from "./CHConfigEditorHooks";
import { pluginVersion } from "utils/version";
import { defaultLogsTable, defaultTraceTable } from "otel";

describe('onHttpHeadersChange', () => {
it('should properly sort headers into secure/plain config fields', async () => {
Expand Down Expand Up @@ -90,9 +91,15 @@ describe('onHttpHeadersChange', () => {
});

describe('useConfigDefaults', () => {
const expectedDefaults = {
const expectedDefaults: Partial<CHConfig> = {
version: pluginVersion,
protocol: Protocol.Native,
logs: {
defaultTable: defaultLogsTable
},
traces: {
defaultTable: defaultTraceTable
}
};

it('should rename v3 fields to latest config names', async () => {
Expand Down Expand Up @@ -210,7 +217,13 @@ describe('useConfigDefaults', () => {
jsonData: {
host: 'existing',
dialTimeout: 20,
protocol: Protocol.Http
protocol: Protocol.Http,
logs: {
defaultTable: 'not_default_logs'
},
traces: {
defaultTable: '' // empty
}
}
} as any as DataSourceSettings<CHConfig>;

Expand All @@ -221,7 +234,13 @@ describe('useConfigDefaults', () => {
...expectedDefaults,
host: 'existing',
dialTimeout: 20,
protocol: Protocol.Http
protocol: Protocol.Http,
logs: {
defaultTable: 'not_default_logs'
},
traces: {
defaultTable: ''
}
}
};
expect(onOptionsChange).toHaveBeenCalledTimes(1);
Expand Down
15 changes: 15 additions & 0 deletions src/views/CHConfigEditorHooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DataSourceSettings, KeyValue } from "@grafana/data";
import { defaultLogsTable, defaultTraceTable } from "otel";
import { useEffect, useRef } from "react";
import { CHConfig, CHHttpHeader, CHSecureConfig, Protocol } from "types/config";
import { pluginVersion } from "utils/version";
Expand Down Expand Up @@ -92,6 +93,20 @@ export const useConfigDefaults = (options: DataSourceSettings<CHConfig>, onOptio
jsonData.protocol = Protocol.Native;
}

if (!jsonData.logs || jsonData.logs.defaultTable === undefined) {
jsonData.logs = {
...jsonData.logs,
defaultTable: defaultLogsTable
};
}

if (!jsonData.traces || jsonData.traces.defaultTable === undefined) {
jsonData.traces = {
...jsonData.traces,
defaultTable: defaultTraceTable
};
}

onOptionsChange({
...options,
jsonData,
Expand Down

0 comments on commit 7d40624

Please sign in to comment.