Skip to content

Commit

Permalink
improve: tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
patroza committed Nov 14, 2023
1 parent 212aada commit a29f57f
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-papayas-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect-app/infra": patch
---

improve tracing
2 changes: 1 addition & 1 deletion packages/infra/_src/api/writeDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ export function writeOpenapiDocs(rdescs: Record<string, Record<string, RouteDesc
]
}))
.flatMap((_) => writeTextFile("./openapi.json", JSON.stringify(_, undefined, 2)).orDie)
.flatMap(() => Effect.logInfo("OpenAPI spec written to './openapi.json'"))
.flatMap(() => Effect.logDebug("OpenAPI spec written to './openapi.json'"))
}
4 changes: 2 additions & 2 deletions packages/infra/_src/instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
*/
export function instr<R, E, A>(self: Effect<R, E, A>, name: string, properties?: Record<string, string>) {
return self
.zipLeft(Effect.logTrace(`instrumented`).annotateLogs("properties", (properties || {}).$$.pretty))
.withSpan(name)
.zipLeft(Effect.logTrace(`instrumented`))
.withSpan(name, { attributes: properties })
.withLogSpan(name)

// trackMetric(name, time, properties))
Expand Down
2 changes: 1 addition & 1 deletion packages/infra/_src/logger/logFmtLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const logfmtLogger = Logger.make<unknown, void>(
let { annotations } = _
const c = getRequestContext(_.context)
const requestContext = c.value
if (requestContext) {
if (requestContext && requestContext.name !== "_root") {
annotations = HashMap.make(...[
...annotations,
...{
Expand Down
24 changes: 8 additions & 16 deletions packages/infra/_src/services/Store/Cosmos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ export function makeCosmosStore({ prefix }: StorageConfig) {
)
return batchResult.flat() as unknown as NonEmptyReadonlyArray<PM>
})
.instrument("cosmos.bulkSet")
.annotateLogs("cosmos.db", containerId)
.instrument("cosmos.bulkSet", { containerId, modelName: name })

const batchSet = (items: NonEmptyReadonlyArray<PM>) => {
return Effect
Expand Down Expand Up @@ -183,8 +182,7 @@ export function makeCosmosStore({ prefix }: StorageConfig) {
})
)
})
.instrument("cosmos.batchSet")
.annotateLogs("cosmos.db", containerId)
.instrument("cosmos.batchSet", { containerId, modelName: name })
}

const s: Store<PM, Id> = {
Expand All @@ -202,8 +200,7 @@ export function makeCosmosStore({ prefix }: StorageConfig) {
.then(({ resources }) => resources)
)
)
.instrument("cosmos.all")
.annotateLogs("cosmos.db", containerId),
.instrument("cosmos.all", { containerId, modelName: name }),
filterJoinSelect: <T extends object>(
filter: FilterJoinSelect,
cursor?: { skip?: number; limit?: number }
Expand Down Expand Up @@ -234,8 +231,7 @@ export function makeCosmosStore({ prefix }: StorageConfig) {
)
return v
})
.instrument("cosmos.filterJoinSelect")
.annotateLogs("cosmos.db", containerId),
.instrument("cosmos.filterJoinSelect", { containerId, modelName: name }),
/**
* May return duplicate results for "join_find", when matching more than once.
*/
Expand Down Expand Up @@ -275,8 +271,7 @@ export function makeCosmosStore({ prefix }: StorageConfig) {
.then(({ resources }) => resources.map((_) => _.f))
)
))
.instrument("cosmos.filter")
.annotateLogs("cosmos.db", containerId)
.instrument("cosmos.filter", { containerId, modelName: name })
},
find: (id) =>
Effect
Expand All @@ -286,8 +281,7 @@ export function makeCosmosStore({ prefix }: StorageConfig) {
.read<PM>()
.then(({ resource }) => Option.fromNullable(resource))
)
.instrument("cosmos.find")
.annotateLogs("cosmos.db", containerId),
.instrument("cosmos.find", { containerId, modelName: name }),
set: (e) =>
Option
.fromNullable(e._etag)
Expand Down Expand Up @@ -330,15 +324,13 @@ export function makeCosmosStore({ prefix }: StorageConfig) {
_etag: x.etag
})
})
.instrument("cosmos.set")
.annotateLogs("cosmos.db", containerId),
.instrument("cosmos.set", { containerId, modelName: name }),
batchSet,
bulkSet,
remove: (e: PM) =>
Effect
.promise(() => container.item(e.id, config?.partitionValue(e)).delete())
.instrument("cosmos.remove")
.annotateLogs("cosmos.db", containerId)
.instrument("cosmos.remove", { containerId, modelName: name })
}

