Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add metadata interface to JS SDK #53

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions js/packages/observe-sdk-datadog/test/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ adapter.start(bytes).then((traceContext) => {
...traceContext.getImportObject(),
}).then((instance) => {
wasi.start(instance);
adapter.setMetadata({
http_status_code: 200,
http_url: "https://example.com",
});
traceContext.stop();
});
});
70 changes: 69 additions & 1 deletion js/src/lib/adapters/datadog/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ export enum DatadogSpanKind {
Internal = "internal",
}

export enum DatadogLanguage {
Cpp = "cpp",
Dotnet = "dotnet",
Go = "go",
Jvm = "jvm",
Javascript = "javascript",
Php = "php",
Ruby = "ruby",
Python = "python",
}

export interface DatadogConfig {
agentHost: URL;
serviceName: string;
Expand All @@ -42,10 +53,25 @@ export const DefaultDatadogConfig: DatadogConfig = {
traceBatchMax: 100,
};

export interface DatadogMetadata {
resource_name?: string,
http_status_code?: number,
http_url?: string,
http_method?: string,
http_client_ip?: string,
http_request_content_length?: number,
http_request_content_length_uncompressed?: number,
http_response_content_length?: number,
http_response_content_length_uncompressed?: number,
span_kind?: DatadogSpanKind,
language?: DatadogLanguage,
component?: string,
}

export class DatadogTraceContext implements Collector {
constructor(
private collector: SpanCollector,
) {}
) { }
getImportObject(): WebAssembly.Imports {
return this.collector.getImportObject();
}
Expand All @@ -70,6 +96,8 @@ export class DatadogAdapter implements Adapter {
formatter: DatadogFormatter;
config: DatadogConfig;
traceIntervalId: number | undefined;
meta: DatadogMetadata | undefined;

constructor(config?: DatadogConfig) {
this.config = DefaultDatadogConfig;
if (config) {
Expand Down Expand Up @@ -134,6 +162,10 @@ export class DatadogAdapter implements Adapter {
}
}

public setMetadata(meta: DatadogMetadata): void {
nilslice marked this conversation as resolved.
Show resolved Hide resolved
this.meta = meta;
}

private addSpanToTrace(
trace: Trace,
fn: FunctionCall,
Expand Down Expand Up @@ -170,6 +202,42 @@ export class DatadogAdapter implements Adapter {

private async send() {
if (this.formatter.traces.length > 0) {
for (var trace of this.formatter.traces) {
const span = trace.spans[0];
if (span && this.meta) {
if (this.meta.resource_name) {
span.meta["resource"] = this.meta.resource_name;
}
if (this.meta.http_status_code) {
span.meta["http.status_code"] = this.meta.http_status_code;
}
if (this.meta.http_url) {
span.meta["http.url"] = this.meta.http_url;
}
if (this.meta.http_method) {
span.meta["http.method"] = this.meta.http_method;
}
if (this.meta.http_client_ip) {
span.meta["http.client_ip"] = this.meta.http_client_ip;
}
if (this.meta.http_request_content_length) {
span.meta["http.request.content_length"] = this.meta.http_request_content_length;
}
if (this.meta.http_request_content_length_uncompressed) {
span.meta["http.request.content_length_uncompressed"] = this.meta.http_request_content_length_uncompressed;
}
if (this.meta.http_response_content_length) {
span.meta["http.response.content_length"] = this.meta.http_response_content_length;
}
if (this.meta.http_response_content_length_uncompressed) {
span.meta["http.response.content_length_uncompressed"] = this.meta.http_response_content_length_uncompressed;
}
if (this.meta.span_kind) {
span.meta["span.kind"] = this.meta.span_kind;
}
}
}

const controller = new AbortController();
const id = setTimeout(() => controller.abort(), 1000);
try {
Expand Down
Loading