Skip to content

Commit

Permalink
Updating to use the new endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaHegde committed Nov 17, 2023
1 parent 8c92dbd commit 7455526
Show file tree
Hide file tree
Showing 13 changed files with 1,048 additions and 991 deletions.
10 changes: 0 additions & 10 deletions admin/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
runtimeauth "github.com/rilldata/rill/runtime/server/auth"
"github.com/rs/cors"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/attribute"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -300,15 +299,6 @@ func (s *Server) Ping(ctx context.Context, req *adminv1.PingRequest) (*adminv1.P
return resp, nil
}

func (s *Server) Telemetry(ctx context.Context, req *adminv1.TelemetryRequest) (*adminv1.TelemetryResponse, error) {
dims := make([]attribute.KeyValue, 0)
for k, v := range req.Event {
dims = append(dims, attribute.String(k, v))
}
s.uiActivity.Emit(ctx, "cloud-ui-telemetry", 1, dims...)
return &adminv1.TelemetryResponse{}, nil
}

func timeoutSelector(fullMethodName string) time.Duration {
return time.Minute
}
Expand Down
37 changes: 37 additions & 0 deletions admin/server/telemetry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package server

import (
"context"

adminv1 "github.com/rilldata/rill/proto/gen/rill/admin/v1"
"go.opentelemetry.io/otel/attribute"
)

func (s *Server) Telemetry(ctx context.Context, req *adminv1.TelemetryRequest) (*adminv1.TelemetryResponse, error) {
dims := make([]attribute.KeyValue, 0)
for k, v := range req.Event.AsMap() {
a, ok := toKeyValue(k, v)
if ok {
dims = append(dims, a)
}
}
s.uiActivity.Emit(ctx, "cloud-ui-telemetry", 1, dims...)
return &adminv1.TelemetryResponse{}, nil
}

func toKeyValue(k string, v interface{}) (attribute.KeyValue, bool) {
switch vt := v.(type) {
case string:
return attribute.String(k, vt), true
case bool:
return attribute.Bool(k, vt), true
case int:
return attribute.Int(k, vt), true
case int64:
return attribute.Int64(k, vt), true
case float64:
return attribute.Float64(k, vt), true
}

return attribute.KeyValue{}, false
}
2 changes: 0 additions & 2 deletions proto/gen/rill/admin/v1/admin.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2484,8 +2484,6 @@ definitions:
properties:
event:
type: object
additionalProperties:
type: string
v1TelemetryResponse:
type: object
v1TriggerReconcileResponse:
Expand Down
1,874 changes: 934 additions & 940 deletions proto/gen/rill/admin/v1/api.pb.go

Large diffs are not rendered by default.

29 changes: 28 additions & 1 deletion proto/gen/rill/admin/v1/api.pb.validate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion proto/rill/admin/v1/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ message GenerateReportYAMLResponse {
}

message TelemetryRequest {
map<string, string> event = 1;
google.protobuf.Struct event = 1;
}

message TelemetryResponse {}
Expand Down
4 changes: 0 additions & 4 deletions runtime/server/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,6 @@ func (s *Server) GetResource(ctx context.Context, req *runtimev1.GetResourceRequ
attribute.String("args.name.name", req.Name.Name),
)

if req.Name.Name == "AdData_dashboard" {
return nil, errors.New("error")
}

if !auth.GetClaims(ctx).CanInstance(req.InstanceId, auth.ReadObjects) {
return nil, ErrForbidden
}
Expand Down
2 changes: 1 addition & 1 deletion web-admin/src/client/gen/index.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export interface V1TelemetryResponse {
[key: string]: any;
}

export type V1TelemetryRequestEvent = { [key: string]: string };
export type V1TelemetryRequestEvent = { [key: string]: any };

export interface V1TelemetryRequest {
event?: V1TelemetryRequestEvent;
Expand Down
28 changes: 11 additions & 17 deletions web-common/src/metrics/initMetrics.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ADMIN_URL } from "@rilldata/web-admin/client/http-client";
import { BehaviourEventHandler } from "@rilldata/web-common/metrics/BehaviourEventHandler";
import { ErrorEventHandler } from "@rilldata/web-common/metrics/ErrorEventHandler";
import { BehaviourEventFactory } from "@rilldata/web-common/metrics/service/BehaviourEventFactory";
import { MetricsService } from "@rilldata/web-common/metrics/service/MetricsService";
import { ProductHealthEventFactory } from "@rilldata/web-common/metrics/service/ProductHealthEventFactory";
import { RillAdminTelemetryClient } from "@rilldata/web-common/metrics/service/RillAdminTelemetryClient";
import { RillIntakeClient } from "@rilldata/web-common/metrics/service/RillIntakeClient";
import type { V1RuntimeGetConfig } from "@rilldata/web-common/runtime-client/manual-clients";
import { ActiveEventHandler } from "./ActiveEventHandler";
Expand All @@ -17,14 +17,11 @@ export let behaviourEvent: BehaviourEventHandler;
export let errorEvent: ErrorEventHandler;

