MCPcopy
hub / github.com/caddyserver/caddy / Emit

Method Emit

modules/caddyevents/app.go:207–324  ·  view source on GitHub ↗

Emit creates and dispatches an event named eventName to all relevant handlers with the metadata data. Events are emitted and propagated synchronously. The returned Event value will have any additional information from the invoked handlers. Note that the data map is not copied, for efficiency. After

(ctx caddy.Context, eventName string, data map[string]any)

Source from the content-addressed store, hash-verified

205// Note that the data map is not copied, for efficiency. After Emit() is called, the
206// data passed in should not be changed in other goroutines.
207func (app *App) Emit(ctx caddy.Context, eventName string, data map[string]any) caddy.Event {
208 logger := app.logger.With(zap.String("name", eventName))
209
210 e, err := caddy.NewEvent(ctx, eventName, data)
211 if err != nil {
212 logger.Error("failed to create event", zap.Error(err))
213 }
214
215 var originModule caddy.ModuleInfo
216 var originModuleID caddy.ModuleID
217 var originModuleName string
218 if origin := e.Origin(); origin != nil {
219 originModule = origin.CaddyModule()
220 originModuleID = originModule.ID
221 originModuleName = originModule.String()
222 }
223
224 logger = logger.With(
225 zap.String("id", e.ID().String()),
226 zap.String("origin", originModuleName))
227
228 // add event info to replacer, make sure it's in the context
229 repl, ok := ctx.Context.Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
230 if !ok {
231 repl = caddy.NewReplacer()
232 ctx.Context = context.WithValue(ctx.Context, caddy.ReplacerCtxKey, repl)
233 }
234 repl.Map(func(key string) (any, bool) {
235 switch key {
236 case "event":
237 return e, true
238 case "event.id":
239 return e.ID(), true
240 case "event.name":
241 return e.Name(), true
242 case "event.time":
243 return e.Timestamp(), true
244 case "event.time_unix":
245 return e.Timestamp().UnixMilli(), true
246 case "event.module":
247 return originModuleID, true
248 case "event.data":
249 return e.Data, true
250 }
251
252 if after, ok0 := strings.CutPrefix(key, "event.data."); ok0 {
253 key = after
254 if val, ok := e.Data[key]; ok {
255 return val, true
256 }
257 }
258
259 return nil, false
260 })
261
262 logger = logger.WithLazy(zap.Any("data", e.Data))
263
264 logger.Debug("event")

Callers

nothing calls this directly

Implementers 6

fooModuleadmin_test.go
PKImodules/caddypki/pki.go
TLSmodules/caddytls/tls.go
Filesystemsmodules/caddyfs/filesystem.go
Appmodules/caddyhttp/app.go
Appmodules/caddyevents/app.go

Calls 14

OriginMethod · 0.95
StringMethod · 0.95
IDMethod · 0.95
NameMethod · 0.95
TimestampMethod · 0.95
WithValueMethod · 0.80
MapMethod · 0.80
CaddyModuleMethod · 0.65
HandleMethod · 0.65
WithMethod · 0.45
StringMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected