From 377430e741767e86162c4f14aabffa5ebf051f71 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Sun, 26 May 2024 08:18:57 -0400 Subject: [PATCH] [Go] move Milliseconds into tracing package You could argue that the Milliseconds type is closely related to the tracing output format. So move the type into core/tracing and remove the gtime package. --- go/core/flow.go | 7 +++---- go/core/gen.go | 8 ++++---- go/core/schemas.config | 6 +++--- go/{gtime/gtime.go => core/tracing/milliseconds.go} | 2 +- .../tracing/milliseconds_test.go} | 2 +- go/core/tracing/store.go | 11 +++++------ go/core/tracing/trace_store_exporter.go | 7 +++---- go/core/tracing/trace_store_exporter_test.go | 7 +++---- 8 files changed, 23 insertions(+), 27 deletions(-) rename go/{gtime/gtime.go => core/tracing/milliseconds.go} (98%) rename go/{gtime/gtime_test.go => core/tracing/milliseconds_test.go} (98%) diff --git a/go/core/flow.go b/go/core/flow.go index 7ecb7b3b0c..311af7a8f3 100644 --- a/go/core/flow.go +++ b/go/core/flow.go @@ -26,7 +26,6 @@ import ( "github.com/firebase/genkit/go/core/logger" "github.com/firebase/genkit/go/core/tracing" - "github.com/firebase/genkit/go/gtime" "github.com/firebase/genkit/go/internal" "github.com/google/uuid" otrace "go.opentelemetry.io/otel/trace" @@ -172,7 +171,7 @@ type flowState[I, O any] struct { FlowID string `json:"flowId,omitempty"` FlowName string `json:"name,omitempty"` // start time in milliseconds since the epoch - StartTime gtime.Milliseconds `json:"startTime,omitempty"` + StartTime tracing.Milliseconds `json:"startTime,omitempty"` Input I `json:"input,omitempty"` mu sync.Mutex @@ -189,7 +188,7 @@ func newFlowState[I, O any](id, name string, input I) *flowState[I, O] { FlowID: id, FlowName: name, Input: input, - StartTime: gtime.ToMilliseconds(time.Now()), + StartTime: tracing.ToMilliseconds(time.Now()), Cache: map[string]json.RawMessage{}, Operation: &operation[O]{ FlowID: id, @@ -352,7 +351,7 @@ func (f *Flow[I, O, S]) execute(ctx context.Context, state *flowState[I, O], dis }() ctx = flowContextKey.NewContext(ctx, fctx) exec := &flowExecution{ - StartTime: gtime.ToMilliseconds(time.Now()), + StartTime: tracing.ToMilliseconds(time.Now()), } state.mu.Lock() state.Executions = append(state.Executions, exec) diff --git a/go/core/gen.go b/go/core/gen.go index 0ef7fd96db..2e98644998 100644 --- a/go/core/gen.go +++ b/go/core/gen.go @@ -16,7 +16,7 @@ package core -import "github.com/firebase/genkit/go/gtime" +import "github.com/firebase/genkit/go/core/tracing" type flowError struct { Error string `json:"error,omitempty"` @@ -25,8 +25,8 @@ type flowError struct { type flowExecution struct { // end time in milliseconds since the epoch - EndTime gtime.Milliseconds `json:"endTime,omitempty"` + EndTime tracing.Milliseconds `json:"endTime,omitempty"` // start time in milliseconds since the epoch - StartTime gtime.Milliseconds `json:"startTime,omitempty"` - TraceIDs []string `json:"traceIds,omitempty"` + StartTime tracing.Milliseconds `json:"startTime,omitempty"` + TraceIDs []string `json:"traceIds,omitempty"` } diff --git a/go/core/schemas.config b/go/core/schemas.config index 76aa30e370..8004f5de8f 100644 --- a/go/core/schemas.config +++ b/go/core/schemas.config @@ -1,7 +1,7 @@ # This file holds configuration for the genkit-schema.json file # generated by the npm export:schemas script. -core import github.com/firebase/genkit/go/gtime +core import github.com/firebase/genkit/go/core/tracing # DocumentData type was hand-written. DocumentData omit @@ -67,8 +67,8 @@ FlowInvokeEnvelopeMessageRunScheduled omit Operation omit FlowStateExecution name flowExecution -FlowStateExecution.startTime type gtime.Milliseconds -FlowStateExecution.endTime type gtime.Milliseconds +FlowStateExecution.startTime type tracing.Milliseconds +FlowStateExecution.endTime type tracing.Milliseconds FlowError name flowError diff --git a/go/gtime/gtime.go b/go/core/tracing/milliseconds.go similarity index 98% rename from go/gtime/gtime.go rename to go/core/tracing/milliseconds.go index 3b8c63cc91..79315cf134 100644 --- a/go/gtime/gtime.go +++ b/go/core/tracing/milliseconds.go @@ -13,7 +13,7 @@ // limitations under the License. // Package gtime provides time functionality for Go Genkit. -package gtime +package tracing import "time" diff --git a/go/gtime/gtime_test.go b/go/core/tracing/milliseconds_test.go similarity index 98% rename from go/gtime/gtime_test.go rename to go/core/tracing/milliseconds_test.go index 94081bac43..49694ef3fa 100644 --- a/go/gtime/gtime_test.go +++ b/go/core/tracing/milliseconds_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package gtime +package tracing import ( "testing" diff --git a/go/core/tracing/store.go b/go/core/tracing/store.go index 0d57257925..42c75a1edb 100644 --- a/go/core/tracing/store.go +++ b/go/core/tracing/store.go @@ -18,7 +18,6 @@ import ( "context" "errors" - "github.com/firebase/genkit/go/gtime" ) // A Store stores trace information. @@ -61,8 +60,8 @@ type Query struct { type Data struct { TraceID string `json:"traceId"` DisplayName string `json:"displayName"` - StartTime gtime.Milliseconds `json:"startTime"` - EndTime gtime.Milliseconds `json:"endTime"` + StartTime Milliseconds `json:"startTime"` + EndTime Milliseconds `json:"endTime"` Spans map[string]*SpanData `json:"spans"` } @@ -75,8 +74,8 @@ type SpanData struct { SpanID string `json:"spanId"` TraceID string `json:"traceId,omitempty"` ParentSpanID string `json:"parentSpanId,omitempty"` - StartTime gtime.Milliseconds `json:"startTime"` - EndTime gtime.Milliseconds `json:"endTime"` + StartTime Milliseconds `json:"startTime"` + EndTime Milliseconds `json:"endTime"` Attributes map[string]any `json:"attributes,omitempty"` DisplayName string `json:"displayName"` Links []*Link `json:"links,omitempty"` @@ -97,7 +96,7 @@ type BoolValue struct { } type TimeEvent struct { - Time gtime.Milliseconds `json:"time,omitempty"` + Time Milliseconds `json:"time,omitempty"` Annotation Annotation `json:"annotation,omitempty"` } diff --git a/go/core/tracing/trace_store_exporter.go b/go/core/tracing/trace_store_exporter.go index 991a6b4a3f..93c4e93a40 100644 --- a/go/core/tracing/trace_store_exporter.go +++ b/go/core/tracing/trace_store_exporter.go @@ -19,7 +19,6 @@ import ( "errors" "strings" - "github.com/firebase/genkit/go/gtime" "go.opentelemetry.io/otel/attribute" sdktrace "go.opentelemetry.io/otel/sdk/trace" otrace "go.opentelemetry.io/otel/trace" @@ -89,8 +88,8 @@ func convertSpan(span sdktrace.ReadOnlySpan) *SpanData { sd := &SpanData{ SpanID: sc.SpanID().String(), TraceID: sc.TraceID().String(), - StartTime: gtime.ToMilliseconds(span.StartTime()), - EndTime: gtime.ToMilliseconds(span.EndTime()), + StartTime: ToMilliseconds(span.StartTime()), + EndTime: ToMilliseconds(span.EndTime()), Attributes: attributesToMap(span.Attributes()), DisplayName: span.Name(), Links: convertLinks(span.Links()), @@ -140,7 +139,7 @@ func convertEvents(evs []sdktrace.Event) []TimeEvent { var tes []TimeEvent for _, e := range evs { tes = append(tes, TimeEvent{ - Time: gtime.ToMilliseconds(e.Time), + Time: ToMilliseconds(e.Time), Annotation: Annotation{ Description: e.Name, Attributes: attributesToMap(e.Attributes), diff --git a/go/core/tracing/trace_store_exporter_test.go b/go/core/tracing/trace_store_exporter_test.go index 29333212d7..487b00f929 100644 --- a/go/core/tracing/trace_store_exporter_test.go +++ b/go/core/tracing/trace_store_exporter_test.go @@ -18,7 +18,6 @@ import ( "testing" "time" - "github.com/firebase/genkit/go/gtime" "github.com/google/go-cmp/cmp" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" @@ -90,11 +89,11 @@ func TestConvertSpan(t *testing.T) { SpanID: spanID1, ParentSpanID: spanID2, SpanKind: "INTERNAL", - StartTime: gtime.Milliseconds(1e3), - EndTime: gtime.Milliseconds(2e3), + StartTime: Milliseconds(1e3), + EndTime: Milliseconds(2e3), Attributes: map[string]any{"k": "v"}, TimeEvents: TimeEvents{TimeEvent: []TimeEvent{{ - Time: gtime.Milliseconds(3e3), + Time: Milliseconds(3e3), Annotation: Annotation{ Attributes: map[string]any{"k2": "v2"}, Description: "ename",