-
Notifications
You must be signed in to change notification settings - Fork 0
Initial example of generating Prometheus SDK metrics from Otel semconv with the weaver tool. #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| {%- set my_file_name = ctx.metric_name | lower | snake_case ~ ".go" -%} | ||
| {{- template.set_file_name(my_file_name) -}} | ||
| {% set construct_name = ctx.id.split('.')[-1] %} | ||
| {%- set instrToVecTypes = { | ||
| 'counter': "CounterVec", | ||
| 'gauge': "GaugeVec", | ||
| }-%} | ||
| {%- set instrToOptTypes = { | ||
| 'counter': "CounterOpts", | ||
| 'gauge': "GaugeOpts", | ||
| }-%} | ||
| {%- set labelTypeToTypes = { | ||
| 'string': "string", | ||
| 'int': "int", | ||
| 'double': "float64", | ||
| }-%} | ||
| {% macro const_label_type(label) -%} | ||
| {{ construct_name | pascal_case ~ label.tag | pascal_case }} | ||
| {%- endmacro %} | ||
| // Copyright 2025 The Prometheus Authors | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // Code generated from semantic convention specification. DO NOT EDIT. | ||
|
|
||
| package semconv // TODO(bwplotka): Use id prefix or something more unique? | ||
|
|
||
| import ( | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/prometheus/client_golang/prometheus/promauto" | ||
| ) | ||
|
|
||
| {%- for label in ctx.attributes -%} | ||
| {%- if label.type.members is defined %} | ||
|
|
||
| type {{ const_label_type(label) }} string | ||
|
|
||
| const ( | ||
| {%- for value in label.type.members %} | ||
| {{ value.id | pascal_case }}{{ const_label_type(label) }} {{ const_label_type(label) }} = "{{ value.value }}" | ||
| {%- endfor %} | ||
| ) | ||
| {%- endif %} | ||
| {%- endfor %} | ||
|
|
||
| func MustNew{{ construct_name | pascal_case }}{{ ctx.instrument | pascal_case }}Vec(reg prometheus.Registerer) *prometheus.{{ instrToVecTypes[ctx.instrument] }} { | ||
| return promauto.With(reg).New{{ instrToVecTypes[ctx.instrument] }}(prometheus.{{ instrToOptTypes[ctx.instrument] }}{ | ||
| Name: "{{ ctx.metric_name }}", | ||
| {% if ctx.note is defined -%} | ||
| Help: "{{ ctx.note }}", | ||
| {% else -%} | ||
| Help: "{{ ctx.brief }}", | ||
| {%- endif %} | ||
| // Unit: "{{ ctx.unit }}" // TODO(bwplotka): Add Unit as one of the supported options. | ||
| }, []string{ | ||
| {%- for label in ctx.attributes %} | ||
| "{{label.tag}}", | ||
| {%- endfor %} | ||
| }) | ||
| } | ||
|
|
||
| /* | ||
| TODO(bwplotka): Add more type safety e.g. for CustomElementsCounterVec: | ||
|
|
||
| type CustomElementsCounterVec struct { | ||
| prometheus.CounterVec | ||
| } | ||
|
|
||
| func (v *CustomElementsCounterVec) WithLabelValues(integer int, category CustomElementsCategory, fraction float64) prometheus.Counter { | ||
| // This is not ideal as we do, potentially expensive stringifying on the hot path. | ||
| // Fix might require internals to completely differ in the client_golang for the efficient solution. | ||
| return v.CounterVec.WithLabelValues(fmt.Sprintf("%v", integer), string(category), fmt.Sprintf("%v", fraction)) | ||
| } | ||
| */ | ||
|
|
||
|
|
||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| templates: | ||
| - template: metric.go.j2 | ||
| filter: > | ||
| .groups | ||
| | map(select(.type == "metric")) | ||
| | sort_by(.metric_name) | ||
| application_mode: each | ||
|
|
||
| comment_formats: | ||
| go: | ||
| format: markdown | ||
| prefix: "// " | ||
| indent_first_level_list_items: true | ||
| shortcut_reference_link: true | ||
| trim: true | ||
| remove_trailing_dots: true | ||
| default_comment_format: go |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/prometheus/client_golang/prometheus/promauto" | ||
| ) | ||
|
|
||
| func mustNewCustomStableMetric(reg prometheus.Registerer) *prometheus.CounterVec { | ||
| return promauto.With(reg).NewCounterVec(prometheus.CounterOpts{ | ||
| Name: "my_app_custom_elements_total", | ||
| Help: "Custom counter metric for my app counting important elements. It serves as an example " + | ||
| "of a very important metric that everyone is using.", | ||
| }, []string{"integer", "category", "fraction"}) | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # Schema: https://github.com/open-telemetry/weaver/blob/main/schemas/semconv.schema.json | ||
| groups: | ||
| - id: metric.my-org.custom_elements | ||
| type: metric | ||
| metric_name: my_app_custom_elements_total | ||
| brief: "Custom counter metric for my app counting important elements. It serves as an example of a very important metric that everyone is using." | ||
| instrument: counter | ||
| # Unit schema: https://github.com/open-telemetry/semantic-conventions/blob/main/docs/general/metrics.md#instrument-units | ||
| # It comes from https://unitsofmeasure.org/ucum standard. | ||
| unit: "{elements}" | ||
| attributes: | ||
| - id: metric.my-org.custom_elements.integer | ||
| tag: "integer" | ||
| type: int | ||
| brief: | | ||
| This is an important label that specifies the integer for this count. | ||
| - id: metric.my-org.custom_elements.category | ||
| tag: "category" | ||
| type: | ||
| members: | ||
| - id: a | ||
| value: "a" | ||
| stability: stable | ||
| - id: b | ||
| value: "b" | ||
| stability: stable | ||
| - id: other | ||
| value: "other" | ||
| stability: stable | ||
| brief: | | ||
| This is an important label that specifies the category for this count. | ||
| - id: metric.my-org.custom_elements.fraction | ||
| tag: "fraction" | ||
| type: double | ||
| brief: | | ||
| This is an important label that specifies the fraction for this count. | ||
| stability: stable | ||
| - id: metric.my-org.some_elements | ||
| type: metric | ||
| metric_name: my_app_some_elements_total | ||
| deprecated: "Deprecated, not needed anymore" | ||
| brief: "old metric" | ||
| instrument: counter | ||
| unit: "{unknown}" | ||
| stability: experimental |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hope the intention is clear. This binary will help us showcase the case of e.g. same type of container runs with different versions where metric name changed between e.g.
my-app -metric-source=generated@v0.1.0my-app -metric-source=generated@v0.2.0