Skip to content

Commit abc0340

Browse files
committed
fix(tracing): upgrade to otel 2.0
1 parent ad8f46e commit abc0340

File tree

15 files changed

+1610
-232
lines changed

15 files changed

+1610
-232
lines changed

genkit-tools/common/src/types/trace.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,16 @@ export const LinkSchema = z.object({
9797
});
9898

9999
/**
100-
* Zod schema for instrumentation library.
100+
* Zod schema for instrumentation scope.
101+
*/
102+
export const InstrumentationScopeSchema = z.object({
103+
name: z.string().readonly(),
104+
version: z.string().optional().readonly(),
105+
schemaUrl: z.string().optional().readonly(),
106+
});
107+
108+
/**
109+
* Zod schema for legacy instrumentation library.
101110
*/
102111
export const InstrumentationLibrarySchema = z.object({
103112
name: z.string().readonly(),
@@ -118,7 +127,8 @@ export const SpanDataSchema = z
118127
attributes: z.record(z.string(), z.unknown()),
119128
displayName: z.string(),
120129
links: z.array(LinkSchema).optional(),
121-
instrumentationLibrary: InstrumentationLibrarySchema,
130+
instrumentationLibrary: InstrumentationLibrarySchema.optional(),
131+
instrumentationScope: InstrumentationScopeSchema.optional(),
122132
spanKind: z.string(),
123133
sameProcessAsParentSpan: z.object({ value: z.boolean() }).optional(),
124134
status: SpanStatusSchema.optional(),

genkit-tools/genkit-schema.json

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,24 @@
14041404
],
14051405
"additionalProperties": false
14061406
},
1407+
"InstrumentationScope": {
1408+
"type": "object",
1409+
"properties": {
1410+
"name": {
1411+
"type": "string"
1412+
},
1413+
"version": {
1414+
"type": "string"
1415+
},
1416+
"schemaUrl": {
1417+
"type": "string"
1418+
}
1419+
},
1420+
"required": [
1421+
"name"
1422+
],
1423+
"additionalProperties": false
1424+
},
14071425
"Link": {
14081426
"type": "object",
14091427
"properties": {
@@ -1500,6 +1518,9 @@
15001518
"instrumentationLibrary": {
15011519
"$ref": "#/$defs/InstrumentationLibrary"
15021520
},
1521+
"instrumentationScope": {
1522+
"$ref": "#/$defs/InstrumentationScope"
1523+
},
15031524
"spanKind": {
15041525
"type": "string"
15051526
},
@@ -1541,7 +1562,6 @@
15411562
"endTime",
15421563
"attributes",
15431564
"displayName",
1544-
"instrumentationLibrary",
15451565
"spanKind"
15461566
],
15471567
"additionalProperties": false

genkit-tools/telemetry-server/src/utils/otlp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function toSpanData(span: OtlpSpan, scope: OtlpScopeSpan['scope']): SpanData {
120120
endTime: toMillis(span.endTimeUnixNano),
121121
displayName: span.name,
122122
attributes,
123-
instrumentationLibrary: {
123+
instrumentationScope: {
124124
name: scope.name,
125125
version: scope.version,
126126
},

go/ai/gen.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ type GenerationUsage struct {
184184
TotalTokens int `json:"totalTokens,omitempty"`
185185
}
186186

187+
type InstrumentationScope struct {
188+
Name string `json:"name,omitempty"`
189+
SchemaUrl string `json:"schemaUrl,omitempty"`
190+
Version string `json:"version,omitempty"`
191+
}
192+
187193
type Media struct {
188194
ContentType string `json:"contentType,omitempty"`
189195
Url string `json:"url,omitempty"`

js/core/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
"license": "Apache-2.0",
2828
"dependencies": {
2929
"@opentelemetry/api": "^1.9.0",
30-
"@opentelemetry/context-async-hooks": "~1.25.0",
31-
"@opentelemetry/core": "~1.25.0",
32-
"@opentelemetry/sdk-metrics": "~1.25.0",
33-
"@opentelemetry/sdk-node": "^0.52.0",
34-
"@opentelemetry/sdk-trace-base": "~1.25.0",
35-
"@opentelemetry/exporter-jaeger": "^1.25.0",
30+
"@opentelemetry/context-async-hooks": "^2.2.0",
31+
"@opentelemetry/core": "^2.2.0",
32+
"@opentelemetry/sdk-metrics": "^2.2.0",
33+
"@opentelemetry/sdk-node": "^0.208.0",
34+
"@opentelemetry/sdk-trace-base": "^2.2.0",
35+
"@opentelemetry/exporter-jaeger": "^2.2.0",
3636
"@types/json-schema": "^7.0.15",
3737
"ajv": "^8.12.0",
3838
"ajv-formats": "^3.0.1",

js/core/src/tracing/exporter.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class TraceServerExporter implements SpanExporter {
7272
displayName: span.name,
7373
links: span.links,
7474
spanKind: SpanKind[span.kind],
75-
parentSpanId: span.parentSpanId,
75+
parentSpanId: span.parentSpanContext?.spanId,
7676
sameProcessAsParentSpan: { value: !span.spanContext().isRemote },
7777
status: span.status,
7878
timeEvents: {
@@ -85,17 +85,17 @@ export class TraceServerExporter implements SpanExporter {
8585
})),
8686
},
8787
};
88-
if (span.instrumentationLibrary !== undefined) {
89-
spanData.instrumentationLibrary = {
90-
name: span.instrumentationLibrary.name,
88+
if (span.instrumentationScope !== undefined) {
89+
spanData.instrumentationScope = {
90+
name: span.instrumentationScope.name,
9191
};
92-
if (span.instrumentationLibrary.schemaUrl !== undefined) {
93-
spanData.instrumentationLibrary.schemaUrl =
94-
span.instrumentationLibrary.schemaUrl;
92+
if (span.instrumentationScope.schemaUrl !== undefined) {
93+
spanData.instrumentationScope.schemaUrl =
94+
span.instrumentationScope.schemaUrl;
9595
}
96-
if (span.instrumentationLibrary.version !== undefined) {
97-
spanData.instrumentationLibrary.version =
98-
span.instrumentationLibrary.version;
96+
if (span.instrumentationScope.version !== undefined) {
97+
spanData.instrumentationScope.version =
98+
span.instrumentationScope.version;
9999
}
100100
}
101101
deleteUndefinedProps(spanData);

js/core/src/tracing/types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ export const InstrumentationLibrarySchema = z.object({
8080
schemaUrl: z.string().optional().readonly(),
8181
});
8282

83+
export const InstrumentationScopeSchema = z.object({
84+
name: z.string().readonly(),
85+
version: z.string().optional().readonly(),
86+
schemaUrl: z.string().optional().readonly(),
87+
});
88+
8389
export const SpanDataSchema = z.object({
8490
spanId: z.string(),
8591
traceId: z.string(),
@@ -89,7 +95,8 @@ export const SpanDataSchema = z.object({
8995
attributes: z.record(z.string(), z.any()),
9096
displayName: z.string(),
9197
links: z.array(LinkSchema).optional(),
92-
instrumentationLibrary: InstrumentationLibrarySchema,
98+
instrumentationLibrary: InstrumentationLibrarySchema.optional(),
99+
instrumentationScope: InstrumentationScopeSchema.optional(),
93100
spanKind: z.string(),
94101
sameProcessAsParentSpan: z.object({ value: z.boolean() }).optional(),
95102
status: SpanStatusSchema.optional(),

js/core/tests/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class TestSpanExporter implements SpanExporter {
4141
displayName: span.name,
4242
links: span.links,
4343
spanKind: SpanKind[span.kind],
44-
parentSpanId: span.parentSpanId,
44+
parentSpanId: span.parentSpanContext?.spanId,
4545
sameProcessAsParentSpan: { value: !span.spanContext().isRemote },
4646
status: span.status,
4747
timeEvents: {

js/plugins/google-cloud/package.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@
3232
"license": "Apache-2.0",
3333
"dependencies": {
3434
"@google-cloud/logging-winston": "^6.0.0",
35-
"@google-cloud/opentelemetry-cloud-monitoring-exporter": "^0.19.0",
36-
"@google-cloud/opentelemetry-cloud-trace-exporter": "^2.4.1",
37-
"@google-cloud/opentelemetry-resource-util": "^2.4.0",
35+
"@google-cloud/opentelemetry-cloud-monitoring-exporter": "^0.21.0",
36+
"@google-cloud/opentelemetry-cloud-trace-exporter": "^3.0.0",
37+
"@google-cloud/opentelemetry-resource-util": "^3.0.0",
3838
"@opentelemetry/api": "^1.9.0",
39-
"@opentelemetry/auto-instrumentations-node": "^0.49.1",
40-
"@opentelemetry/core": "~1.25.0",
41-
"@opentelemetry/instrumentation": "^0.52.0",
42-
"@opentelemetry/instrumentation-pino": "^0.41.0",
43-
"@opentelemetry/instrumentation-winston": "^0.39.0",
44-
"@opentelemetry/resources": "~1.25.0",
45-
"@opentelemetry/sdk-metrics": "~1.25.0",
46-
"@opentelemetry/sdk-node": "^0.52.0",
47-
"@opentelemetry/sdk-trace-base": "~1.25.0",
39+
"@opentelemetry/auto-instrumentations-node": "^0.67.1",
40+
"@opentelemetry/core": "^2.2.0",
41+
"@opentelemetry/instrumentation": "^0.208.0",
42+
"@opentelemetry/instrumentation-pino": "^0.55.0",
43+
"@opentelemetry/instrumentation-winston": "^0.53.0",
44+
"@opentelemetry/resources": "^2.2.0",
45+
"@opentelemetry/sdk-metrics": "^2.2.0",
46+
"@opentelemetry/sdk-node": "^0.208.0",
47+
"@opentelemetry/sdk-trace-base": "^2.2.0",
4848
"google-auth-library": "^9.6.3",
4949
"node-fetch": "^3.3.2",
5050
"winston": "^3.12.0"

js/plugins/google-cloud/src/gcpOpenTelemetry.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ import { type ExportResult } from '@opentelemetry/core';
2626
import type { Instrumentation } from '@opentelemetry/instrumentation';
2727
import { PinoInstrumentation } from '@opentelemetry/instrumentation-pino';
2828
import { WinstonInstrumentation } from '@opentelemetry/instrumentation-winston';
29-
import { Resource } from '@opentelemetry/resources';
29+
import { Resource, resourceFromAttributes } from '@opentelemetry/resources';
3030
import {
31+
AggregationOption,
3132
AggregationTemporality,
32-
DefaultAggregation,
33-
ExponentialHistogramAggregation,
33+
AggregationType,
3434
InMemoryMetricExporter,
3535
InstrumentType,
3636
PeriodicExportingMetricReader,
@@ -73,9 +73,12 @@ export class GcpOpenTelemetry {
7373

7474
constructor(config: GcpTelemetryConfig) {
7575
this.config = config;
76-
this.resource = new Resource({ type: 'global' }).merge(
77-
new GcpDetectorSync().detect()
78-
);
76+
77+
const detectedResource = new GcpDetectorSync().detect();
78+
this.resource = resourceFromAttributes({
79+
type: 'global',
80+
...detectedResource.attributes,
81+
});
7982
}
8083

8184
/**
@@ -237,11 +240,11 @@ class MetricExporterWrapper extends MetricExporter {
237240
});
238241
}
239242

240-
selectAggregation(instrumentType: InstrumentType) {
243+
selectAggregation(instrumentType: InstrumentType): AggregationOption {
241244
if (instrumentType === InstrumentType.HISTOGRAM) {
242-
return new ExponentialHistogramAggregation();
245+
return { type: AggregationType.EXPONENTIAL_HISTOGRAM };
243246
}
244-
return new DefaultAggregation();
247+
return { type: AggregationType.DEFAULT };
245248
}
246249

247250
selectAggregationTemporality(instrumentType: InstrumentType) {

0 commit comments

Comments
 (0)