Optionally include pipeline build span attributes in related step spans
1davidmichael opened this issue · 3 comments
What feature do you want to see added?
We are currently using this plugin within our Jenkins and we have multiple steps or stages that share the same name. We would like the ability to track information such as the # of times called or duration of the stages based on the associated pipeline. We are currently unable to do this because step spans do not include the ci.pipeline.name
or ci.pipeline.id
.
Is it possible to allow some of these to carry on into the step spans? Or is there another solution to link Root Build spans and stage spans together? I've tried doing something like this but haven't had luck with it:
withSpanAttribute(key: "custom_pipeline_name", value: "${env.JOB_NAME}")
https://github.com/jenkinsci/opentelemetry-plugin/blob/main/docs/job-traces.md
Upstream changes
No response
Are you interested in contributing this feature?
No response
The trace.id
atribute is the same in all the spans in the same transaction(pipeline build), that field will relate all of them.
The following filter in discover will return all the pipeline steps of a pipeline, notice that we filter the logs events that are also related to the same trace.id
.
trace.id : b23ef79dc016ab89e24485853d954c1e and not data_stream.type: "logs"
Please take a look at my PR, it might have what you are looking for:
with it you can write
String trimSuffix(String original, String suffix) {
if (original.endsWith(suffix)) {
return original.substring(0, original.length() - suffix.length());
}
return original;
}
String trimWithDefault(String original, String suffix, String _default) {
String trimmed = trimSuffix(original, suffix);
return trimmed.length()==0 ? _default : trimSuffix(trimmed, "/");
}
pipeline {
options {
// set these attributes on the root span / trace and (almost) all child spans
withSpanAttributes([
spanAttribute(key: "custom.pipeline.job.group", value: trimWithDefault(env.JOB_NAME, env.JOB_BASE_NAME, "-"), target: 'PIPELINE_ROOT_SPAN'), // useful for filtering with multibranch jobs
spanAttribute(key: "custom.pipeline.job.name", value: env.JOB_NAME, target: 'PIPELINE_ROOT_SPAN'),
spanAttribute(key: "custom.pipeline.job.id", value: env.BUILD_NUMBER, target: 'PIPELINE_ROOT_SPAN'),
spanAttribute(key: "custom.pipeline.job.url", value: env.BUILD_URL, target: 'PIPELINE_ROOT_SPAN')
// at this point in time env.GIT_COMMIT is not yet defined
])
}
stages {
stage('build') {
// additionally set this attribute only on the root span
setSpanAttributes([spanAttribute(key: "custom.pipeline.job.commit", value: env.GIT_COMMIT, target: 'PIPELINE_ROOT_SPAN')])
// ...
withSpanAttributes([
spanAttribute(key: "custom.step.tag", value: "xyz")
]) { // set this custom attribute only on the 'sh' span below
sh '...'
}
}
}
}
the latest versions has the withSpanAttributes
step that allow to add custom attribute context, for all the rest use trace.id
and trace.parent