// handle mock data
Expand Down
1 change: 1 addition & 0 deletions packages/infra/_src/services/Store/Disk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function makeDiskStoreInt<Id extends string, PM extends PersistenceModelType<Id>
const store = yield* $(
makeMemoryStoreInt<Id, PM, R, E>(
name,
namespace,
!fs.existsSync(file)
? seed
: fsStore.get
Expand Down
31 changes: 17 additions & 14 deletions packages/infra/_src/services/Store/Memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,20 @@ function logQuery(filter: any, cursor: any) {
}

export function makeMemoryStoreInt<Id extends string, PM extends PersistenceModelType<Id>, R = never, E = never>(
name: string,
modelName: string,
namespace: string,
seed?: Effect<R, E, Iterable<PM>>
) {
return Effect.gen(function*($) {
const updateETag = makeUpdateETag(name)
const updateETag = makeUpdateETag(modelName)
const items_ = yield* $(seed ?? Effect([]))
const items = new Map(items_.toChunk.map((_) => [_.id, _] as const))
const makeStore = (): ReadonlyMap<Id, PM> => new Map([...items.entries()].map(([id, e]) => [id, makeETag(e)]))
const store = Ref.unsafeMake(makeStore())
const sem = Semaphore.unsafeMake(1)
const withPermit = sem.withPermits(1)
const values = store.get.map((s) => s.values())
const all = values.map(ReadonlyArray.fromIterable).instrument("mem.all")
const all = values.map(ReadonlyArray.fromIterable).instrument("mem.all", { modelName, namespace })
const batchSet = (items: NonEmptyReadonlyArray<PM>) =>
items
.forEachEffect((i) => s.find(i.id).flatMap((current) => updateETag(i, current)))
Expand All @@ -72,47 +73,49 @@ export function makeMemoryStoreInt<Id extends string, PM extends PersistenceMode
)
.map((_) => _ as NonEmptyArray<PM>)
.apply(withPermit)
.instrument("mem.batchSet")
const s: Store<PM, Id> = {
all,
find: (id) => store.get.map((_) => Option.fromNullable(_.get(id))).instrument("mem.find"),
find: (id) =>
store.get.map((_) => Option.fromNullable(_.get(id))).instrument("mem.find", { modelName, namespace }),
filter: (filter: Filter<PM>, cursor?: { skip?: number; limit?: number }) =>
all
.tap(() => logQuery(filter, cursor))
.map(memFilter(filter, cursor))
.instrument("mem.filter"),
.instrument("mem.filter", { modelName, namespace }),
filterJoinSelect: <T extends object>(filter: FilterJoinSelect) =>
all.map((c) => c.flatMap(codeFilterJoinSelect<PM, T>(filter))).instrument("mem.filterJoinSelect"),
all.map((c) => c.flatMap(codeFilterJoinSelect<PM, T>(filter))).instrument("mem.filterJoinSelect", {
modelName
}),
set: (e) =>
s
.find(e.id)
.flatMap((current) => updateETag(e, current))
.tap((e) => store.get.map((_) => new Map([..._, [e.id, e]])).flatMap((_) => store.set(_)))
.apply(withPermit)
.instrument("mem.set"),
batchSet: flow(batchSet, (_) => _.instrument("mem.batchSet")),
bulkSet: flow(batchSet, (_) => _.instrument("mem.bulkSet")),
.instrument("mem.set", { modelName, namespace }),
batchSet: flow(batchSet, (_) => _.instrument("mem.batchSet", { modelName, namespace })),
bulkSet: flow(batchSet, (_) => _.instrument("mem.bulkSet", { modelName, namespace })),
remove: (e: PM) =>
store
.get
.map((_) => new Map([..._].filter(([_]) => _ !== e.id)))
.flatMap((_) => store.set(_))
.apply(withPermit)
.instrument("mem.remove")
.instrument("mem.remove", { modelName, namespace })
}
return s
})
}

export const makeMemoryStore = () => ({
make: <Id extends string, PM extends PersistenceModelType<Id>, R = never, E = never>(
name: string,
modelName: string,
seed?: Effect<R, E, Iterable<PM>>,
config?: StoreConfig<PM>
) =>
Effect.gen(function*($) {
const storesSem = Semaphore.unsafeMake(1)
const primary = yield* $(makeMemoryStoreInt<Id, PM, R, E>(name, seed))
const primary = yield* $(makeMemoryStoreInt<Id, PM, R, E>(modelName, "primary", seed))
const ctx = yield* $(Effect.context<R>())
const stores = new Map([["primary", primary]])
const getStore = !config?.allowNamespace ? Effect.succeed(primary) : storeId.get.flatMap((namespace) => {
Expand All @@ -126,7 +129,7 @@ export const makeMemoryStore = () => ({
return storesSem.withPermits(1)(Effect.suspend(() => {
const store = stores.get(namespace)
if (store) return Effect(store)
return makeMemoryStoreInt(name, seed)
return makeMemoryStoreInt(modelName, namespace, seed)
.orDie
.provide(ctx)
.tap((store) => Effect.sync(() => stores.set(namespace, store)))
Expand Down

0 comments on commit a29f57f

Please sign in to comment.