|
| 1 | +/* |
| 2 | + * Copyright 2025 CloudWeGo Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package infra |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + |
| 24 | + "github.com/RanFeng/ilog" |
| 25 | + "github.com/cloudwego/eino-ext/callbacks/apmplus" |
| 26 | + "github.com/cloudwego/eino/callbacks" |
| 27 | + hertzconfig "github.com/cloudwego/hertz/pkg/common/config" |
| 28 | + "github.com/hertz-contrib/obs-opentelemetry/provider" |
| 29 | + hertztracing "github.com/hertz-contrib/obs-opentelemetry/tracing" |
| 30 | +) |
| 31 | + |
| 32 | +var EmptyHertzConfigOption = hertzconfig.Option{} |
| 33 | + |
| 34 | +// InitAPMPlusTracing initializes APMPlus observability components for Eino applications |
| 35 | +// Creates Eino APMPlus callbacks and optionally initializes Hertz server tracing integration based on the withHertzServer flag. |
| 36 | +// Parameters: |
| 37 | +// |
| 38 | +// withHertzServer - If true, enables Hertz HTTP server tracing integration |
| 39 | +// |
| 40 | +// Returns: |
| 41 | +// |
| 42 | +// tracer - Hertz server configuration (nil if withHertzServer=false) |
| 43 | +// cfg - Tracing configuration instance (nil if withHertzServer=false) |
| 44 | +// shutdown - cleanup function for eino apmplus callback to flush telemetry data to APMPlus Server |
| 45 | +func InitAPMPlusTracing(ctx context.Context, withHertzServer bool) (tracer hertzconfig.Option, cfg *hertztracing.Config, shutdown func(ctx context.Context) error) { |
| 46 | + appKey := os.Getenv("APMPLUS_APP_KEY") |
| 47 | + if appKey == "" { |
| 48 | + return EmptyHertzConfigOption, nil, nil |
| 49 | + } |
| 50 | + region := os.Getenv("APMPLUS_REGION") |
| 51 | + if region == "" { |
| 52 | + region = "cn-beijing" |
| 53 | + } |
| 54 | + _, shutdown = initAPMPlusCallback(ctx, appKey, region) |
| 55 | + if !withHertzServer { |
| 56 | + return EmptyHertzConfigOption, nil, shutdown |
| 57 | + } |
| 58 | + // for hertz server, init hertz tracing integration |
| 59 | + tracer, cfg = initHertzTracing(ctx, appKey, region) |
| 60 | + return tracer, cfg, shutdown |
| 61 | +} |
| 62 | + |
| 63 | +// initAPMPlusCallback initializes the Eino APMPlus callback handler. |
| 64 | +// It creates trace spans and collects metrics for Eino applications, |
| 65 | +// which will be sent to the APMPlus server for observability. |
| 66 | +func initAPMPlusCallback(ctx context.Context, appKey, region string) (callbacks.Handler, func(ctx context.Context) error) { |
| 67 | + ilog.EventInfo(ctx, "Init APMPlus callback, watch at: https://console.volcengine.com/apmplus-server", region) |
| 68 | + cbh, shutdown, err := apmplus.NewApmplusHandler(&apmplus.Config{ |
| 69 | + Host: fmt.Sprintf("apmplus-%s.volces.com:4317", region), |
| 70 | + AppKey: appKey, |
| 71 | + ServiceName: "deer-go", |
| 72 | + Release: "release/v0.0.1", |
| 73 | + }) |
| 74 | + if err != nil { |
| 75 | + ilog.EventError(ctx, err, "init apmplus callback failed") |
| 76 | + return nil, nil |
| 77 | + } |
| 78 | + callbacks.AppendGlobalHandlers(cbh) |
| 79 | + ilog.EventInfo(ctx, "Init APMPlus Callback success") |
| 80 | + return cbh, shutdown |
| 81 | +} |
| 82 | + |
| 83 | +// initHertzTracing initializes Hertz framework tracing integration |
| 84 | +// It creates trace spans and collects metrics for incoming HTTP requests, |
| 85 | +// which will be sent to the APMPlus server for observability. |
| 86 | +func initHertzTracing(ctx context.Context, appKey, region string) (hertzconfig.Option, *hertztracing.Config) { |
| 87 | + ilog.EventInfo(ctx, "Init Hertz Tracing", region) |
| 88 | + _ = provider.NewOpenTelemetryProvider( |
| 89 | + provider.WithServiceName("deer-go"), |
| 90 | + provider.WithExportEndpoint(fmt.Sprintf("apmplus-%s.volces.com:4317", region)), |
| 91 | + provider.WithInsecure(), |
| 92 | + provider.WithHeaders(map[string]string{"X-ByteAPM-AppKey": appKey}), |
| 93 | + ) |
| 94 | + tracer, cfg := hertztracing.NewServerTracer() |
| 95 | + return tracer, cfg |
| 96 | + |
| 97 | +} |
0 commit comments