export async function initMetrics(localConfig: V1RuntimeGetConfig) {
metricsService = new MetricsService(
new RillIntakeClient(`${RILL_RUNTIME_URL}/local/track`),
[
new ProductHealthEventFactory(),
new BehaviourEventFactory(),
new ErrorEventFactory(),
]
);
metricsService = new MetricsService(new RillIntakeClient(), [
new ProductHealthEventFactory(),
new BehaviourEventFactory(),
new ErrorEventFactory(),
]);
metricsService.loadLocalFields(localConfig);

const commonUserMetrics = await collectCommonUserFields();
Expand All @@ -34,14 +31,11 @@ export async function initMetrics(localConfig: V1RuntimeGetConfig) {
}

export async function initCloudMetrics() {
metricsService = new MetricsService(
new RillIntakeClient(`${ADMIN_URL}/v1/track`),
[
new ProductHealthEventFactory(),
new BehaviourEventFactory(),
new ErrorEventFactory(),
]
);
metricsService = new MetricsService(new RillAdminTelemetryClient(), [
new ProductHealthEventFactory(),
new BehaviourEventFactory(),
new ErrorEventFactory(),
]);

const commonUserMetrics = await collectCommonUserFields();
errorEvent = new ErrorEventHandler(metricsService, commonUserMetrics);
Expand Down
8 changes: 4 additions & 4 deletions web-common/src/metrics/service/MetricsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { MetricsEventFactory } from "./MetricsEventFactory";
import type { ErrorEventFactory } from "./ErrorEventFactory";
import type { CommonFields, MetricsEvent } from "./MetricsTypes";
import type { ProductHealthEventFactory } from "./ProductHealthEventFactory";
import type { RillIntakeClient } from "./RillIntakeClient";
import type { TelemetryClient } from "./RillIntakeClient";

export const ClientIDStorageKey = "client_id";

Expand Down Expand Up @@ -44,8 +44,8 @@ export class MetricsService
private commonFields: Record<string, unknown>;

public constructor(
private readonly rillIntakeClient: RillIntakeClient,
private readonly metricsEventFactories: Array<MetricsEventFactory>
private readonly telemetryClient: TelemetryClient,
metricsEventFactories: Array<MetricsEventFactory>
) {
metricsEventFactories.forEach((actions) => {
getActionMethods(actions).forEach((action) => {
Expand Down Expand Up @@ -98,7 +98,7 @@ export class MetricsService
{ ...this.commonFields },
...args
);
await this.rillIntakeClient.fireEvent(event);
await this.telemetryClient.fireEvent(event);
}

private getOrSetClientID(): string {
Expand Down
19 changes: 19 additions & 0 deletions web-common/src/metrics/service/RillAdminTelemetryClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ADMIN_URL } from "@rilldata/web-admin/client/http-client";
import type { MetricsEvent } from "@rilldata/web-common/metrics/service/MetricsTypes";
import type { TelemetryClient } from "@rilldata/web-common/metrics/service/RillIntakeClient";

export class RillAdminTelemetryClient implements TelemetryClient {
public async fireEvent(event: MetricsEvent) {
try {
const resp = await fetch(`${ADMIN_URL}/v1/telemetry`, {
method: "POST",
body: JSON.stringify({ event }),
credentials: "include",
});
if (!resp.ok)
console.error(`Failed to send ${event.event_type}. ${resp.statusText}`);
} catch (err) {
console.error(`Failed to send ${event.event_type}. ${err.message}`);
}
}
}
18 changes: 10 additions & 8 deletions web-common/src/metrics/service/RillIntakeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@ const RillIntakeUser = "data-modeler";
const RillIntakePassword =
"lkh8T90ozWJP/KxWnQ81PexRzpdghPdzuB0ly2/86TeUU8q/bKiVug==";

export class RillIntakeClient {
export interface TelemetryClient {
fireEvent(event: MetricsEvent): Promise<void>;
}

export class RillIntakeClient implements TelemetryClient {
private readonly authHeader: string;

public constructor(private readonly host: string) {
public constructor() {
// this is the format rill-intake expects.
this.authHeader =
"Basic " + btoa(`${RillIntakeUser}:${RillIntakePassword}`);
}

public async fireEvent(event: MetricsEvent) {
if (!this.host) return;
try {
const resp = await fetch(this.host, {
const resp = await fetch(`${RILL_RUNTIME_URL}/local/track`, {
method: "POST",
body: JSON.stringify(event),
// headers: {
// Authorization: this.authHeader,
// },
credentials: "include",
headers: {
Authorization: this.authHeader,
},
});
if (!resp.ok)
console.error(`Failed to send ${event.event_type}. ${resp.statusText}`);
Expand Down
6 changes: 3 additions & 3 deletions web-common/src/proto/gen/rill/admin/v1/api_pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5739,9 +5739,9 @@ export class GenerateReportYAMLResponse extends Message<GenerateReportYAMLRespon
*/
export class TelemetryRequest extends Message<TelemetryRequest> {
/**
* @generated from field: map<string, string> event = 1;
* @generated from field: google.protobuf.Struct event = 1;
*/
event: { [key: string]: string } = {};
event?: Struct;

constructor(data?: PartialMessage<TelemetryRequest>) {
super();
Expand All @@ -5751,7 +5751,7 @@ export class TelemetryRequest extends Message<TelemetryRequest> {
static readonly runtime: typeof proto3 = proto3;
static readonly typeName = "rill.admin.v1.TelemetryRequest";
static readonly fields: FieldList = proto3.util.newFieldList(() => [
{ no: 1, name: "event", kind: "map", K: 9 /* ScalarType.STRING */, V: {kind: "scalar", T: 9 /* ScalarType.STRING */} },
{ no: 1, name: "event", kind: "message", T: Struct },
]);

static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): TelemetryRequest {
Expand Down

0 comments on commit 7455526

Please sign in to comment.