|
| 1 | +// Copyright 2020-2024 Tetrate |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + |
| 20 | + "github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm" |
| 21 | + "github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm/types" |
| 22 | +) |
| 23 | + |
| 24 | +func main() {} |
| 25 | +func init() { |
| 26 | + proxywasm.SetVMContext(&vmContext{}) |
| 27 | +} |
| 28 | + |
| 29 | +// vmContext implements types.VMContext. |
| 30 | +type vmContext struct { |
| 31 | + // Embed the default VM context here, |
| 32 | + // so that we don't need to reimplement all the methods. |
| 33 | + types.DefaultVMContext |
| 34 | +} |
| 35 | + |
| 36 | +// NewPluginContext implements types.VMContext. |
| 37 | +func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext { |
| 38 | + return &metricPluginContext{} |
| 39 | +} |
| 40 | + |
| 41 | +// metricPluginContext implements types.PluginContext. |
| 42 | +type metricPluginContext struct { |
| 43 | + // Embed the default plugin context here, |
| 44 | + // so that we don't need to reimplement all the methods. |
| 45 | + types.DefaultPluginContext |
| 46 | +} |
| 47 | + |
| 48 | +// NewHttpContext implements types.PluginContext. |
| 49 | +func (ctx *metricPluginContext) NewHttpContext(contextID uint32) types.HttpContext { |
| 50 | + return &metricHttpContext{} |
| 51 | +} |
| 52 | + |
| 53 | +// metricHttpContext implements types.HttpContext. |
| 54 | +type metricHttpContext struct { |
| 55 | + // Embed the default http context here, |
| 56 | + // so that we don't need to reimplement all the methods. |
| 57 | + types.DefaultHttpContext |
| 58 | +} |
| 59 | + |
| 60 | +const ( |
| 61 | + customHeaderKey = "my-custom-header" |
| 62 | + customHeaderValueTagKey = "value" |
| 63 | +) |
| 64 | + |
| 65 | +// counters is a map from custom header value to a counter metric. |
| 66 | +// Note that Proxy-Wasm plugins are single threaded, so no need to use a lock. |
| 67 | +var counters = map[string]proxywasm.MetricCounter{} |
| 68 | + |
| 69 | +// OnHttpRequestHeaders implements types.HttpContext. |
| 70 | +func (ctx *metricHttpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action { |
| 71 | + customHeaderValue, err := proxywasm.GetHttpRequestHeader(customHeaderKey) |
| 72 | + if err == nil { |
| 73 | + counter, ok := counters[customHeaderValue] |
| 74 | + if !ok { |
| 75 | + // This metric is processed as: custom_header_value_counts{value="foo",reporter="wasmgosdk"} n. |
| 76 | + // The extraction rule is defined in envoy.yaml as a bootstrap configuration. |
| 77 | + // See https://www.envoyproxy.io/docs/envoy/latest/api-v3/config/metrics/v3/stats.proto#config-metrics-v3-statsconfig. |
| 78 | + fqn := fmt.Sprintf("custom_header_value_counts_%s=%s_reporter=wasmgosdk", customHeaderValueTagKey, customHeaderValue) |
| 79 | + counter = proxywasm.DefineCounterMetric(fqn) |
| 80 | + counters[customHeaderValue] = counter |
| 81 | + } |
| 82 | + counter.Increment(1) |
| 83 | + } |
| 84 | + return types.ActionContinue |
| 85 | +} |
0 commit comments