Skip to content

Commit 5a7922a

Browse files
committed
feat: add ion.Attr type alias for OTel attributes
- Add attrs.go with Attr type alias for attribute.KeyValue - Update Span interface to use ion.Attr in public API - Document OTel integration philosophy in README This makes Ion's API appear self-contained while still using standard OTel types underneath. Users continue to import go.opentelemetry.io/otel/attribute for constructors.
1 parent a73977d commit 5a7922a

3 files changed

Lines changed: 72 additions & 16 deletions

File tree

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,22 @@ func ProcessOrder(ctx context.Context, orderID string) error {
326326
}
327327
```
328328

329+
> **📘 OTel Types in Ion**
330+
>
331+
> Ion uses OpenTelemetry for tracing and metrics. The `ion.Attr` type (visible in Ion's API signatures) is an alias for `attribute.KeyValue`.
332+
>
333+
> To create attributes, import the OTel package directly:
334+
> ```go
335+
> import "go.opentelemetry.io/otel/attribute"
336+
>
337+
> span.SetAttributes(
338+
> attribute.String("order.id", orderID),
339+
> attribute.Int64("retry.count", 3),
340+
> )
341+
> ```
342+
>
343+
> This is intentional: **Ion abstracts provider lifecycle, not the instrumentation API.** Users benefit from learning the standard OTel API.
344+
329345
---
330346
331347
## Common Configurations

attrs.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Package ion provides unified observability for JupiterMeta applications.
2+
//
3+
// This file defines type aliases for OpenTelemetry attribute types used in
4+
// tracing and metrics. These aliases allow Ion's API to appear self-contained
5+
// in documentation while still using the standard OTel types underneath.
6+
package ion
7+
8+
import "go.opentelemetry.io/otel/attribute"
9+
10+
// ─────────────────────────────────────────────────────────────────────────────
11+
// Attribute Types (for Tracing and Metrics)
12+
// ─────────────────────────────────────────────────────────────────────────────
13+
//
14+
// Attr is a key-value pair used for trace span attributes and metric dimensions.
15+
// This is an alias for the OpenTelemetry attribute.KeyValue type.
16+
//
17+
// Create attributes using the standard OTel constructors:
18+
//
19+
// import "go.opentelemetry.io/otel/attribute"
20+
//
21+
// span.SetAttributes(
22+
// attribute.String("order.id", orderID),
23+
// attribute.Int64("retry.count", 3),
24+
// )
25+
//
26+
// Or for metrics:
27+
//
28+
// counter.Add(ctx, 1, metric.WithAttributes(
29+
// attribute.String("shard_id", "3"),
30+
// ))
31+
//
32+
// Note: Ion intentionally does NOT wrap attribute constructors. This ensures
33+
// users learn the standard OpenTelemetry API, which is an industry skill.
34+
type Attr = attribute.KeyValue
35+
36+
// AttrKey is a type alias for attribute keys.
37+
// Use attribute.Key("mykey").String("value") for advanced patterns.
38+
type AttrKey = attribute.Key

tracer.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55

66
"go.opentelemetry.io/otel"
7-
"go.opentelemetry.io/otel/attribute"
87
"go.opentelemetry.io/otel/codes"
98
"go.opentelemetry.io/otel/trace"
109
)
@@ -32,9 +31,11 @@ type Span interface {
3231
// RecordError records an error as an event.
3332
RecordError(err error)
3433
// SetAttributes sets attributes on the span.
35-
SetAttributes(attrs ...attribute.KeyValue)
34+
// Use attribute.String(), attribute.Int64(), etc. to create Attr values.
35+
SetAttributes(attrs ...Attr)
3636
// AddEvent adds an event to the span.
37-
AddEvent(name string, attrs ...attribute.KeyValue)
37+
// Use attribute.String(), attribute.Int64(), etc. to create Attr values.
38+
AddEvent(name string, attrs ...Attr)
3839
}
3940

4041
// SpanOption configures span creation.
@@ -44,7 +45,7 @@ type SpanOption interface {
4445

4546
type spanOptions struct {
4647
kind trace.SpanKind
47-
attributes []attribute.KeyValue
48+
attributes []Attr
4849
links []trace.Link
4950
otelOpts []trace.SpanStartOption
5051
}
@@ -56,12 +57,13 @@ func (k kindOption) apply(o *spanOptions) { o.kind = trace.SpanKind(k) }
5657
// WithSpanKind sets the span kind (client, server, etc).
5758
func WithSpanKind(kind trace.SpanKind) SpanOption { return kindOption(kind) }
5859

59-
type attrOption []attribute.KeyValue
60+
type attrOption []Attr
6061

6162
func (a attrOption) apply(o *spanOptions) { o.attributes = append(o.attributes, a...) }
6263

6364
// WithAttributes adds attributes to the span.
64-
func WithAttributes(attrs ...attribute.KeyValue) SpanOption { return attrOption(attrs) }
65+
// Use attribute.String(), attribute.Int64(), etc. to create Attr values.
66+
func WithAttributes(attrs ...Attr) SpanOption { return attrOption(attrs) }
6567

6668
type linkOption []trace.Link
6769

@@ -113,11 +115,11 @@ type otelSpan struct {
113115
span trace.Span
114116
}
115117

116-
func (s *otelSpan) End() { s.span.End() }
117-
func (s *otelSpan) SetStatus(code codes.Code, desc string) { s.span.SetStatus(code, desc) }
118-
func (s *otelSpan) RecordError(err error) { s.span.RecordError(err) }
119-
func (s *otelSpan) SetAttributes(attrs ...attribute.KeyValue) { s.span.SetAttributes(attrs...) }
120-
func (s *otelSpan) AddEvent(name string, attrs ...attribute.KeyValue) {
118+
func (s *otelSpan) End() { s.span.End() }
119+
func (s *otelSpan) SetStatus(code codes.Code, desc string) { s.span.SetStatus(code, desc) }
120+
func (s *otelSpan) RecordError(err error) { s.span.RecordError(err) }
121+
func (s *otelSpan) SetAttributes(attrs ...Attr) { s.span.SetAttributes(attrs...) }
122+
func (s *otelSpan) AddEvent(name string, attrs ...Attr) {
121123
s.span.AddEvent(name, trace.WithAttributes(attrs...))
122124
}
123125

@@ -131,8 +133,8 @@ func (noopTracer) Start(ctx context.Context, _ string, _ ...SpanOption) (context
131133

132134
type noopSpan struct{}
133135

134-
func (noopSpan) End() {}
135-
func (noopSpan) SetStatus(codes.Code, string) {}
136-
func (noopSpan) RecordError(error) {}
137-
func (noopSpan) SetAttributes(...attribute.KeyValue) {}
138-
func (noopSpan) AddEvent(string, ...attribute.KeyValue) {}
136+
func (noopSpan) End() {}
137+
func (noopSpan) SetStatus(codes.Code, string) {}
138+
func (noopSpan) RecordError(error) {}
139+
func (noopSpan) SetAttributes(...Attr) {}
140+
func (noopSpan) AddEvent(string, ...Attr) {}

0 commit comments

Comments
 (0)