|
4 | 4 | */ |
5 | 5 | import type { Attributes, HrTime } from '@opentelemetry/api'; |
6 | 6 | import type { AnyValue, LogAttributes } from '@opentelemetry/api-logs'; |
| 7 | +import type { InstrumentationScope } from '@opentelemetry/core'; |
| 8 | +import type { Resource } from '@opentelemetry/resources'; |
7 | 9 | import type { IProtobufWriter } from './i-protobuf-writer'; |
8 | 10 |
|
9 | 11 | /** |
@@ -187,3 +189,60 @@ export function writeAnyValue(writer: IProtobufWriter, value: AnyValue): void { |
187 | 189 | } |
188 | 190 | // Else: unsupported type, write nothing |
189 | 191 | } |
| 192 | + |
| 193 | +/** |
| 194 | + * Write an InstrumentationScope message. |
| 195 | + * |
| 196 | + * Proto fields (InstrumentationScope): |
| 197 | + * 1 name string (wire type 2) |
| 198 | + * 2 version string (wire type 2) |
| 199 | + */ |
| 200 | +export function writeInstrumentationScope( |
| 201 | + writer: IProtobufWriter, |
| 202 | + scope: InstrumentationScope, |
| 203 | + fieldNumber: number |
| 204 | +): void { |
| 205 | + writer.writeTag(fieldNumber, 2); |
| 206 | + const start = writer.startLengthDelimited(); |
| 207 | + const startPos = writer.pos; |
| 208 | + |
| 209 | + // name (field 1, string) |
| 210 | + writer.writeTag(1, 2); |
| 211 | + writer.writeString(scope.name); |
| 212 | + |
| 213 | + // version (field 2, string) - skip if empty |
| 214 | + if (scope.version) { |
| 215 | + writer.writeTag(2, 2); |
| 216 | + writer.writeString(scope.version); |
| 217 | + } |
| 218 | + |
| 219 | + writer.finishLengthDelimited(start, writer.pos - startPos); |
| 220 | +} |
| 221 | + |
| 222 | +/** |
| 223 | + * Write a Resource message and its enclosing tag. |
| 224 | + * |
| 225 | + * Proto fields (Resource): |
| 226 | + * 1 attributes repeated KeyValue (wire type 2) |
| 227 | + * 2 dropped_attributes_count uint32 (wire type 0) |
| 228 | + */ |
| 229 | +export function writeResource( |
| 230 | + writer: IProtobufWriter, |
| 231 | + resource: Resource, |
| 232 | + fieldNumber: number |
| 233 | +): void { |
| 234 | + writer.writeTag(fieldNumber, 2); |
| 235 | + const resourceStart = writer.startLengthDelimited(); |
| 236 | + const resourceStartPos = writer.pos; |
| 237 | + |
| 238 | + // attributes (field 1, repeated KeyValue) |
| 239 | + if (resource.attributes) { |
| 240 | + writeAttributes(writer, resource.attributes, 1); |
| 241 | + } |
| 242 | + |
| 243 | + // dropped_attributes_count (field 2, uint32) - set to 0 as we don't track this |
| 244 | + writer.writeTag(2, 0); |
| 245 | + writer.writeVarint(0); |
| 246 | + |
| 247 | + writer.finishLengthDelimited(resourceStart, writer.pos - resourceStartPos); |
| 248 | +} |
0 commit comments